lukeg
lukeg

Reputation: 1357

subset data based on matrix of row numbers

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

Upvotes: 1

Views: 1356

Answers (1)

talat
talat

Reputation: 70266

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

Upvotes: 5

Related Questions