首先,我已经成功地将我的数据从长格式转换为宽格式。数据如下。
+======+==========+======+======+
| Name | Date | Val1 | Val2 |
+======+==========+======+======+
| A | 1/1/2018 | 1 | 2 |
+------+----------+------+------+
| B | 1/1/2018 | 2 | 3 |
+------+----------+------+------+
| C | 1/1/2018 | 3 | 4 |
+------+----------+------+------+
| D | 1/4/2018 | 4 | 5 |
+------+----------+------+------+
| A | 1/4/2018 | 5 | 6 |
+------+----------+------+------+
| B | 1/4/2018 | 6 | 7 |
+------+----------+------+------+
| C | 1/4/2018 | 7 | 8 |
+------+----------+------+------+
要将上表从长格式转换为宽格式,我使用了以下代码行:
test_wide <- reshape(test_data, idvar = 'Name', timevar = 'Date', direction = "wide" )
上述代码的结果如下:
+======+===============+===============+===============+===============+
| Name | Val1.1/1/2018 | Val2.1/1/2018 | Val1.1/4/2018 | Val2.1/4/2018 |
+======+===============+===============+===============+===============+
| A | 1 | 2 | 5 | 6 |
+------+---------------+---------------+---------------+---------------+
| B | 2 | 3 | 6 | 7 |
+------+---------------+---------------+---------------+---------------+
| C | 3 | 4 | 7 | 8 |
+------+---------------+---------------+---------------+---------------+
| D | NA | NA | 4 | 5 |
+------+---------------+---------------+---------------+---------------+
我面临的问题是我需要 R 考虑Date
日期格式的列。日期列的范围从1/1/2018
到 ,因为1/4/2018
日期中没有值1/2/2018
,1/3/2018
我不会看到任何列为Val1.1/2/2018
、Val2.1/3/2018
和。Val3.1/2/2018
Val3.1/3/2018
我想转换成宽格式,以便我可以获得日期1/2/2018
和列1/3/2018
,即使这些列只包含 NULLS。
这样做的原因是我需要将数据用作时间序列。
编辑:
复制和粘贴的初始数据:
Name Date Val1 Val2
A 1/1/2018 1 2
B 1/1/2018 2 3
C 1/1/2018 3 4
D 1/4/2018 4 5
A 1/4/2018 5 6
B 1/4/2018 6 7
C 1/4/2018 7 8
", header=TRUE)
转换后的数据复制和粘贴:
Name,Val1.1/1/2018,Val2.1/1/2018,Val1.1/4/2018,Val2.1/4/2018
A,1,2,5,6
B,2,3,6,7
C,3,4,7,8
D,NA,NA,4,5
dput(test_data) 结果:
structure(list(Name = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L), .Label = c("A",
"B ", "C", "D"), class = "factor"), Date = structure(c(1L, 1L,
1L, 2L, 2L, 2L, 2L), .Label = c("1/1/2018", "1/4/2018"), class = "factor"),
Val1 = 1:7, Val2 = 2:8), class = "data.frame", row.names = c(NA,
-7L))