H C
H C

Reputation: 17

How can we select a csv file rows that contain information in a certain column in R?

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?

a csv file, only row 241 and some other rows have information on the index column. I only need these rows.

Upvotes: 0

Views: 321

Answers (2)

J_Alaniz
J_Alaniz

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

RyanFrost
RyanFrost

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

Related Questions