How can I get rid of NA values on data import in R? -
follow post:
some background: developing program allows user upload csv files. currently, testing dataset looks like:
type date lively count sm 1/13/2010 10 10 sm 1/14/2010 10 20 sm 2/15/2010 20 30 4/16/2010 5 42 1/17/2010 10 34 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 user imports this, fine. import method is:
dataset <- read.csv(input$file$datapath) dataset <- na.omit(dataset)
however, let's user saved above table in excel, saving csv. deleted last 2 columns.
type date sm 1/13/2010 sm 1/14/2010 sm 2/15/2010 4/16/2010 1/17/2010 3/18/2010 sm 1/19/2010 sm 4/20/2010 sm 3/21/2010 sm 3/21/2010
if looked @ str() of table, last 2 columns wold contain na values because in excel, columns have been formatted. can't take in these na values, mess program later on. i'd rid of them. na.omit() doesn't seem nas.
i have found solution using
dataset[is.na(dataset)] <- c("")
to replace these columns blank chars, mess me later when i'm checking columns of uploaded dataset characters!
does have better solution (telling user upload file in excel sheet not option )?
to remove columns containing na
:
dataset [,colsums(is.na(dataset )) <nrow(dataset),drop=false]
Comments
Post a Comment