将数据帧从长格式转换为宽格式,在 R

时间:2021-02-01 15:37:07

标签: r data.table transformation reshape2 dcast

这可能已经有人回答了,但我找不到我想要的。

我有一个数据框,如:

Area <- c(1,1,1,1,2,2,2,2,3,3,3,3)
Scenario <- c(a,b,c,d,a,b,c,d,a,b,c,d)
Type <- c(EV, EV, EV, EV, EV, EV, EV, EV, EV, EV, EV, EV,)
Y2020 <- c(0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6)
Y2021 <- c(0.2,0.4,0.5,0.6,0.8,1.0,1.0,1.1,1.2,1.5,1.3,1.5)
y2022 <- c(0.3,0.6,0.2,0.7,0.5,0.6,0.7,0.8,0.9,1.1,1.3,1.6)

dt <- data.frame(Area,Scenario, Y2020, Y2021, Y2022)

所以看起来像:

Area  Scenario Type Y2020  Y2021  Y2022
 1       a      EV   0.5    0.2    0.3
 1       b      EV   0.6    0.4    0.6
 1       c      EV   0.7    0.5    0.8
 1       d      EV   0.8    0.6    0.7
 2       a      EV   0.9    0.8    0.5
 2       b      EV   1.0    1.0    0.6
 2       c      EV   1.1    1.0    0.7
 2       d      EV   1.2    1.1    0.8
 3       a      EV   1.3    1.2    0.9
 3       b      EV   1.4    1.5    1.1
 3       c      EV   1.5    1.3    1.3
 3       d      EV   1.6    1.5    1.6

我想通过像这样旋转场景列来获得宽格式:

Area Type Y2020_a  Y2021_a  Y2022_a  Y2020_b  Y2021_b ...
 1    EV    0.5      0.2      0.3      0.6      0.4
 2    EV    0.9      0.8      0.5      1.0      1.0
 3    EV    1.3      1.2      0.9      1.4      1.5

我尝试使用 dcast(dt, id ~ Scenario, value.var=names(dt)[4:6]) 正如@Arun from Reshape multiple values at once 所建议的那样,但它返回了“.subset2(x ,i,精确 = 精确):递归索引在级别 2 失败"

这是我的实际数据的精简版本,所以如果可以用更大的数据集复制它,那就太好了!

希望有人能帮忙!谢谢

2 个答案:

答案 0 :(得分:1)

具有函数 reshape() 的命题:

dt <- read.table(header = TRUE, text = "
Area  Scenario Type Y2020  Y2021  Y2022
 1       a      EV   0.5    0.2    0.3
 1       b      EV   0.6    0.4    0.6
 1       c      EV   0.7    0.5    0.8
 1       d      EV   0.8    0.6    0.7
 2       a      EV   0.9    0.8    0.5
 2       b      EV   1.0    1.0    0.6
 2       c      EV   1.1    1.0    0.7
 2       d      EV   1.2    1.1    0.8
 3       a      EV   1.3    1.2    0.9
 3       b      EV   1.4    1.5    1.1
 3       c      EV   1.5    1.3    1.3
 3       d      EV   1.6    1.5    1.6
")

reshape(data = dt,
        idvar = c("Area", "Type"),
        v.names = c("Y2020", "Y2021", "Y2022"),
        timevar = "Scenario",
        direction = "wide")
#>   Area Type Y2020.a Y2021.a Y2022.a Y2020.b Y2021.b Y2022.b Y2020.c Y2021.c
#> 1    1   EV     0.5     0.2     0.3     0.6     0.4     0.6     0.7     0.5
#> 5    2   EV     0.9     0.8     0.5     1.0     1.0     0.6     1.1     1.0
#> 9    3   EV     1.3     1.2     0.9     1.4     1.5     1.1     1.5     1.3
#>   Y2022.c Y2020.d Y2021.d Y2022.d
#> 1     0.8     0.8     0.6     0.7
#> 5     0.7     1.2     1.1     0.8
#> 9     1.3     1.6     1.5     1.6

# Created on 2021-02-01 by the reprex package (v0.3.0.9001)

问候,

答案 1 :(得分:0)

您需要先将数据转换为长格式,然后再转换为宽格式

    ${options}=                         Evaluate
    ...                                 sys.modules['selenium.webdriver'].ChromeOptions()
    ...                                 sys, selenium.webdriver
    Call Method                         ${options}
    ...                                 add_argument                            --user-agent\=User agent with spaces

    Create WebDriver                    Chrome                                  chrome_options=${options}

    Go To                               https://www.whatismybrowser.com/detect/what-is-my-user-agent

reprex package (v0.3.0) 于 2021 年 2 月 1 日创建