重塑面板数据 [英] Reshaping panel data

查看:186
本文介绍了重塑面板数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重塑我的面板数据分析数据。我在互联网上搜索,只发现如何通过使用Stata获得所需的结果;但是我应该使用R和Excel。

I need to reshape my data for panel data analysis. I searched the internet and only found out how to get the desired results by using Stata; however I am supposed to use R and Excel.

我的初始和最终数据(所需结果)看起来非常类似于本例重新整形的第一页数据与Stata。
http://spot.colorado.edu/ 〜moonhawk / technical / C1912567120 / E220703361 / Media / reshape.pdf

My initial and final data(the desired result) looks very similar to the given in the first page of this example of reshaping data with Stata. http://spot.colorado.edu/~moonhawk/technical/C1912567120/E220703361/Media/reshape.pdf

可以使用R还是Excel?我尝试使用reshape2库中的融合函数,但是我得到

Is it attainable with R or just Excel? I tried using melt function from reshape2 library, yet I get

      CountryName  ProductName Unit Years    value
1     Belarus databaseHouseholds '000 Y1977 2942.702
2     Belarus databasePopulation '000 Y1977 9434.200
3     Belarus databaseUrbanPopulation '000 Y1977 4946.882
4     Belarus databaseRuralPopulation '000 Y1977 4487.318
5     Belarus originalHouseholds '000 Y1977       NA
6     Belarus originalUrban households '000 Y1977       NA
7     Poland ..............................................
...........................................................

当我想得到这样的东西:

when I would like to get something like this:

CountryName  Years  databaseHouseholds databasePopulation databaseUrbanPopulation databaseRuralPopulationUnit originalHousehold originalUrbanhouseholds
Belarus

在列数据库中,databasePopula应该是它们各自的值,所以我可以使用数据集进行面板建模。
非常感谢。

In the columns databaseHouseholds, databasePopulation,... should be their respective values, so I can use dataset for panel modeling. Thank you very much.

推荐答案

尝试:

 library(reshape2)
 dcast(dat, CountryName+Years+Unit~ProductName, value.var="value")
 #    CountryName Years Unit databaseHouseholds databasePopulation
 #1     Belarus Y1977    0           2942.702             9434.2
 #   databaseRuralPopulation databaseUrbanPopulation originalHouseholds
 #1                4487.318                4946.882                 NA
 #  originalUrban households
 # 1                       NA



data



data

dat <- structure(list(CountryName = c("Belarus", "Belarus", "Belarus", 
"Belarus", "Belarus", "Belarus"), ProductName = c("databaseHouseholds", 
"databasePopulation", "databaseUrbanPopulation", "databaseRuralPopulation", 
"originalHouseholds", "originalUrban households"), Unit = c(0L, 
 0L, 0L, 0L, 0L, 0L), Years = c("Y1977", "Y1977", "Y1977", "Y1977", 
 "Y1977", "Y1977"), value = c(2942.702, 9434.2, 4946.882, 4487.318, 
 NA, NA)), .Names = c("CountryName", "ProductName", "Unit", "Years", 
 "value"), class = "data.frame", row.names = c("1", "2", "3", 
 "4", "5", "6"))

这篇关于重塑面板数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆