简体   繁体   中英

Subset data.frame by column

I have this 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".

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% :

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 . We convert the 'data.frame' to 'data.table' ( setDT(df) ), and set the 'key' as column 'a', then we subset the rows.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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