Get a subset not containing a given value of the c

2019-03-06 08:54发布

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")

标签: r subset
3条回答
放我归山
2楼-- · 2019-03-06 08:59
df <- read.csv(text="A,22
B,333
C,Not Av.
D,Not Av.", header=F)

df[df[,2] != "Not Av.",]
查看更多
一夜七次
3楼-- · 2019-03-06 09:03

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
查看更多
\"骚年 ilove
4楼-- · 2019-03-06 09:22

In case you still want to use the subset function:

df<-subset(df,!grepl("Not Av",df[,2]))
查看更多
登录 后发表回答