I need help with a simple task. I have a dataframe 28x52 and I want to make several dataframes and rename each one. So it'll turn into 13 28x4 data frames. I know this probably involves a for
loop and so I tried:
for(i in seq(1,52,4)){
i <- data[,i:i+3]
}
It didn't work though.
Here's one approach on a 3x9 source data.frame
where we want to subset it into 3 3x3 data.frame
s.
m <- data.frame(matrix(1:27, ncol = 9))
lapply(split(sequence(ncol(m)), rep(1:3, each = 3)), function(x) m[, x])
# $`1`
# X1 X2 X3
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
#
# $`2`
# X4 X5 X6
# 1 10 13 16
# 2 11 14 17
# 3 12 15 18
#
# $`3`
# X7 X8 X9
# 1 19 22 25
# 2 20 23 26
# 3 21 24 27
Create a sequence of the columns you want to extract, and create a list
of your data.frame
s.
So, applied to something that replicates the structure of your data, you could use:
m <- data.frame(matrix(1:(28*52), ncol = 52))
lapply(split(sequence(ncol(m)), rep(1:(52/4), each = 4)), function(x) m[, x])
Collected from the Internet
Please contact debug[email protected] to delete if infringement.
Comments