0

I want to import a tsv file including some 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 am using the following command:

a = read.csv("C:\test\testFile.tsv", sep="\t")

I want to ignore all non-numeric values automatically (or put something like "NA"). And don't want to mention all the string column names to be ignored.

I tried "stringsAsFactors" and "as.is" parameters, with no success.

Any ideas?

2
  • That's a good idea. Thank you. But, again, we have to check of the field types (for more than 10 fields in the real case). We want the correlation between numeric values. I tried rcorr(as.matrix(a[,1:2])) and it works for this case, but not for the more complicated case. Commented Jul 7, 2015 at 22:55
  • 1
    @Alisa - Maybe consider doing a <- a[sapply(a,is.numeric)] Commented Jul 7, 2015 at 22:59

2 Answers 2

1

You have quite a few options here. First, you can inform R while reading the table:

data <- read.csv("C:\test\testFile.tsv", 
sep="\t", 
colClasses=c(NA, NA, "NULL"))

If you have many nonnumeric columns, say 10, you can use rep as colClasses=c(NA, NA, rep("NULL", 10)).

Second, you can read everything and process deletion afterwards (note the stringsAsFactors):

data <- read.csv("C:\test\testFile.tsv", 
    sep="\t", stringsAsFactors = FALSE)

You can subset everything column that is identified as character.

df[, !sapply(df, is.character)]

Or then apply a destructive method to you data.frame:

  df[sapply(df, is.character)] <- list(NULL)

You can go further to make sure only numeric columns are left:

df[,-grep ("Date|factor|character", sapply(df, class))] <- list(NULL)
Sign up to request clarification or add additional context in comments.

Comments

0

Just found this solution:

a = read.csv("C:\test\testFile.tsv", sep="\t", colClasses=c(NA, NA, "NULL"))

It is not completely automatic though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.