Reshaping panel data

泪湿孤枕 提交于 2019-12-12 04:56:09

问题


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.

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

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

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


回答1:


Try:

 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

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"))


来源:https://stackoverflow.com/questions/25352779/reshaping-panel-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!