Question

I have to read a csv file with read.table, but I have a small problem: my csv contains some rows like that:

  A  B  C
1 2  3  4
2 2  5  6
3 4  8  error:8
. .  .  .
. .  .  .
. .  .  .

I would like to remove all the lines that contains error in my csv file or remove error and let the lines, I don't know if it's possible with R? this is my code:

file <- paste("C:\\test.csv")
table <- read.table(file,sep=",",header=T,fill=TRUE)
Was it helpful?

Solution

This should work:

dat.file <- paste("C:\\test.csv")

# use readLines() to get file line-by-line 
dat.in <- readLines(dat.file)

# filter out "error"
dat.in <- dat.in[grep("error", dat.in, invert=TRUE)]

# turn structure into a string we can pass to textConnection
read.csv(textConnection(paste(dat.in, collapse="\n")), header=TRUE)

If you were on a Linux/OS X system, I'd have had you pipe the file through the system grep, but, sigh, #Windows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top