Reputation: 17
How can we select a csv file rows that contain information in a certain column in R?
For example, I have a csv file, which has a column called "index" but not all the rows has information on that column. I only need these rows that has information on the index. How can I select these rows and subset them in R?
Upvotes: 0
Views: 321
Reputation: 98
In an easily readable but somewhat not fancy way:
index <- table$Index
selector <- nchar(index)>0 & !is.na(index)
table[selector]
If you're using data.table:
DT[!is.na(Index) & nchar(Index)>0, ]
Upvotes: 0
Reputation: 1428
Following @r2evans' approach, but assuming Index might contain values other than "A1":
dat <- read.csv("path", stringsAsFactors=FALSE)
dat_filtered <- dat[!is.na(dat$Index) & grepl("\\S", dat$Index), ]
Upvotes: 2