user08041991
user08041991

Reputation: 637

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

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

Upvotes: 0

Views: 71

Answers (2)

Otto_K
Otto_K

Reputation: 341

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.

Upvotes: 0

akrun
akrun

Reputation: 887741

The match argument is not correct

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

Upvotes: 2

Related Questions