为什么键不能分布在多列中?

时间:2019-03-31 23:23:21

标签: r spread

我正在尝试将“类型”列的键值分布在多个列中。数据帧(dat_weighted)如下:

  AreaName             Type index_disagg_standard index Category
1 Barking and Dagenham MRF  0.3329420             0.518 Average
2 Barking and Dagenham SDH  0.5286273             0.518 Average
3 Barking and Dagenham HO   0.7096024             0.518 Average
4 Barnet               MRF  0.8639524             0.750 Average
5 Barnet               SDH  0.3641302             0.750 Average
6 Barnet               HO   0.8493885             0.750 Average
7 Barnsley             MRF  0.5628280             0.610 Average
8 Barnsley             SDH  0.801927              0.610 Average
9 Barnsley             HO   0.4823344             0.610 Average

我尝试过的代码如下:

dat_index <- dat_weighted %>%
  distinct(AreaName, .keep_all = TRUE) %>%
  arrange(index) %>%
  spread(key = Type, value = index_disagg_standard)

但这只为我提供了这一点:

  AreaName             MRF         index
1 Barking and Dagenham 0.33294203  0.518
2 Barnet               0.86395241  0.750
3 Barnsley             0.56282804  0.610

并且我期望这样的事情:

  AreaName             MRF         SDH       OH        index
1 Barking and Dagenham 0.33294203  0.5286273 0.7096024 0.518
2 Barnet               0.86395241  0.3641302 0.8493885 0.750    
3 Barnsley             0.56282804  0.801927  0.4823344 0.610

2 个答案:

答案 0 :(得分:0)

要获得您的预期输出,我们可以

library(dplyr)
library(tidyr)

df %>%
  select(-Category) %>%
  gather(key, value, -c(AreaName, Type, index)) %>%
  spread(Type, value) %>%
  select(-key)

#             AreaName index        HO       MRF       SDH
#1 Barking_and_Dagenham 0.518 0.7096024 0.3329420 0.5286273
#2               Barnet 0.750 0.8493885 0.8639524 0.3641302
#3             Barnsley 0.610 0.4823344 0.5628280 0.8019270

答案 1 :(得分:0)

我们只需要删除“类别”列,它就可以正常工作

library(tidyverse)
dat_weighted  %>% 
     select(-Category) %>% 
     spread(Type, index_disagg_standard)
#             AreaName index        HO       MRF       SDH
#1 Barking and Dagenham 0.518 0.7096024 0.3329420 0.5286273
#2               Barnet 0.750 0.8493885 0.8639524 0.3641302
#3             Barnsley 0.610 0.4823344 0.5628280 0.8019270

数据

dat_weighted <- structure(list(AreaName = c("Barking and Dagenham", 
     "Barking and Dagenham", 
"Barking and Dagenham", "Barnet", "Barnet", "Barnet", "Barnsley", 
"Barnsley", "Barnsley"), Type = c("MRF", "SDH", "HO", "MRF", 
"SDH", "HO", "MRF", "SDH", "HO"), index_disagg_standard = c(0.332942, 
0.5286273, 0.7096024, 0.8639524, 0.3641302, 0.8493885, 0.562828, 
0.801927, 0.4823344), index = c(0.518, 0.518, 0.518, 0.75, 0.75, 
0.75, 0.61, 0.61, 0.61), Category = c("Average", "Average", "Average", 
"Average", "Average", "Average", "Average", "Average", "Average"
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9"))