1
votes

Say i have the following data

B <- (5:20)
C <- (6:21)
D <- (7:22)
E <- (8:23)
data <- data.frame(B,C,D,E)

and I also have a matrix of

id <- c(4,7,9,12,15)

where this matrix represents the row identities I want to output into a new data.frame

How can one use the subset function to subset the original data

new <- subset(data, .....)

so that new only consists of the 5 observations

1
Don't use subset. It's as simple as data[id,]. Study help("[").Roland
small remark : id is not a matrix, it is a vectorCath
thanks for the advice on what to research @Rolandlukeg

1 Answers

5
votes

Try

data[id,]
#    B  C  D  E
#4   8  9 10 11
#7  11 12 13 14
#9  13 14 15 16
#12 16 17 18 19
#15 19 20 21 22

The syntax data[i,j] creates a subset of data with row(s) i and column(s) j