在 R 中重新排列数据框

标签 r

我有不同变量的年度国家数据,位于 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/

相关文章:

r - 一个 ggplot2 图中的 PCA 图和方差比例表 - R

r - 拆分、应用和合并 2 列数据

r - dplyr: case_when 涉及很多案例

r - 以编程方式重命名dplyr中的列

r - 如何获得 R 时间序列中下一行和上一行之间的差异?

r - 在 R 中查找给定数据的 "row wise" "Mode"

r - 来自 R "image"函数的意外转置翻转输出

r - 如何在 dplyr 中对 r 中包含 POSIXct 数据类型的数据帧进行分组

string - 如何将字符串转换为 R 中的变量名

R::ggplot2::geom_points:如何用饼图交换点?