多个变量(以列为单位),多年(以列为单位)重新整形为R中的flatfile

时间:2018-04-30 09:28:56

标签: r tidyr reshape2

我的数据采用以下格式,包含变量,数据按年份,其中A,B,C,D是行ID' s。

        Variable 1     blank column       Variable 2
  2008 2009 2010 2011                2008 2009 2010 2011
A   1   5    9    13                   5   10   15   20
B   2   6    10   14                  25   30   35   40
C   3   7    11   15                  45   50   55   60
D   4   8    12   16                  65   70   75   80

我想以这种格式得到它:

  Variable   Year  Data
A Variable1  2008  1
A Variable1  2009  5
.....
.....
D Variable2  2010  75
D Variable2  2011  80

我想过从库(tidyr)中使用聚集,但我无法弄清楚要做到这一点。非常感谢您的帮助。对不起,没有可重现的例子。

structure(list(X1 = c(NA, "A", "B", "C", "D"), Variable1 = c(2008, 
1, 2, 3, 4), X3 = c(2009, 5, 6, 7, 8), X4 = c(2010, 9, 10, 11, 
12), X5 = c(2011, 13, 14, 15, 16), Variable1 = c(2008, 5, 25, 
45, 65), X7 = c(2009, 10, 30, 50, 70), X8 = c(2010, 15, 35, 55, 
75), X9 = c(2011, 20, 40, 60, 80)), .Names = c("X1", "Variable1", 
"X3", "X4", "X5", "Variable1", "X7", "X8", "X9"), row.names = c(NA, 
5L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

希望这有帮助!

library(tidyverse)

names(df) <- c("row_name", 
               paste(c(t(replicate(4, names(df)[1 + seq(1, length.out=floor(length(names(df))/4), by=4)]))),
                     df[1,-1], 
                     sep="_"))

df[-1,] %>%
  gather(Variable_Year, Data, -row_name) %>%
  separate(Variable_Year, into=c("Variable", "Year"), sep="_") %>%
  arrange(row_name, Variable, Year)

请注意,您不能将非唯一值作为&#34;行名称&#34;对于数据框,您可能需要考虑另一种处理row_name列以下的方法。

输出为:

   row_name  Variable Year Data
1         A Variable1 2008    1
2         A Variable1 2009    5
...
31        D Variable2 2010   75
32        D Variable2 2011   80

示例数据:

df -> structure(list(row_name = c(NA, "A", "B", "C", "D"), Variable1_2008 = c(2008, 
1, 2, 3, 4), Variable1_2009 = c(2009, 5, 6, 7, 8), Variable1_2010 = c(2010, 
9, 10, 11, 12), Variable1_2011 = c(2011, 13, 14, 15, 16), Variable2_2008 = c(2008, 
5, 25, 45, 65), Variable2_2009 = c(2009, 10, 30, 50, 70), Variable2_2010 = c(2010, 
15, 35, 55, 75), Variable2_2011 = c(2011, 20, 40, 60, 80)), .Names = c("row_name", 
"Variable1_2008", "Variable1_2009", "Variable1_2010", "Variable1_2011", 
"Variable2_2008", "Variable2_2009", "Variable2_2010", "Variable2_2011"
), row.names = c(NA, 5L), class = "data.frame")