Help with reading CSV columns
Hello, I am new to programming and making great progress in learning but I can't figure this one problem out. Here goes;
So I am using fread to read a CSV with like 15 columns and about 100 rows. Then I narrow down the number of columns and keep the ones I want, about 5. One columns is a date, which I am trying to covert from a string type to a date type, using as.Date. But only that column the others will remain as string types.
Here is the issue, I want to pass just the column to as.Date but it gives and error
Error in `[<-.data.table`(`*tmp*`, , value_date, value = c(16035, 16035, :
object 'value_date' not found
Why does it not see 'value_date'?
Can I not pass a column name the way I am?
If I put variables like x and z the code works but I would like to use like below so that the rest of the code can work.
Here is the code:
ALLdata <- fread("/user/435/data/code/ALL.csv")
ALLdata<- Alldata[,c("serial","first","last","value_date","produce"),with=F]
ALLdata[,value_date] <- c(paste(substr(ALLdata[,value_date],1,4),"-",substr(ALLdata[,value_date],5,6),"-",substr(ALLdata[,value_date],7,8)))
ALLdata[,value_date] <- as.Date(x, "%Y - %m - %d")
Thank You for any help.
Posted Jan 16, 2020, 3:29 PM UTC
I think the issue is that value_date is a variable, "value_date" would be a column name.
Hmm so I need to put "..." around all of the value_date?
The thing is further down the code, I didn't share that part, I try to the exact same thing with another CSV, I keep columns I want and convert string type to a date type, and I don't have any problems having R do its thing. It's so weird!
Yes, R is interpreting value_date as a variable, a name for a value, string or data structure. When you quote it it is seen as a string.
You probably already went over variables earlier but they can be a bit confusing when starting out.
Ok, a little update but not sure if it would change things. I checked the "class" of value_date and it is an integer. Will that change things? That is the only thing that is different between the two parts of code I have.
Thanks for your help.
Yeah, that should say something along the lines of "error value_date not found".
You may have assigned it a value at some point. Can you check what its value is?