在多列中展开一列

Spread one column in multiple columns

我有一列 "m",其中包含与一个主题 (ID) 关联的多个值。我需要将此列中的值分布在 5 个不同的列中以获得我在下面提供的第二个 table。我还需要将名称关联到这些列。

f <- read.table(header = TRUE, text = "
    Scale ID            m
1       1  1    0.4089795
2       1  1  0.001041055
3       1  1    0.1843616
4       1  1   0.03398921
5       1  1        FALSE
6       3  1    0.1179424
7       3  1    0.3569155
8       3  1    0.2006204
9       3  1   0.04024855
10      3  1        FALSE
")  

输出应该是这样的

  ID Scale         x           y         z          a     b
1  1     1 0.4089795 0.001041055 0.1843616 0.03398921 FALSE
2  1     3 0.1179424 0.356915500 0.2006204 0.04024855 FALSE

感谢您的帮助!

df <- read.table(header = TRUE, text = "
Scale ID            m
1       1  1    0.4089795
2       1  1  0.001041055
3       1  1    0.1843616
4       1  1   0.03398921
5       1  1        FALSE
6       3  1    0.1179424
7       3  1    0.3569155
8       3  1    0.2006204
9       3  1   0.04024855
10      3  1        FALSE
") 

library(tidyverse)

df %>%
  group_by(Scale, ID) %>%                     # for each combination of Scale and ID
  mutate(names = c("x","y","z","a","b")) %>%  # add column names
  ungroup() %>%                               # forget the grouping
  spread(-Scale, -ID) %>%                     # reshape data
  select(Scale, ID, x, y, z, a, b)            # order columns

# # A tibble: 2 x 7
#   Scale    ID x         y           z         a          b    
#   <int> <int> <fct>     <fct>       <fct>     <fct>      <fct>
# 1     1     1 0.4089795 0.001041055 0.1843616 0.03398921 FALSE
# 2     3     1 0.1179424 0.3569155   0.2006204 0.04024855 FALSE