【发布时间】:2020-06-12 11:49:15
【问题描述】:
我目前正在尝试研究如何旋转我的数据框(下面的小 dput)。目前一栏包含有关国家、ISO 代码、行业和部门的信息。我需要将此信息分散到 4 列中,并带有一个对应的值列。我之前使用过 melt 和 pivot_long 函数,但不确定如何生成 4 个新列以及 value 列。
DI_SMALL <- structure(list(V1 = structure(c(NA, NA, NA, NA, 1L, 1L, 1L, 1L
), .Label = "Energy Usage (TJ)", class = "factor"), V2 = structure(c(NA,
NA, NA, NA, 2L, 1L, 4L, 3L), .Label = c("Coal", "Natural Gas",
"Nuclear Electricity", "Petroleum"), class = "factor"), V3 = structure(c(5L,
4L, 7L, 6L, 3L, 2L, 1L, 1L), .Label = c("0", "1.29327085460648e-05",
"1.59504500372979e-05", "AFG", "Afghanistan", "Agriculture",
"Industries"), class = "factor"), V4 = structure(c(5L, 4L, 7L,
6L, 3L, 2L, 1L, 1L), .Label = c("0", "6.53466630114587e-06",
"8.05944706428482e-06", "AFG", "Afghanistan", "Fishing", "Industries"
), class = "factor"), V5 = structure(c(5L, 4L, 6L, 7L, 3L, 2L,
1L, 1L), .Label = c("0", "1.88562621206664e-05", "2.32557880912235e-05",
"AFG", "Afghanistan", "Industries", "Mining and Quarrying"), class = "factor"),
V6 = structure(c(5L, 4L, 7L, 6L, 3L, 2L, 1L, 1L), .Label = c("0",
"2.00284547443433e-05", "2.47018365704401e-05", "AFG", "Afghanistan",
"Food & Beverages", "Industries"), class = "factor")), row.names = c("V1",
"V2", "V3", "V4", "X", "X.1", "X.2", "X.3"), class = "data.frame")
理想情况下,输出将包含 7 列。现有的先到列,Country、ISO、Industry 和 Sector,然后是 Value。像这样:
Output <- structure(list(NA. = structure(c(1L, 1L, 1L, 1L), .Label = "Energy Usage (TJ)", class = "factor"),
NA..1 = structure(c(2L, 1L, 4L, 3L), .Label = c("Coal ",
"Natural Gas", "Nuclear Electricity", "Petroleum"), class = "factor"),
Country = structure(c(1L, 1L, 1L, 1L), .Label = "Afghanistan", class = "factor"),
ISO = structure(c(1L, 1L, 1L, 1L), .Label = "AFG", class = "factor"),
Industry = structure(c(1L, 1L, 1L, 1L), .Label = "Industries", class = "factor"),
Sector = structure(c(1L, 1L, 1L, 1L), .Label = "Agriculture", class = "factor"),
Value = c(1.595045004, 1.2932706, 0, 0)), class = "data.frame", row.names = c(NA,
-4L))
希望这是有道理的,任何想法都将不胜感激!
谢谢
【问题讨论】:
-
在
outputvalue是如何计算的? -
您好,应该是DI_Small矩阵中的对应值,但都在一列中
-
以更易读的风格格式化您的代码会非常有帮助。例如。为每个参数创建一个新行。
标签: r pivot tidyverse tidyr reshape2