I am basically new in R. I have a data frame presented below and I would like to create multiple files from the original data frame maintaining the first two columns while using the rest of the data frame as my third column. Each of the new data frame will be save as new file. here's the scenario:
Original data frame
data
id x1 x2 x3
1 A 1 11 4
2 A 2 12 6
3 B 3 13 7
4 B 4 14 9
5 B 5 15 8
6 C 6 16 10
What I would like to create
data1
id x1 x2
1 A 1 11
2 A 2 12
3 B 3 13
4 B 4 14
5 B 5 15
6 C 6 16
data2
id x1 x3
1 A 1 4
2 A 2 6
3 B 3 7
4 B 4 9
5 B 5 8
6 C 6 10
CodePudding user response:
Try this,
mylist <- list()
for(i in seq_along(df[,3:ncol(df)])){
mylist[[i]] <- df[,c(1,2,(i 2))]
}
And to extract each df,
names(mylist) = paste0('data',3:(length(mylist) 2))
list2env(mylist, .GlobalEnv)