问题描述
我有一个数据框架,例如:
I have a data frame e.g.:
sub day
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4
我想删除可以识别的特定行子和日的组合。
例如,我想删除行,其中sub ='1'和day ='2',sub = 3和day ='4'。我该怎么做?
我意识到我可以指定行号,但是这需要应用到一个巨大的数据帧,这将是繁琐的,每一行都要经历ID和ID。
and I would like to remove specific rows that can be identified by the combination of sub and day.For example say I wanted to remove rows where sub='1' and day='2' and sub=3 and day='4'. How could I do this?I realise that I could specify the row numbers, but this needs to be applied to a huge dataframe which would be tedious to go through and ID each row.
推荐答案
DF[ ! DF$sub %in% c(1, 2, 3), ] # note the ! (negation)
或者如果sub是使用引号建议的因素:
Or if sub is a factor as suggested by your use of quotes:
DF[ ! DF$sub %in% c("1", "2", "3"), ]
也可以使用子集:
subset(DF, ! sub %in% c("1", "2", "3") )
(我认同使用哪个
在Dirk的答案中使用[即使有人声称它不需要。)
(And I endorse the use of which
in Dirk's answer when using "[" even though some claim it is not needed.)
这篇关于从数据帧中删除特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!