r - Reshaping panel data -


i need reshape data panel data analysis. searched internet , found out how desired results using stata; supposed use r , excel.

my initial , final data(the desired result) looks similar given in first page of example of reshaping data stata. http://spot.colorado.edu/~moonhawk/technical/c1912567120/e220703361/media/reshape.pdf

is attainable r or excel? tried using melt function reshape2 library, yet 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 this:

countryname  years  databasehouseholds databasepopulation databaseurbanpopulation databaseruralpopulationunit originalhousehold originalurbanhouseholds belarus 

in columns databasehouseholds, databasepopulation,... should respective values, can use dataset panel modeling. thank much.

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

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -