lea
lea

Reputation: 1

convert a char to num R

I have this data

  names(tcars) <- c("MPG",

  "DISPLACEMENT","HORSEPOWER","WEIGHT","ACCELERATION")
     str(tcars) 'data.frame':   38 obs. of  5 variables:  $ MPG         : chr  "16,9" "15,5" "19,2" "18,5" ...  $ DISPLACEMENT: int  350 351 267
  360 98 134 119 105 131 163 ...  $ HORSEPOWER  : int  155 142 125 150
  68 95 97 75 103 125 ...  $ WEIGHT      : chr  "4,36" "4,054" "3,605"
  "3,94" ...  $ ACCELERATION: chr  "2,73" "2,26" "2,56" "2,45" ...

I tried to convert the MPG column to numeric with the following request and got the message below

  MPG <- as.numeric(tcars$MPG) NAs introduits lors de la conversion automatique and the type was not converted
  str(tcars) 'data.frame':  38 obs. of  5 variables:  $ MPG         : chr  "16,9" "15,5" "19,2" "18,5" ...  $ DISPLACEMENT: int  350 351 267
  360 98 134 119 105 131 163 ...  $ HORSEPOWER  : int  155 142 125 150
  68 95 97 75 103 125 ...  $ WEIGHT      : chr  "4,36" "4,054" "3,605"
  "3,94" ...  $ ACCELERATION: chr  "2,73" "2,26" "2,56" "2,45" ...

so I tried this code and all the character type of my data changed to NA :(

  tcars[] <- lapply(tcars, function(x) as.numeric(as.character(x))) NAs introduits lors de la conversion automatiqueNAs introduits lors de la conversion automatique NAs introduits lors de la conversion automatique

  str(tcars$MPG)  num [1:38] NA NA NA NA 30 NA NA NA NA 17 ...
  data.frame(tcars$MPG)
  data.frame(tcars$DISPLACEMENT)

  str(tcars) 'data.frame':  38 obs. of  5 variables: 
  $ MPG         : num  NA NA NA NA 30 NA NA NA NA 17 ...  $ DISPLACEMENT: num  350 351   267 360 98 134 119 105 131 163 ...  $ HORSEPOWER  : num  155 142 125    150 68 95 97 75 103 125 ...  $ WEIGHT      : num  NA NA NA NA NA NA NA NA NA NA ...  $ ACCELERATION: num  NA NA NA NA NA NA NA NA NA NA ...

Upvotes: 0

Views: 148

Answers (2)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21440

A dplyrsolution:

tcars %>%
  mutate(across(everything(), ~as.numeric(sub(",", ".", .))))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389265

Replace commas before turning the data to numeric.

tcars[] <- lapply(tcars, function(x) as.numeric(sub(',', '.', x)))
tcars

#   MPG HORSEPOWER WEIGHT
#1 16.9        155  4.360
#2 15.5        142  4.054
#3 19.2        125  3.605
#4 18.5        150  3.566

This is assuming that numbers like "16,9" and "15,5" are actually 16.9 and 15.5 respectively.

data

tcars <- data.frame(MPG = c("16,9", "15,5", "19,2", "18,5"), 
                    HORSEPOWER = c(155, 142, 125, 150), 
                    WEIGHT = c("4,36", "4,054", "3,605", "3,566"))

Upvotes: 1

Related Questions