2017-06-07 43 views
0

如何將單個柱從T1熔化到T2。如何熔化R中的單個柱

T1 \t \t \t 
 
Mar-17 \t \t T2 \t 
 
328 \t \t Mar-17 \t 328 
 
29.57 \t \t Mar-17 \t 29.57 
 
38.483 \t \t Mar-17 \t 38.483 
 
31.26 \t \t Mar-17 \t 31.26 
 
37.3 \t \t Mar-17 \t 37.3 
 
60.026 \t \t Mar-17 \t 60.026 
 
47.059 \t \t Mar-17 \t 47.059 
 
40.215 \t \t Mar-17 \t 40.215 
 
51.79 \t \t Mar-17 \t 51.79 
 
45.284 \t \t Mar-17 \t 45.284 
 
41.271 \t \t Mar-17 \t 41.271 
 
42.411 \t \t Mar-17 \t 42.411
什麼ID,我應該使用從T3

T3 \t \t \t \t 
 
Mar-17 \t Apr-17 \t \t T4 \t 
 
328 \t 396 \t \t Mar-17 \t 328 
 
29.57 \t 216.57 \t \t Mar-17 \t 29.57 
 
38.483 \t 116.483 \t \t Mar-17 \t 38.483 
 
31.26 \t 168.26 \t \t Mar-17 \t 31.26 
 
37.3 \t 128.3 \t \t Mar-17 \t 37.3 
 
60.026 \t 82.026 \t \t Mar-17 \t 60.026 
 
47.059 \t 113.059 \t \t Mar-17 \t 47.059 
 
40.215 \t 165.215 \t \t Mar-17 \t 40.215 
 
51.79 \t 80.79 \t \t Mar-17 \t 51.79 
 
45.284 \t 185.284 \t \t Mar-17 \t 45.284 
 
41.271 \t 183.271 \t \t Mar-17 \t 41.271 
 
42.411 \t 204.411 \t \t Mar-17 \t 42.411 
 
\t \t \t Apr-17 \t 396 
 
\t \t \t Apr-17 \t 216.57 
 
\t \t \t Apr-17 \t 116.483 
 
\t \t \t Apr-17 \t 168.26 
 
\t \t \t Apr-17 \t 128.3 
 
\t \t \t Apr-17 \t 82.026 
 
\t \t \t Apr-17 \t 113.059 
 
\t \t \t Apr-17 \t 165.215 
 
\t \t \t Apr-17 \t 80.79 
 
\t \t \t Apr-17 \t 185.284 
 
\t \t \t Apr-17 \t 183.271 
 
\t \t \t Apr-17 \t 204.411

得到輸出T4當我使用堆棧

T2 <- stack(T1) 

這是給錯誤消息rep.int

錯誤(名稱(X),長度(X)):無效的 '次' 值

+2

Mar-17是列名還是第一行? – neilfws

回答

2

我能夠重現你的錯誤,當我沒有列名(但是有Mar-17Apr-17作爲第一個觀察),所以我假設這就是你的數據結構。如果Mar-17Apr-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") 
+0

感謝邁克,它工作得很好,你一直非常慷慨地重現數據。 –