两种解决方案:
1)用data.table:
你可以使用melt函数:
library(data.table)
long <- melt(setDT(wide), id.vars = c("Code","Country"), variable.name = "year")
这使:
长>
国家代码年值
1: AFG阿富汗1950 20,249
2: ALB阿尔巴尼亚1950 8097
第3集:AFG阿富汗1951 21,352
4: ALB阿尔巴尼亚1951 8,986
5:阿富汗空军1952 22,532
第6集:ALB阿尔巴尼亚1952 10058
7:阿富汗空军1953年23,557
8:阿尔巴尼亚1953年11,123
9:阿富汗空军1954 24555
10:阿尔巴尼亚1954年12,246
其他表示法:
melt(setDT(wide), id.vars = 1:2, variable.name = "year")
melt(setDT(wide), measure.vars = 3:7, variable.name = "year")
melt(setDT(wide), measure.vars = as.character(1950:1954), variable.name = "year")
2)带着整洁:
使用pivot_longer ():
library(tidyr)
long <- wide %>%
pivot_longer(
cols = `1950`:`1954`,
names_to = "year",
values_to = "value"
)
注意:
names_to and values_to default to "name" and "value", respectively, so you could write this extra-succinctly as wide %>% pivot_longer(`1950`:`1954`).
The cols argument uses the highly flexible tidyselect DSL, so you can select the same columns using a negative selection (!c(Code, Country)), a selection helper(starts_with("19"); matches("^\\d{4}$")), numeric indices (3:7), and more.
tidyr::pivot_longer() is the successor to tidyr::gather() and reshape2::melt(), which are no longer under development.
改变值
数据的另一个问题是,值将被R读取为字符值(作为数字中的,的结果)。你可以用gsub和as来修复。数值,或者在重塑之前:
long$value <- as.numeric(gsub(",", "", long$value))
或者在重塑过程中,使用数据。表或tidyr:
# data.table
long <- melt(setDT(wide),
id.vars = c("Code","Country"),
variable.name = "year")[, value := as.numeric(gsub(",", "", value))]
# tidyr
long <- wide %>%
pivot_longer(
cols = `1950`:`1954`,
names_to = "year",
values_to = "value",
values_transform = ~ as.numeric(gsub(",", "", .x))
)
数据:
wide <- read.table(text="Code Country 1950 1951 1952 1953 1954
AFG Afghanistan 20,249 21,352 22,532 23,557 24,555
ALB Albania 8,097 8,986 10,058 11,123 12,246", header=TRUE, check.names=FALSE)