I have a dataframe
with 118
observations and has three columns in total. Now, I would like to split this dataframe into two
dataframes
with 59
observations each. I tried two different approaches but none of them returned what I wanted.
How can I do this using dplyr
in R
?
Sample dataframe:
Code Count_2020 Count_2021
A 1 2
B 2 4
C 3 6
D 4 8
E 5 10
F 6 12
Desired output:
DF1
Code Count_2020 Count_2021
A 1 2
B 2 4
C 3 6
DF2
Code Count_2020 Count_2021
D 4 8
E 5 10
F 6 12
1st Approach
Based on this answer
library(tidyverse)
df= df %>% group_split(Code)
Now this returns a list of 118
, so it's a list of 118
observations where each list
has 3
columns.
2nd Approach
Based on this answer
library(tidyverse)
df= df %>% sample_n(size = 59) %>%
split(f = as.factor(.$Code))
Now this returns a list of 59
.