I have a larger dataframe from which I would like to split up based on 2 columns and a changing 3rd column.
Am on mobile so hard to give a reproducible example so I will try my best to describe.
I have a large Dataframe with 10 columns, the first 2 being ID and Year.
I would like to have smaller ones where the 3rd column will be each of the remaining 8.
So a total of 8 smaller dataframes
I have tried:
newDF1<-select(BIGdf, c("ID", "Year", "3rdVariable"))
newDF2<-select(BIGdf, c("ID", "Year", "4thVariable"))
And achieve the result but is there a way I don't have to write out each individual variable. Sorry for the poor formatting any help would be appreciated.
CodePudding user response:
It is usually bad practice to split up data which belongs together.
However, you can automatically create new R objects based on expressions using assign
:
library(tidyverse)
columns <-
iris %>%
colnames() %>%
setdiff("Species")
columns
#> [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
columns %>%
walk(~ {
data <- iris %>% head(2) %>% select_at(c("Species", .x))
assign(.x, data, envir = globalenv())
})
# access created objects
Sepal.Width
#> Species Sepal.Width
#> 1 setosa 3.5
#> 2 setosa 3.0
Sepal.Length
#> Species Sepal.Length
#> 1 setosa 5.1
#> 2 setosa 4.9
Created on 2021-11-25 by the reprex package (v2.0.1)
CodePudding user response:
Adding to danlooos answer, if you want a Base R solution you could just use a loop:
for (col in colnames(iris)[-1:-2]) {
assign(col, iris[, c("Sepal.Length", "Sepal.Width", col)], envir = globalenv())
}
Or do the same thing and store the resulting data frames in a list, which I personally find somewhat cleaner:
new_frames <- list()
for (col in colnames(iris)[-1:-2]) {
new_frames <- append(new_frames, list(iris[, c("Sepal.Length", "Sepal.Width", col)]))
}