通过两列组合重组列

Reorganizing columns by two column combination

我目前正在学习 tidyr 和 dplyr。去了下面的问题我不知道如何正确面对:

对以下数据集进行成像:

Factor 1    Factor 2        Year    value
A            green          2016     1.2
A            green          2017     1.9
B            yellow         2017      3
B            yellow         2018      8

试图获得:

Factor 1    Factor 2     Year.2016   Year.2017  Year.2018
A            green          1.2          1.9        NA           
B            yellow         NA            3          8

我在这方面有基本的 R 知识,并使用默认 R 函数尝试了几个选项但没有结果

library(dplyr)
library(tidyr)

# example data
dt = read.table(text = "
Factor1    Factor2    Year    value
A            green       2016    1.2
A            green       2017    1.9
B            yellow      2017    3
B            yellow      2018    8
", header=T)

dt %>% spread(Year, value, sep=".")

#   Factor1 Factor2 Year.2016 Year.2017 Year.2018
# 1       A   green       1.2       1.9        NA
# 2       B  yellow        NA       3.0         8

如果您有两个或更多 value 列,您可以使用这种涉及更多重塑的方法:

library(dplyr)
library(tidyr)

# example data
dt = read.table(text = "
Factor1    Factor2    Year    value  value2
A            green       2016    1.2   5
A            green       2017    1.9   5
B            yellow      2017    3     5
B            yellow      2018    8     5
", header=T)

dt %>% 
  gather(v, value, -Factor1, -Factor2, -Year) %>%
  unite(Year, Year, v) %>%
  spread(Year, value, sep = ".")

#   Factor1 Factor2 Year.2016_value Year.2016_value2 Year.2016_value3 Year.2017_value
# 1       A   green             1.2                5                9             1.9
# 2       B  yellow              NA               NA               NA             3.0
#   Year.2017_value2 Year.2017_value3 Year.2018_value Year.2018_value2 Year.2018_value3
# 1                5                9              NA               NA               NA
# 2                5                9               8                5                9