我有不同变量的年度国家数据,位于 CSV 文件中。数据有很多国家和地区。下面是前五行数据的简单示例。
region LAM LAM LAM LAM LAM LAM
country Brazil Brazil Brazil Peru Peru Peru
variable FC FP FCO FC FP FCO
1850 10 20 30 15 25 16
1851 10 20 30 15 25 16
当我在 R 中将 CSV 文件作为数据框读入后,我想按如下方式对其进行转换,以使其易于使用。
region country year variable amount
LAM Brazil 1850 FC 10
LAM Brazil 1851 FC 10
LAM Brazil 1850 FP 20
LAM Brazil 1850 FP 20
LAM Brazil 1850 FCO 30
LAM Brazil 1850 FCO 30
LAM Peru 1850 FC 15
有人知道最简单的方法吗?
最佳答案
library(data.table)
(df <- fread("
region LAM LAM LAM LAM LAM LAM
country Brazil Brazil Brazil Peru Peru Peru
variable FC FP FCO FC FP FCO
1850 10 20 30 15 25 16
1851 10 20 30 15 25 16", header = FALSE))
df <- setnames(transpose(df), df[, V1]) # transpose df and set col names, where the first column of df is the var names.
df <- df[-1, ] # then our df is df without the first row
df_long <- melt(df, id.vars = c("region", "country", "variable"), variable.name = "year", value.name = "amount")
df_long
region country variable year amount
1 LAM Brazil FC 1850 10
2 LAM Brazil FP 1850 20
3 LAM Brazil FCO 1850 30
4 LAM Peru FC 1850 15
5 LAM Peru FP 1850 25
6 LAM Peru FCO 1850 16
7 LAM Brazil FC 1851 10
8 LAM Brazil FP 1851 20
9 LAM Brazil FCO 1851 30
10 LAM Peru FC 1851 15
11 LAM Peru FP 1851 25
12 LAM Peru FCO 1851 16
关于在 R 中重新排列数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62428760/