bshelt141
bshelt141

Reputation: 1223

Subsetting a data frame by rows, dependent on the column number in R

I'd like to subset a data frame in R by different row counts, depending on which column the data is in. For example, we start with this data frame:

df <- data.frame(t1 = c(1:6), t2 = c(4:9), t3 = (7:12), t4 = c(12:17), t5 = c(18:23))

df
  t1 t2 t3 t4 t5
1  1  4  7 12 18
2  2  5  8 13 19
3  3  6  9 14 20
4  4  7 10 15 21
5  5  8 11 16 22
6  6  9 12 17 23

Now, I'd like to subset the data frame by capturing rows 5:6 for column t1, rows 4:5 for column t2, rows 3:4 for column t3, etc. (e.g., 1 row higher set of 2 data points for each column we go out) so that the new data fram looks like this:

df2
  t1 t2 t3 t4 t5
1  5  7  9 13 18
2  6  8 10 14 19

My real-work problem can have up to 50 columns, so I'd love to be able to use a clean, simple function that I'm sure is out there.

Thanks in advance.

Upvotes: 1

Views: 185

Answers (2)

akrun
akrun

Reputation: 887048

You may try

n <- nrow(df)
s1 <- 1:ncol(df)
df2 <- as.data.frame(matrix(df[cbind(c(n-1, n) - rep((s1-1), each=2), 
         rep(s1,each=2))],ncol=ncol(df), dimnames=list(NULL, names(df))))
df2
#  t1 t2 t3 t4 t5
#1  5  7  9 13 18
#2  6  8 10 14 19

Upvotes: 2

Marta Cz-C
Marta Cz-C

Reputation: 789

You can use sapply, for example:

df <- data.frame(t1 = c(1:6), t2 = c(4:9), t3 = (7:12), t4 = c(12:17), t5 = c(18:23))
m <-sapply(1:ncol(df), function(i){
  df[(6-i):(7-i),i]
  })
df2 <- data.frame(m)
colnames(df2) <- colnames(df)

Upvotes: 2

Related Questions