我有使用代碼轉換列成行r中
test <- data.frame(dis = c(10,20,30,40),dur=c(30,40,60,90),method=c("car","car","Bicycle","Bicycle"),to_lon=c(-1.980,-1.5678,-1.324,-1.456),to_lat=c(55.3009,55.3416,55.1123,55.2234),from_lon=c(-1.4565,-1.3424,-1.4566,-1.1111),from_lat=c(76.8888,65.8999,76.9088,25.3344))
dis dur method to_lon to_lat from_lon from_lat
1 10 30 car -1.9800 55.3009 -1.4565 76.8888
2 20 40 car -1.5678 55.3416 -1.3424 65.8999
3 30 60 Bicycle -1.3240 55.1123 -1.4566 76.9088
4 40 90 Bicycle -1.4560 55.2234 -1.1111 25.3344
形成的下面數據欲這個數據幀轉換,使得它具有一排用於to_lat和to_lon和下一行中它具有from_lat和from_lon。其餘細節不需要更改,可以複製。期望的結果應該如下
dis dur method longitude latitude
from 10 30 car -1.98 55.3009
to 10 30 car -1.4565 76.8888
from 20 40 car -1.5678 55.3416
to 20 40 car -1.3424 65.8999
from 30 60 Bicycle -1.324 55.1123
to 30 60 Bicycle -1.4566 76.9088
from 40 90 Bicycle -1.456 55.2234
to 40 90 Bicycle -1.1111 25.3344
任何幫助將不勝感激。
謝謝。
除了@ akrun的答案,看看這個頁面'reshape2'和'tidyr'解決方案(給你的標籤):http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/ – Laterow
我好沒有發現任何有用的功能來轉換上述在帖子中提到的數據。有任何想法嗎? – syebill