[英]subset data frame with R
如果我们以一些示例数据为例,我们可以获得以下各种输出
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),]
match
参数不正确
df2[match(D,df2$A),]
# A B
#5 5 24
#7 7 26
#3 3 22
#3.1 3 22
我不是很确定,但是您是否想要这样的数据集:
A <- (1:10)
B <- (20:29)
D <- c(1,2,3,3)
df1 <- data.frame(A,B)
df2<-df1[df1$A%in%D,]
但是我不明白为什么要删除前两行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.