带有R的子集数据帧

时间:2016-05-09 12:59:54

标签: r dataframe subset

如果我们采用一些示例数据,我们可以获得如下的各种输出

A <- (1:10)
B <- (20:29)
df1 <- data.frame(A,B)
D <- c(1,2,3,3)
# with this command, output the first, second, third and third row
df1[D,]

D <- c(5,7,3,3)
# and here the 5th, 7th ....
df1[D,]

但我希望获得第二个数据框,其中D值对应于等效的A

# here we reomve the first two rows of data
df2 <- df1[-c(1,2),]
# now we want to call upon our D and obtain a new data frame with 
# A==5,A==7, and 2x A==3
df2[match(df2$A==D),]

如果我使用它,我没有得到重复值

df2[(df2$A %in% D),]

2 个答案:

答案 0 :(得分:2)

match参数不正确

df2[match(D,df2$A),]
#   A  B
#5   5 24
#7   7 26
#3   3 22
#3.1 3 22

答案 1 :(得分:0)

我不是真的很害羞,但你想要一个这样的数据集:

 A <- (1:10)
 B <- (20:29)
 D <- c(1,2,3,3)
 df1 <- data.frame(A,B)

 df2<-df1[df1$A%in%D,]

但是,我不明白为什么要删除前两行。