使用 gather() 从宽到长格式重塑
library(tidyr)
## example data
df <- read.table(text =" numbers firstName secondName
1 1 1.5862639 0.4087477
2 2 0.1499581 0.9963923
3 3 0.4117353 0.3740009
4 4 -0.4926862 0.4437916", header = T)
df
# numbers firstName secondName
# 1 1 1.5862639 0.4087477
# 2 2 0.1499581 0.9963923
# 3 3 0.4117353 0.3740009
# 4 4 -0.4926862 0.4437916
我们可以使用’数字’作为关键列来收集列:
gather(data = df,
key = numbers,
value = myValue)
# numbers numbers myValue
# 1 1 firstName 1.5862639
# 2 2 firstName 0.1499581
# 3 3 firstName 0.4117353
# 4 4 firstName -0.4926862
# 5 1 secondName 0.4087477
# 6 2 secondName 0.9963923
# 7 3 secondName 0.3740009
# 8 4 secondName 0.4437916