通过两列组合重组列

时间:2019-06-19 10:33:16

标签: r dplyr tidyr database-management

我目前正在学习提迪尔和dplyr。在以下问题中,我不确定如何正确面对:

想象以下数据集:

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

试图获取:

SHOULDCHANGESTATUS

我在这方面具有基本的R知识,并使用默认R功能尝试了几种选择,但没有结果

1 个答案:

答案 0 :(得分: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