[R] How can I extract part of the data in a panel dataset?

arun smartpink111 at yahoo.com
Mon Apr 8 20:04:22 CEST 2013


Hi,
1)
Grunfeld1951<-Grunfeld[Grunfeld$year==1951,]
 Grunfeld1951
#    firm year    inv   value capital
#17     1 1951 755.90 4833.00 1207.70
#37     2 1951 588.20 2289.50  342.10
#57     3 1951 135.20 1819.40  671.30
#77     4 1951 160.62  809.00  203.50
#97     5 1951  80.30  327.30  683.90
#117    6 1951  95.30  676.90  177.20
#137    7 1951  64.77  179.00  450.60
#157    8 1951  54.38  723.80  129.70
#177    9 1951  56.49  339.90  391.00
#197   10 1951   4.67   83.04    9.07

2)


 Grunfeld51_54<-Grunfeld[Grunfeld$year%in% 1951:1954,]
 head(Grunfeld51_54)
 #  firm year    inv  value capital
#17    1 1951  755.9 4833.0  1207.7
#18    1 1952  891.2 4924.9  1430.5
#19    1 1953 1304.4 6241.7  1777.3
#20    1 1954 1486.7 5593.6  2226.3
#37    2 1951  588.2 2289.5   342.1
#38    2 1952  645.5 2159.4   444.2

 with(Grunfeld51_54,tapply(capital,list(firm),FUN=mean))
#        1         2         3         4         5         6         7         8 
#1660.4500  519.9000  771.6500  313.7750  748.1000  206.8500  478.7500  165.8750 
 #       9        10 
 #429.0000   11.2525 


#or
 ddply(Grunfeld51_54,.(firm),summarise, Mean=mean(capital))
#   firm      Mean
#1     1 1660.4500
#2     2  519.9000
#3     3  771.6500
#4     4  313.7750
#5     5  748.1000
#6     6  206.8500
#7     7  478.7500
#8     8  165.8750
#9     9  429.0000
#10   10   11.2525


#or
aggregate(capital~firm,data=Grunfeld51_54,mean)
#   firm   capital
#1     1 1660.4500
#2     2  519.9000
#3     3  771.6500
#4     4  313.7750
#5     5  748.1000
#6     6  206.8500
#7     7  478.7500
#8     8  165.8750
#9     9  429.0000
#10   10   11.2525


A.K.




----- Original Message -----
From: jpm miao <miaojpm at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Monday, April 8, 2013 10:29 AM
Subject: [R] How can I extract part of the data in a panel dataset?

Taking the Grunfeld data, which is built-in in R, for example,

(1)How can I construct a dataset (or dataframe) that consists of the data
of all firms in 1951?

(2)How can I calculate the average capital in each form over the period
1951-1954?
What I can imagine is to categorize the data by firm, and then select the
data between 1951 and 1954 for each firm, but how can I do it?

Thanks,

Miao

Grunfeld data:
  invest value capital firm year  317.6 3078.5 2.8 General Motors 1935
391.8 4661.7 52.6 General Motors 1936  410.6 5387.1 156.9 General Motors
1937  257.7 2792.2 209.2 General Motors 1938  330.8 4313.2 203.4 General
Motors 1939  461.2 4643.9 207.2 General Motors 1940  512 4551.2 255.2 General
Motors 1941  448 3244.1 303.7 General Motors 1942  499.6 4053.7 264.1 General
Motors 1943  547.5 4379.3 201.6 General Motors 1944  561.2 4840.9 265 General
Motors 1945  688.1 4900.9 402.2 General Motors 1946  568.9 3526.5 761.5 General
Motors 1947  529.2 3254.7 922.4 General Motors 1948  555.1 3700.2
1020.1 General
Motors 1949  642.9 3755.6 1099 General Motors 1950  755.9 4833 1207.7 General
Motors 1951  891.2 4924.9 1430.5 General Motors 1952  1304.4 6241.7
1777.3 General
Motors 1953  1486.7 5593.6 2226.3 General Motors 1954  209.9 1362.4 53.8 US
Steel 1935  355.3 1807.1 50.5 US Steel 1936  469.9 2676.3 118.1 US Steel
1937  262.3 1801.9 260.2 US Steel 1938  230.4 1957.3 312.7 US Steel 1939
361.6 2202.9 254.2 US Steel 1940
...........

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list