Electrino
Electrino

Reputation: 2900

Function to split a data frame into n groups of data frames in R

I'm trying to write a function that takes a data frame and splits it into multiple data frames, every specified number of columns. For example, if a have a data frame that looks something like this:

df <- data.frame(
x1 = rnorm(1000),
x2 = rnorm(1000),
x3 = rnorm(1000),
x4 = rnorm(1000),
x5 = rnorm(1000),
x6 = rnorm(1000)
)

I want to split it, say, every second column and save that as it's own data frame. So the pseudocode would look something like:

function(df){
split df every second column
save each group as it's own unique data frame (named A-C for this example)
}

the end result would mean I have 3 data frames with data frame A having two columns (i.e., x1,x2), similarly B has two columns (x3,x4), and C has two columns (x5,x6).

Any suggestions as to how I might achieve this?

Upvotes: 3

Views: 299

Answers (2)

akrun
akrun

Reputation: 887951

We can use split.default to split into 2 columns each into a list

lst1 <- split.default(df, LETTERS[as.integer(gl(ncol(df), 2, ncol(df)))])

and then save the dataset with write.csv

lapply(names(lst1), function(x) write.csv(lst1[[x]], paste0(x, ".csv")))

Upvotes: 4

Ronak Shah
Ronak Shah

Reputation: 389325

We can use split.default to split data into every two columns

temp <- split.default(df, gl(ncol(df)/2, 2))
temp

#$`1`
#          x1       x2
#1  -0.560476  1.22408
#2  -0.230177  0.35981
#3   1.558708  0.40077
#4   0.070508  0.11068
#5   0.129288 -0.55584
#6   1.715065  1.78691
#7   0.460916  0.49785
#8  -1.265061 -1.96662
#9  -0.686853  0.70136
#10 -0.445662 -0.47279

#$`2`
#         x3        x4
#1  -1.06782  0.426464
#2  -0.21797 -0.295071
#3  -1.02600  0.895126
#4  -0.72889  0.878133
#5  -0.62504  0.821581
#....
#....

We can keep the data in the list or if needed in separate dataframe, we can do

names(temp) <- LETTERS[1:3]
list2env(temp, .GlobalEnv)

data

set.seed(123)
df <- data.frame(
  x1 = rnorm(10),
  x2 = rnorm(10),
  x3 = rnorm(10),
  x4 = rnorm(10),
  x5 = rnorm(10),
  x6 = rnorm(10)
)

Upvotes: 2

Related Questions