【发布时间】:2020-09-27 13:52:15
【问题描述】:
抱歉,如果我重复发布,但我尝试了我在 stackoverflow 中看到的不同方法,但无法完全解决问题或理解我为什么会遇到它。
所以我有一个这样的数据集:
council_name <- c("Barking and Dagenham","Barking and Dagenham","Barking and Dagenham","Barking and Dagenham","Barking and Dagenham","Barking and Dagenham","Barking and Dagenham","Barking and Dagenham","Barnet","Barnet")
period <- c("1st 2006", "1st 2006", "1st 2006", "1st 2006", "2nd 2006", "2nd 2006", "2nd 2006", "2nd 2006", "1st 2006", "1st 2006")
category <- c ("glass", "fridges", "paper", "glass", "glass", "fridges", "paper", "glass", "glass", "fridges")
data <- c(333, 222, 100, 98, 450, 540, 33, 450, 560, 120)
category2 <- c ("collected", "collected", "collected", "no donors", "collected", "collected", "collected", "no donors", "collected", "collected")
df <- data.frame (council_name, period, category, category2, data)
而我想要的是这样的:
council_name <- c("Barking and Dagenham","Barking and Dagenham","Barnet")
period <- c("1st 2006", "2nd 2006", "1st 2006")
glass <- c(333, 450, 560)
fridges <- c(222,540,120)
paper <- c(100, 33, NA)
no.donors <- c(98, 450, NA)
df.desired <- data.frame (council_name, period, glass, fridges, paper, no.donors)
我一直在尝试使用数据透视函数做多种事情,但事实上我需要从 category1 和 category2 中提取列名,但用 df 中同一列的值填充单元格,这给了我各种各样的问题。
非常感谢您的帮助!
【问题讨论】:
标签: r dplyr data.table pivot tidyr