How can I get rid of NA values on data import in R?
Follow up to this post:
http://r.789695.n4.nabble.com/importing-csv-gets-me-all-16-000-columns-with-quot-NA-quot-td3006480.html
Some background: I am developing a program that allows the user to upload
csv files. Currently, I am testing a dataset that looks like:
Type Date Lively Count
sm 1/13/2010 10 10
sm 1/14/2010 10 20
sm 2/15/2010 20 30
am 4/16/2010 5 42
am 1/17/2010 10 34
am 3/18/2010 40 54
sm 1/19/2010 10 65
sm 4/20/2010 5 67
sm 3/21/2010 40 76
sm 3/21/2010 70 76
When the user imports this, everything is fine. My import method is:
dataset <- read.csv(input$file$datapath)
dataset <- na.omit(dataset)
However, let's say the user saved the above table in Excel, saving it as a
csv. Then he deleted the last two columns.
Type Date
sm 1/13/2010
sm 1/14/2010
sm 2/15/2010
am 4/16/2010
am 1/17/2010
am 3/18/2010
sm 1/19/2010
sm 4/20/2010
sm 3/21/2010
sm 3/21/2010
If I looked at the str() of this table, the last 2 columns wold now
contain NA values because in Excel, the columns have already been
formatted. I can't take in these NA values, as they mess up my program
later on. I'd like to get rid of them. My na.omit() doesn't seem to do
anything about the NAs.
I have found a solution using
dataset[is.na(dataset)] <- c("")
to replace these columns with blank chars, but this could mess me up later
when I'm checking which columns of the uploaded dataset are characters!
Does anyone have a better solution (telling the user to upload the file in
another Excel sheet is not an option )?
No comments:
Post a Comment