Home > Blockchain >  Convert character variables into numeric with R
Convert character variables into numeric with R

Time:12-30

FIRST QUESTION EVER ;)

Here's the point: I have this dataset and I started without "stringsAsFactors=FALSE" in read.csv function. I can't work with those data because I got the Warning message: NAs introduced by coercion. Thank you for the help :)

rm(list=ls())

path <- "....."
file <- read.csv(path, header = TRUE, sep = ",", stringsAsFactors=FALSE)

str(file)
#'data.frame':  33 obs. of  11 variables:
#$ Var1: chr  "01/09/2021" "02/09/2021" "09/09/2021" "10/09/2021" ...
#$ Var2: chr  "mercoledì" "giovedì" "giovedì" "venerdì" ...
#$ Var3: chr  "2,5" "2,5" "2,5" "3,0" ...
#$ Var4: chr  "4,0" "0,0" "2,0" "3,0" ...
#$ Var5: chr  "2,0" "5,0" "5,0" "5,0" ...
#$ Var5: chr  "0,0" "0,0" "0,0" "0,0" ...
#$ Var6: chr  "6,0" "5,0" "7,0" "8,0" ...
#$ Var7: chr  "23,5" "25,0" "28,0" "32,0" ...
#$ Var8: chr  "0,0" "1,0" "5,0" "5,5" ...
#$ Var9: chr  "23,5" "26,0" "33,0" "37,5" ...
#$ Var10: chr  "67,0" "0,0" "0,0" "0,0" ...

as.numeric(file$Var7)

1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA Warning message: NAs introduced by coercion

CSV FILE

CodePudding user response:

I managed to recreate your problem. Your file is using , both as field separator and decimal separator (which is uncommon).

You can fix your problem by specifying that decimals are commas in (dec = ",") in read.csv(), as follows:

read.csv(
  path, 
  header = TRUE, 
  sep = ",", 
  dec = ",", # I've added this line
  stringsAsFactors = FALSE
)

Change this, run str(file) again, and you should see that most columns are numeric.

  • Related