【问题标题】:Panel Data from Long to wide reshape or cast面板数据从长到宽重塑或铸造
【发布时间】:2013-05-09 03:43:29
【问题描述】:

您好,我有面板数据,想将我的指标名称列从长格式转换为宽格式。目前所有的列都是长格式,Year(1960-2011),Country Name(世界上所有国家),Indicator name(因不同的指标而异)和Value(对应年份、指标名称和国家名称的个体值) .我该怎么做才能有人帮忙。我希望各种指标采用宽格式,并在其下方以及其他列年份和国家名称上具有相应的值。请帮忙

Indicator.Name  Year    Country
GDP             1960    USA
GDP             1960    UK




Country Name    Year    GDP    PPP    HHH
USA             1960    7       9      10
Uk              1960    9       10     NA
World           1960    7       5      3
Africa          1960    3       7      NA

【问题讨论】:

  • 请缩进您的代码。即在每行代码的开头放四个空格。
  • 我会支持@Frank 所说的话……您的代码几乎不可读。但是,请查看 reshape2 包。具体来说,dcast。你会想要像dcast(yourdata, Country + Year ~ Indicator.Name, value.var='Value') 这样的东西。
  • 这是我第一次访问这个网站,感谢 Hugo 的友好态度,并不是每个人都达到你的能力水平。无论如何,贾斯汀,我不能感谢你,它就像一个魅力。弗兰克感谢您的耐心等待。
  • @JimmyT 不让你在那里回答。他应该做出这样的回答
  • 我已经尽力清理了...

标签: r casting reshape reshape2


【解决方案1】:

尝试使用 reshape2 中的 dcast,如下所示:

library(reshape2)
indicator <- c('PPP','PPP','GDP','GDP')
country.name <- c('USA','UK','USA','UK')
year <- c(1960,1961,1960,1961)
value <- c(5,7,8,9)
d <- data.frame(indicator, country.name, year, value)
d1 <- dcast(d, country.name + year ~ indicator)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2012-12-01
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多