将长格式数据转换为R

时间:2020-04-18 21:42:45

标签: r arrays reshape plyr

我有一些看起来像这样的数据:

> ExampleData
# A tibble: 14,833 x 4
   CountryCode  Flow      FuelType     Value
   <fct>        <fct>     <fct>        <dbl>
 1 ALB          road      coa            0   
 2 ALB          services  coa            3.3
 3 ALB          manufact  coa          113.1  
 4 ALB          mining    coa            0  
 5 ALB          road      nga            0   
 6 ALB          services  nga            2.4
[...]

从代表国家,流量和燃料类型的每种组合的角度来看,数据是完整的。

我想将其转换为三维数组,其中国家表示一个维度,流动表示另一个维度,燃料类型表示第三个维度。因此,我可以引用数据X [a,b,c],其中b和c是我的因素CountryCode,Flow和FuelType的相应整数值。

所以我要寻找的是“宽”数据的多维形式。

1 个答案:

答案 0 :(得分:1)

一个选项是xtabs中的base R

out <- xtabs(Value ~ CountryCode + Flow + FuelType, data = ExampleData)
out
#, , FuelType = coa

#           Flow
#CountryCode manufact mining  road services
#        ALB    113.1    0.0   0.0      3.3

#, , FuelType = nga

#           Flow
#CountryCode manufact mining  road services
#        ALB      0.0    0.0   0.0      2.4

我们可以使用位置索引或键提取单个元素

out["ALB", "manufact", "coa"]
#[1] 113.1

或与tapply

tapply(ExampleData[['Value']], ExampleData[-4], FUN = I)

数据

ExampleData <- structure(list(CountryCode = c("ALB", "ALB", "ALB", "ALB", "ALB", 
"ALB"), Flow = c("road", "services", "manufact", "mining", "road", 
"services"), FuelType = c("coa", "coa", "coa", "coa", "nga", 
"nga"), Value = c(0, 3.3, 113.1, 0, 0, 2.4)), 
class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))