将一列分散在多列中

时间:2018-08-27 21:11:07

标签: r function dplyr spread

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

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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