Filtering a data frame by factors in R

前端 未结 2 973
无人及你
无人及你 2021-01-13 11:04

I have the following dataframe:

sp <- combn(c(\"sp1\",\"sp2\",\"sp3\",\"sp4\"),2)
d <- data.frame(t(sp),\"freq\"=sample(0:100,6))

and

相关标签:
2条回答
  • 2021-01-13 11:20

    Do not make x1 and x2 factors. Just use vectors. Use %in% for the logical test.

    sp <- combn(c("sp1","sp2","sp3","sp4"),2)
    d <- data.frame(t(sp),"freq"=sample(0:100,6))
    x1 <- c("sp1","sp2")
    x2 <- c("sp3","sp4")
    sub <- d[d$X1 %in% x1 & d$X2 %in% x2,]
    
    0 讨论(0)
  • 2021-01-13 11:24

    You are almost there:

    d[d$X1 %in% x1 & d$X2 %in% x2,]
    
    0 讨论(0)
提交回复
热议问题