如何在 R 中熔化单列
How to Melt single column in R
如何从T1到T2熔解单根柱。
T1
Mar-17 T2
328 Mar-17 328
29.57 Mar-17 29.57
38.483 Mar-17 38.483
31.26 Mar-17 31.26
37.3 Mar-17 37.3
60.026 Mar-17 60.026
47.059 Mar-17 47.059
40.215 Mar-17 40.215
51.79 Mar-17 51.79
45.284 Mar-17 45.284
41.271 Mar-17 41.271
42.411 Mar-17 42.411
以及我应该使用什么 ID 从 T3 获取 T4 中的输出
T3
Mar-17 Apr-17 T4
328 396 Mar-17 328
29.57 216.57 Mar-17 29.57
38.483 116.483 Mar-17 38.483
31.26 168.26 Mar-17 31.26
37.3 128.3 Mar-17 37.3
60.026 82.026 Mar-17 60.026
47.059 113.059 Mar-17 47.059
40.215 165.215 Mar-17 40.215
51.79 80.79 Mar-17 51.79
45.284 185.284 Mar-17 45.284
41.271 183.271 Mar-17 41.271
42.411 204.411 Mar-17 42.411
Apr-17 396
Apr-17 216.57
Apr-17 116.483
Apr-17 168.26
Apr-17 128.3
Apr-17 82.026
Apr-17 113.059
Apr-17 165.215
Apr-17 80.79
Apr-17 185.284
Apr-17 183.271
Apr-17 204.411
当我使用堆栈时
T2 <- stack(T1)
它给出了错误信息
Error in rep.int(names(x), lengths(x)) : invalid 'times' value
当我没有列名时(而是 Mar-17
和 Apr-17
作为第一个观察),我能够重现你的错误,所以我假设这就是你的数据是结构化的。如果 Mar-17
和 Apr-17
是您的列名,则 stack()
应该有效。
在这种情况下,您可以将变量名称设置为第一行中的值,然后对除第一行以外的所有内容使用 stack()
:
names(T1) <- T1[1,]
T2 <- stack(T1[-1, , drop = F])
head(T2)
# values ind
#1 328 Mar-17
#2 29.57 Mar-17
#3 38.483 Mar-17
#4 31.26 Mar-17
#5 37.3 Mar-17
#6 60.026 Mar-17
names(T3) <- T3[1,]
T4 <- stack(T3[-1,])
head(T4)
# values ind
#1 328 Mar-17
#2 29.57 Mar-17
#3 38.483 Mar-17
#4 31.26 Mar-17
#5 37.3 Mar-17
#6 60.026 Mar-17
数据:
T1 <- structure(list(c("Mar-17", "328", "29.57", "38.483", "31.26",
"37.3", "60.026", "47.059", "40.215", "51.79", "45.284", "41.271",
"42.411")), class = "data.frame", row.names = c(NA, -13L))
T3 <- structure(list(c("Mar-17", "328", "29.57", "38.483", "31.26",
"37.3", "60.026", "47.059", "40.215", "51.79", "45.284", "41.271",
"42.411"), c("Apr-17", "396", "216.57", "116.483", "168.26",
"128.3", "82.026", "113.059", "165.215", "80.79", "185.284",
"183.271", "204.411")), row.names = c(NA, -13L), class = "data.frame")
如何从T1到T2熔解单根柱。
T1
Mar-17 T2
328 Mar-17 328
29.57 Mar-17 29.57
38.483 Mar-17 38.483
31.26 Mar-17 31.26
37.3 Mar-17 37.3
60.026 Mar-17 60.026
47.059 Mar-17 47.059
40.215 Mar-17 40.215
51.79 Mar-17 51.79
45.284 Mar-17 45.284
41.271 Mar-17 41.271
42.411 Mar-17 42.411
T3
Mar-17 Apr-17 T4
328 396 Mar-17 328
29.57 216.57 Mar-17 29.57
38.483 116.483 Mar-17 38.483
31.26 168.26 Mar-17 31.26
37.3 128.3 Mar-17 37.3
60.026 82.026 Mar-17 60.026
47.059 113.059 Mar-17 47.059
40.215 165.215 Mar-17 40.215
51.79 80.79 Mar-17 51.79
45.284 185.284 Mar-17 45.284
41.271 183.271 Mar-17 41.271
42.411 204.411 Mar-17 42.411
Apr-17 396
Apr-17 216.57
Apr-17 116.483
Apr-17 168.26
Apr-17 128.3
Apr-17 82.026
Apr-17 113.059
Apr-17 165.215
Apr-17 80.79
Apr-17 185.284
Apr-17 183.271
Apr-17 204.411
当我使用堆栈时
T2 <- stack(T1)
它给出了错误信息
Error in rep.int(names(x), lengths(x)) : invalid 'times' value
当我没有列名时(而是 Mar-17
和 Apr-17
作为第一个观察),我能够重现你的错误,所以我假设这就是你的数据是结构化的。如果 Mar-17
和 Apr-17
是您的列名,则 stack()
应该有效。
在这种情况下,您可以将变量名称设置为第一行中的值,然后对除第一行以外的所有内容使用 stack()
:
names(T1) <- T1[1,]
T2 <- stack(T1[-1, , drop = F])
head(T2)
# values ind
#1 328 Mar-17
#2 29.57 Mar-17
#3 38.483 Mar-17
#4 31.26 Mar-17
#5 37.3 Mar-17
#6 60.026 Mar-17
names(T3) <- T3[1,]
T4 <- stack(T3[-1,])
head(T4)
# values ind
#1 328 Mar-17
#2 29.57 Mar-17
#3 38.483 Mar-17
#4 31.26 Mar-17
#5 37.3 Mar-17
#6 60.026 Mar-17
数据:
T1 <- structure(list(c("Mar-17", "328", "29.57", "38.483", "31.26",
"37.3", "60.026", "47.059", "40.215", "51.79", "45.284", "41.271",
"42.411")), class = "data.frame", row.names = c(NA, -13L))
T3 <- structure(list(c("Mar-17", "328", "29.57", "38.483", "31.26",
"37.3", "60.026", "47.059", "40.215", "51.79", "45.284", "41.271",
"42.411"), c("Apr-17", "396", "216.57", "116.483", "168.26",
"128.3", "82.026", "113.059", "165.215", "80.79", "185.284",
"183.271", "204.411")), row.names = c(NA, -13L), class = "data.frame")