-4
我經常使用「wide format」格式的數據爲同一個變量提供多個列(請參閱here),我希望以長格式輸入數據(請參閱here)。你有任何簡單和安全的解決方案?如何輕鬆安全地在R中重塑我的數據?
我經常使用「wide format」格式的數據爲同一個變量提供多個列(請參閱here),我希望以長格式輸入數據(請參閱here)。你有任何簡單和安全的解決方案?如何輕鬆安全地在R中重塑我的數據?
當你的兩個例子不具有相同的數字,不知道這是否是你想要什麼:
data <- read.table(text='"id","name1","name2","x1","x2"
1,"Mr a","Mr c","0.575710014459387","-0.991747242336918"
2,"Mr b","Mr d","-0.126033858727122","0.854792650572792"', sep=',', header=TRUE)
reshape(data, direction = 'long', idvar = 'id', varying = list(c('x1', 'x2'), c('name1', 'name2')), times=c('a', 'b'))
或者與熔體功能UNPIVOT:
library(reshape2)
data <- read.table(text='"id","name1","name2","x1","x2","Mr a","Mr c","0.575710014459387","-0.991747242336918","Mr b","Mr d","-0.126033858727122","0.854792650572792"', sep=',', header=TRUE)
reshape(data, direction = 'long', idvar = 'id', varying = list(c('x1', 'x2'), c('name1', 'name2')), times=c('a', 'b'))
melt(data,c("id","name1","name2"),variable.name="x",value.name="value")
包reshape2。 – Roland
你是什麼意思「安全」? – A5C1D2H2I1M1N2O1R2T1