0

我目前正在学习 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 函数尝试了几个选项,但没有结果

4

1 回答 1

0
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
于 2019-06-19T10:38:06.560 回答