我的表有三行,我想按此示例/解决方案中的两行对我的表进行排序: How to reshape data from long to wide format
但是我在表中使用了多个相似的行,只是另一个值。
我试图在此线程之外使用重塑功能和扩散功能(与上面相同): How to reshape data from long to wide format
df1 <- data.frame("Company" = c(1, 1, 1, 1, 2, 2, 3, 3, 3, 4), "Job" = c(1,4,5,1,5,5,1,2,3,4), "Value" = c(2.3,3,4.1,5.4,7.3,4.2,3.1,4,5,1.1))
df1
Company Job Value
1 1 1 2.3
2 1 4 3.0
3 1 5 4.1
4 1 1 5.4
5 2 5 7.3
6 2 5 4.2
7 3 1 3.1
8 3 2 4.0
9 3 3 5.0
10 4 4 1.1
我希望我的桌子看起来像这样: 例如,我要汇总公司1和工作1的值。
df2 <- data.frame("Company" = c(1, 2, 3, 4), "Job1"=c(7.7, 0, 3.1, 0), "Job2"=c(0, 0, 4, 0), "Job3"=c(0, 0, 5, 0), "Job4"=c(3, 0, 0, 1.1), "Job5"=c(4.1, 0, 0, 0))
df2
Company Job1 Job2 Job3 Job4 Job5
1 1 7.7 0 0 3.0 4.1
2 2 0.0 0 0 0.0 0.0
3 3 3.1 4 5 0.0 0.0
4 4 0.0 0 0 1.1 0.0
错误消息是:
reshape(df, idvar = "Company", timevar = "Job", direction = "wide")
1: In reshapeWide(data, idvar = idvar, timevar = timevar, varying = varying, :
multiple rows match for Job=1: first taken
2: In reshapeWide(data, idvar = idvar, timevar = timevar, varying = varying, :
multiple rows match for Job=5: first taken
spread(df, key = Job, value = Value)
Each row of output must be identified by a unique combination of keys.
Keys are shared for 4 rows:
* 1, 4
* 5, 6
答案 0 :(得分:0)
您可以尝试tidyverse
library(tidyverse)
df1 %>%
group_by(Company, Job) %>%
summarise(Value=sum(Value)) %>%
mutate(Job = paste0("Job", Job)) %>%
spread(Job, Value, fill = 0)
# A tibble: 4 x 6
# Groups: Company [4]
Company Job1 Job2 Job3 Job4 Job5
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 7.7 0 0 3 4.1
2 2 0 0 0 0 11.5
3 3 3.1 4 5 0 0
4 4 0 0 0 1.1 0