I am reading a csv file into a dataframe in R. The first column contains dates and the following 50 columns contain floats. I am having trouble coercing the 50 columns to numeric type vs. character. First, I try using the colClasses
argument:
data <- read.csv(paste("regional_data_",term,".csv", sep=""), colClasses=c("character", rep("numeric", 50)))
head(data)
| date | Illinois | Delaware | ...
1 | 2015-10-31 | 419.34 | 632.72 | ...
2 | 2015-11-30 | 513.21 | 507.43 | ...
3 | 2015-12-31 | 629.86 | 623.72 | ...
...
However, when I check the types, they appear to be 'character'.
apply(data , 2, mode)
date 'character'
Illinois 'character'
Delaware 'character'
...
Further, I tried coercing the latter 50 columns to numeric type, but this did not work either.
data[,-1] <- apply(data[,-1], 2, function(x) as.numeric(x))
apply(data , 2, mode)
date 'character'
Illinois 'character'
Delaware 'character'
...
What am I doing wrong? The characters are preventing me from using binary operations on my data.