Reputation: 45
Starting from a data.frame, I want to create several others with different orderings. I'll put an example straight away because I believe it's easier to understand my doubts.
data = data.frame(x1 = sample(1:100,20),
x2 = sample(1:100,20),
x3 = sample(1:100,20),
x4 = sample(1:100,20),
x5 = sample(1:100,20))
all_data <- list()
library(dplyr)
for (i in 1:5) {
all_data[[i]] <- arrange(data, colnames(data)[1:i])
}
So based on the example above, I would like to create several data.frames, where each one will be ordered based on a set of columns. Starting from the first data.frame which will be ordered only by the first column. Then the second data.frame that will be sorted based on the first and second column. Then the third, which will be in relation to the first, second and third column, and so on.
Upvotes: 3
Views: 175
Reputation: 948
for (i in 1:5) {
all_data[[i]] <- data %>% arrange(across(1:i))
}
Upvotes: 3