Removing specific rows from a dataframe

后端 未结 4 662
刺人心
刺人心 2020-11-29 03:54

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
4条回答
  •  遥遥无期
    2020-11-29 03:57

    DF[ ! ( ( DF$sub ==1 & DF$day==2) | ( DF$sub ==3 & DF$day==4) ) , ]   # note the ! (negation)
    

    Or if sub is a factor as suggested by your use of quotes:

    DF[ ! paste(sub,day,sep="_") %in% c("1_2", "3_4"), ]
    

    Could also use subset:

    subset(DF,  ! paste(sub,day,sep="_") %in% c("1_2", "3_4") )
    

    (And I endorse the use of which in Dirk's answer when using "[" even though some claim it is not needed.)

提交回复
热议问题