使用Dplyr和Tidyverse创建一个具有替换子行的子行的表

时间:2017-09-21 03:12:24

标签: r dplyr tidyverse

library(dplyr)
library(forcats) 

使用下面的简单数据框和代码,我想创建一个包含总行数和子行数的表。例如,第一行是来自NEW列的“Region1”和来自TotNumber列的70,然后是“Town1”,“Town2”和“Town3”的三行,以及来自Number的相关数字列,和“Region2”和“Region3”相同。我附上了所需表格的图片...... enter image description here

我也在寻找使用dplyr和Tidyverse的解决方案。

Number<-c(10,30,30,10,56,30,40,50,33,10)
Town<-("Town1","Town2","Town3","Town4","Town5","Town6","Town7","Town8","Town9","Town10")

DF<-data_frame(Town,Number)


DF<-DF%>%mutate_at(vars(Town),funs(as.factor))

创建Region变量...

DF<-DF%>%mutate(NEW=fct_collapse(Town,
Region1=c("Town1","Town2","Town3"),
Region2=c("Town4","Town5","Town6"),
Region3=c("Town7","Town8","Town9","Town10")))%>%
group_by(NEW)%>%
summarise(TotNumber=sum(Number))

1 个答案:

答案 0 :(得分:0)

修改上一个管道并添加一些添加步骤:

library(dplyr)
library(forcats) 
DF%>%mutate(NEW=fct_collapse(Town,
                                 Region1=c("Town1","Town2","Town3"),
                                 Region2=c("Town4","Town5","Town6"),
                                 Region3=c("Town7","Town8","Town9","Town10")),
            NEW = as.character(NEW)) %>%
  group_by(NEW) %>%
  mutate(TotNumber=sum(Number))  %>%
  ungroup() %>%
  split(.$NEW) %>%
  lapply(function(x) rbind(setNames(x[1,3:4], names(x)[1:2]), x[1:2])) %>%
  do.call(rbind, .) 

<强>结果:

# A tibble: 13 × 2
      Town Number
*    <chr>  <dbl>
1  Region1     70
2    Town1     10
3    Town2     30
4    Town3     30
5  Region2     96
6    Town4     10
7    Town5     56
8    Town6     30
9  Region3    133
10   Town7     40
11   Town8     50
12   Town9     33
13  Town10     10

数据:

Number<-c(10,30,30,10,56,30,40,50,33,10)
Town<-c("Town1","Town2","Town3","Town4","Town5","Town6","Town7","Town8","Town9","Town10")

DF<-data_frame(Town,Number) %>%
  mutate_at(vars(Town),funs(as.factor))