COxf
COxf

Reputation: 45

Split Data Frame and call subframe rows by their index

This is a very basic R programming question but I haven't found the answer anywhere, would really appreciate your help:

I split my large dataframe into 23 subframes of 4 rows in length as follows:

DataframeSplits <- split(Dataframe,rep(1:23,each=4))

Say I want to call the second subframe I can:

DataframeSplits[2]

But what if I want to call a specific row of that subframe (using index position)?

I was hoping for something like this (say I call 2nd subframe's 2nd row):

DataframeSplits[2][2,]

But that doesn't work with the error message

Error in DataframeSplits[2][2, ] : incorrect number of dimensions

Upvotes: 2

Views: 480

Answers (1)

loki
loki

Reputation: 10350

If you want to subset the list which is returned by split and use it for later subsetting, you must use double parenthesis like this to get to the sub-data.frame. Then you can subset this one with single parenthesis as you already tried:

Dataframe <- data.frame(x = rep(c("a", "b", "c", "d"), 23), y = 1)
DataframeSplits <- split(Dataframe,rep(1:23,each=4))

DataframeSplits[[2]][2,]

#   x y
# 6 b 1

More info on subsetting can be found in the excellent book by Hadley Wickham.

Upvotes: 1

Related Questions