Get a subset not containing a given value of the column

孤街醉人 提交于 2020-01-11 14:23:08

问题


I have a table called data:

A 22
B 333
C Not Av.
D Not Av.

How can I get a subset, from which all rows containing "Not Av." are excluded? It is important to mention that I have the index of a column to be checked (in this case colnum = 2), but I don't have its name.

I tried this, but it does not work:

data<-subset(data,colnum!="Not Available")

回答1:


df <- read.csv(text="A,22
B,333
C,Not Av.
D,Not Av.", header=F)

df[df[,2] != "Not Av.",]



回答2:


You don't really need the subset function. Just use [:

> set.seed(42)
> DF <- data.frame(x = LETTERS[1:10], 
                   y = sample(c(1, 2, 3, "Not Av."), 10, replace = TRUE))
> DF
   x       y
1  A Not Av.
2  B Not Av.
3  C       2
4  D Not Av.
5  E       3
6  F       3
7  G       3
8  H       1
9  I       3
10 J       3
> DF[DF[2] != "Not Av.",]
   x y
3  C 2
5  E 3
6  F 3
7  G 3
8  H 1
9  I 3
10 J 3



回答3:


In case you still want to use the subset function:

df<-subset(df,!grepl("Not Av",df[,2]))


来源:https://stackoverflow.com/questions/29290798/get-a-subset-not-containing-a-given-value-of-the-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!