Importing in R; (read.csv) Ignore non-numeric columns automatically -
i want import tsv file including non-numeric fields (i.e., date or string) in r:
num1 num2 date 1 2 2012-10-18 12:17:19 2 4 2014-11-16 09:30:23 4 11 2010-03-18 22:18:04 12 3 2015-02-18 12:55:50 13 1 2014-05-16 10:39:11 2 14 2011-05-26 20:48:54
i using following command:
a = read.csv("c:\test\testfile.tsv", sep="\t")
i want ignore non-numeric values automatically (or put "na"). , don't want mention string column names ignored.
i tried "stringsasfactors" , "as.is" parameters, no success.
any ideas?
you have quite few options here. first, can inform r while reading table:
data <- read.csv("c:\test\testfile.tsv", sep="\t", colclasses=c(na, na, "null"))
if have many nonnumeric columns, 10, can use rep
colclasses=c(na, na, rep("null", 10))
.
second, can read , process deletion afterwards (note stringsasfactors):
data <- read.csv("c:\test\testfile.tsv", sep="\t", stringsasfactors = false)
you can subset column identified character.
df[, !sapply(df, is.character)]
or apply destructive method data.frame:
df[sapply(df, is.character)] <- list(null)
you can go further make sure numeric columns left:
df[,-grep ("date|factor|character", sapply(df, class))] <- list(null)
Comments
Post a Comment