Importing CSV File into R - Numeric Values Read as Characters

Importing csv file into R - numeric values read as characters

Whatever algebra you are doing in Excel to create the new column could probably be done more effectively in R.

Please try the following: Read the raw file (before any excel manipulation) into R using read.csv(... stringsAsFactors=FALSE). [If that does not work, please take a look at ?read.table (which read.csv wraps), however there may be some other underlying issue].

For example:

   delim = ","  # or is it "\t" ?
dec = "." # or is it "," ?
myDataFrame <- read.csv("path/to/file.csv", header=TRUE, sep=delim, dec=dec, stringsAsFactors=FALSE)

Then, let's say your numeric columns is column 4

   myDataFrame[, 4]  <- as.numeric(myDataFrame[, 4])  # you can also refer to the column by "itsName"


Lastly, if you need any help with accomplishing in R the same tasks that you've done in Excel, there are plenty of folks here who would be happy to help you out

R: Importing read.csv with numeric format

It looks as if your data contains a 1000s separator where the , is giving you problems. You can either read in the data.frame and convert the relevant columns using gsub or you can define a new class definition as suggested in one of the following links:

  • How to load df with 1000 separator in R as numeric class?
  • processing negative number in "accounting" format

Here we define a new class that removes the commas (the 1000 separator).

setClass("MyNum")
setAs("character", "MyNum",
function(from) as.numeric(gsub(",", "", from) ))
indata <- read.csv("tst.txt", ,
header = TRUE,
sep = "\t",
dec = ".",
colClasses=c(rep("character", 10), "MyNum", "character"))

Alternatively just use as.numeric(gsub(",", "", from) ) where from is the vector containing the 1000s separator.

R, Read in characters as numeric in one step using read.csv?

I've finally found what was wrong. The "NA's" were encoded as "unknown" in the original file (before reading into R). I now realize I was being quite dense. Thank you all for your patience and help. This is the code I ended up using:

d <- read.csv("data.csv",stringsAsFactors=F, na.strings="unknown")

Problems importing numeric data in csv format using R veriosn 3.6.1

You already try import your data with function 'read.csv()'?



Related Topics



Leave a reply



Submit