简体   繁体   English

子集data.frame by column

[英]Subset data.frame by column

I have this data.frame: 我有这个data.frame:

a <- c(rep("1", 3), rep("2", 3), rep("3",3), rep("4",3), rep("5",3))
b <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
df <-data.frame(a,b)
       a  b
1  1  1
2  1  2
3  1  3
4  2  4
5  2  5
6  2  6
7  3  7
8  3  8
9  3  9
10 4 10
11 4 11
12 4 12
13 5 13
14 5 14
15 5 15

I want to have something like this: 我想要这样的东西:

a <- c(rep("2", 3), rep("3", 3))
b <- c(4,5,6,7,8,9)

dffinal<-data.frame(a,b)

  a b
1 2 4
2 2 5
3 2 6
4 3 7
5 3 8
6 3 9

I could use the "subset" function, but its not working 我可以使用“子集”功能,但无法正常工作

sub <- subset(df,c(2,3) == a )

      a b
    5 2 5
    8 3 8

This command only takes one row of "2" and "3" in column "a". 该命令仅在列“ a”中使用一行“ 2”和“ 3”。

Any Help? 有帮助吗?

what about this? 那这个呢?

library(dplyr)
df %>% filter(a == 2 | a==3)
  a b
1 2 4
2 2 5
3 2 6
4 3 7
5 3 8
6 3 9

You're confusing == with %in% : 您将==%in%混淆了:

subset(df, a %in% c(2,3))
#   a b
# 4 2 4
# 5 2 5
# 6 2 6
# 7 3 7
# 8 3 8
# 9 3 9

We can use data.table . 我们可以使用data.table We convert the 'data.frame' to 'data.table' ( setDT(df) ), and set the 'key' as column 'a', then we subset the rows. 我们将'data.frame'转换为'data.table'( setDT(df) ),并将'key'设置为'a'列,然后对行进行子集化。

library(data.table)
setDT(df, key= 'a')[c('2','3')]
#   a b
#1: 2 4
#2: 2 5
#3: 2 6
#4: 3 7
#5: 3 8
#6: 3 9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM