简体   繁体   English

带R的子集数据帧

[英]subset data frame with R

If we take some example data, we can obtain various outputs as follows 如果我们以一些示例数据为例,我们可以获得以下各种输出

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,]

But I want to obtain a second data frame where the D values correspond to an equivalent A 但是我想获得第二个数据帧,其中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),]

If I use this, I do not get the repeated values 如果使用此选项,则不会得到重复的值

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

The match argument is not correct match参数不正确

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

I'm not really shure, but do you want a data set like this: 我不是很确定,但是您是否想要这样的数据集:

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

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

However I don't understand why you remove the first 2 rows. 但是我不明白为什么要删除前两行。

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

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