split dataframe by row number in R

后端 未结 2 1776
Happy的楠姐
Happy的楠姐 2021-01-05 06:05

This is probably really simple, but I can\'t find a solution:

df <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))

v <- c(3, 7)

i

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 06:38

    Another way:

    split(df, findInterval(1:nrow(df), v))
    

    For the alternative interpretation, you can use:

    split(df, cut(1:nrow(df), unique(c(1, v, nrow(df))), include.lowest=TRUE))
    

提交回复
热议问题