My goal is to split a df such that row 1-21 is one data frame, and row 22-39 make up another data frame. I can't find how to do that anywhere.Thanks!
Asked
Active
Viewed 81 times
-2
-
df1 <- df[1:21,] and df2 <- df[22:39,] – smandape Apr 04 '20 at 19:00
-
df1 = df[1:21,] for the first one. please post a reproducible example. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – Diego Apr 04 '20 at 19:00
2 Answers
0
You can use split
using data.table
. Assuming you want a more general version of your problem (where you bundle together every 20 observations)
library(data.table)
setDT(df)
split(df, by = floor(1:nrow(df)/20))

linog
- 5,786
- 3
- 14
- 28
0
You can split data frames by selecting just the rows or the columns that you need and associating them to a new object:
new_df1 <- df[1:21, ]
new_df2 <- df[22:39, ]

Greg
- 3,570
- 5
- 18
- 31