使用行范围创建不同的数据框

时间:2018-10-25 13:47:34

标签: r

从类似此处的示例的数据集中:

df = data.frame(main_name = c("google","yahoo","google","amazon","yahoo","google"),
                volume = c(32,43,412,45,12,54))

我们创建一个范围list

library(tidyverse)

df%>%
   arrange(main_name)%>%
   mutate(row=row_number())%>%
   group_by(main_name)%>%
   summarise(start=first(row),
             end=last(row))%>%
   mutate(res=glue::glue("[{start}:{end}]"))
# A tibble: 3 x 4
  main_name start   end res  
  <fct>     <int> <int> <chr>
1 amazon        1     1 [1:1]
2 google        2     4 [2:4]
3 yahoo         5     6 [5:6]

如果我们使用范围列表,那么如何为每个组创建不同的数据框。

示例: df_amazon

main_name volume
amazon     45

df_google

main_name volume
google     32
google    412
google     54

df_yahoo

main_name volume
yahoo     43
yahoo     12

1 个答案:

答案 0 :(得分:1)

使用plyr库:

library(plyr)
dfs <- dlply(df, .(main_name), data.frame)
> dfs[[1]]
  main_name volume
1    amazon     45
> dfs[[2]]
  main_name volume
1    google     32
2    google    412
3    google     54
> dfs[[3]]
  main_name volume
1     yahoo     43
2     yahoo     12