r 使用 dplyr 'gather' 函数

标签 r dplyr tidyr

我有一个数据框,看起来像下面“输入”中显示的图片。

我尝试每行获取 1 个日期(请参见下面“所需输出”中的图片)。换句话说,我尝试为每一行做一种“转置”。

让我们规定组合 'LC' 和 'Prod' 是唯一键。

输入

enter image description here

所需输出:

enter image description here

信息:

在我的真实数据集中,数量字段(彩色区域)中存在一些缺失值。因此,我应该仍然能够计算缺失值。

我的尝试失败

我尝试了以下但它失败了......

library("dplyr")
outputTest <- tbl_df(inputTest) %>%
  gather(date, value, c(inputTest$LC, inputTest$Prod))

outputTest

来源:
inputTest <- structure(list(LC = structure(c(1L, 3L, 1L, 2L), .Label = c("berlin", 
                                                            "munchen", "stutgart"), class = "factor"), Prod = structure(c(1L, 
                                                                                                                          2L, 2L, 1L), .Label = c("(STORE1)400096", "STORE2_00154"), class = "factor"), 
               PROD_TYPE = structure(c(1L, 2L, 2L, 1L), .Label = c("STORE1", 
                                                                   "STORE2"), class = "factor"), X2015.6.29 = c(20.08, 8.91, 
                                                                                                                11.38, 15.42), X2015.7.6 = c(20.66, 8.49, 10.91, 15.57), 
               X2015.7.13 = c(19.02, 8.55, 10.89, 14.6), X2015.7.20 = c(18.6, 
                                                                        7.95, 10.58, 14.31)), .Names = c("LC", "Prod", "PROD_TYPE", 
                                                                                                         "2015.6.29", "2015.7.6", "2015.7.13", "2015.7.20"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                     -4L))

最佳答案

使用gather,您可以使用否定运算符“-”(减号)指定不想收集的列。在您的情况下,关键是日期,值是值,LC、Prod 和 PROD_TYPE 作为标识符。

output <- as.data.frame(inputTest) %>%
        tidyr::gather(key = Date, value = Value, -LC, -Prod, -PROD_TYPE)

这产生:
         LC           Prod PROD_TYPE      Date Value
1    berlin (STORE1)400096    STORE1 2015.6.29 20.08
2  stutgart   STORE2_00154    STORE2 2015.6.29  8.91
3    berlin   STORE2_00154    STORE2 2015.6.29 11.38
4   munchen (STORE1)400096    STORE1 2015.6.29 15.42
5    berlin (STORE1)400096    STORE1  2015.7.6 20.66
6  stutgart   STORE2_00154    STORE2  2015.7.6  8.49
7    berlin   STORE2_00154    STORE2  2015.7.6 10.91
8   munchen (STORE1)400096    STORE1  2015.7.6 15.57
9    berlin (STORE1)400096    STORE1 2015.7.13 19.02
10 stutgart   STORE2_00154    STORE2 2015.7.13  8.55
11   berlin   STORE2_00154    STORE2 2015.7.13 10.89
12  munchen (STORE1)400096    STORE1 2015.7.13 14.60
13   berlin (STORE1)400096    STORE1 2015.7.20 18.60
14 stutgart   STORE2_00154    STORE2 2015.7.20  7.95
15   berlin   STORE2_00154    STORE2 2015.7.20 10.58
16  munchen (STORE1)400096    STORE1 2015.7.20 14.31

关于r 使用 dplyr 'gather' 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48081635/

相关文章:

r - 将对数正态分布拟合到 R 中的截断数据

r - 是否可以根据变量标签选择列?

r - group_by() 和 summarise() 与所有组合(包括不存在的组合)

用新数据框替换数据框中的特定值

R - 在 dplyr 中使用 group_by() 和 mutate() 来应用返回组长度向量的函数

r - 如何为R中的每个不同列选择不同行中的值?

r - 从R中的日期时间提取日期的最快方法

r - R-仅对特定列进行平方

r - 将带逗号的字符串公式传递到R数据表

r - 在 purrr 中处理不同长度的向量