[R] Subsetting year range

arun smartpink111 at yahoo.com
Thu Nov 1 14:01:45 CET 2012


HI,

Without an example dataset, it is a bit difficult to test.  

Just a comment about your code:


"
TableAP <- cbind(zoo(TableAP), Lagpolity4AP1, Lagpolity4AP2, Lagpolity4AP3,
Lagpolity4AP4, Lagpolity4AP5,
LaglrgdpchAP1, LaglrgdpchAP2, LaglrgdpchAP3, LaglrgdpchAP4, LaglrgdpchAP5)
TableAP <- data.frame(TableAP)
"
Instead of data.frame(cbind(TableAP)), you could have used data.frame() alone.

May be this example helps you.
dat1<-data.frame(col1=sample(1:30,10,replace=TRUE),year=c(1961:1970))
dat2<-zoo(dat1)

#Now see the difference in dat4 and dat5

dat4<-data.frame(cbind(dat2,col2=rnorm(10,15)) )  
 str(dat4)
#'data.frame':    10 obs. of  3 variables:
# $ col1: num  27 5 13 19 5 11 4 29 14 21
# $ year: num  1961 1962 1963 1964 1965 ...
# $ col2: num  15.2 13.6 16.9 15.8 15.5 ...
dat5<-data.frame(dat2,col2=rnorm(10,15))
# str(dat5)
#'data.frame':    10 obs. of  3 variables:
# $ col1: int  27 5 13 19 5 11 4 29 14 21
# $ year: int  1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
# $ col2: num  14.9 14 16 14.2 16.2 ...

#your code:
 subset(dat5,year=>1962)
#Error: unexpected '>' in "subset(dat5,year=>"

subset(dat5,year>=1962) 
#   col1 year     col2
#2     5 1962 14.00960
#3    13 1963 15.96100
#4    19 1964 14.16140
#5     5 1965 16.20417
#6    11 1966 15.35976
#7     4 1967 14.90305
#8    29 1968 16.43865
#9    14 1969 15.88728
#10   21 1970 17.17676
 dat5[which(dat5$year>1963),] 
#   col1 year     col2
#4    19 1964 14.16140
#5     5 1965 16.20417
#6    11 1966 15.35976
#7     4 1967 14.90305
#8    29 1968 16.43865
#9    14 1969 15.88728
#10   21 1970 17.17676


A.K.  





----- Original Message -----
From: fuckecon <iamstanhu at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, October 31, 2012 11:19 PM
Subject: [R] Subsetting year range

Hi,

I have a panel data set that I am trying to subset. I am trying to keep
values for years >=1960.
The full set is from 1940 to 2000. 

I tried a few things, but none worked. Here are a couple that I am trying to
use.

TableAPS1 <- subset(TableAP, year => 1959)

TableAPS1 <- TableAP[ which(year > 1959),]


It would be really nice if someone can point out what I am doing wrong with
this part, or somewhere else that I am coding wrong.

Thanks,


Full code:

###########
TableAP <- read.csv("AnnualPanel.csv")
TableAP <- data.frame(TableAP)
TableAP
nrow (TableAP)

install.packages("zoo")
require(zoo)

Lagpolity4AP1 <- lag(zoo(TableAP$polity4), -1)
Lagpolity4AP2 <- lag(zoo(TableAP$polity4), -2)
Lagpolity4AP3 <- lag(zoo(TableAP$polity4), -3)
Lagpolity4AP4 <- lag(zoo(TableAP$polity4), -4)
Lagpolity4AP5 <- lag(zoo(TableAP$polity4), -5)
LaglrgdpchAP1 <- lag(zoo(TableAP$lrgdpch), -1)
LaglrgdpchAP2 <- lag(zoo(TableAP$lrgdpch), -2)
LaglrgdpchAP3 <- lag(zoo(TableAP$lrgdpch), -3)
LaglrgdpchAP4 <- lag(zoo(TableAP$lrgdpch), -4)
LaglrgdpchAP5 <- lag(zoo(TableAP$lrgdpch), -5)

TableAP <- cbind(zoo(TableAP), Lagpolity4AP1, Lagpolity4AP2, Lagpolity4AP3,
Lagpolity4AP4, Lagpolity4AP5,
LaglrgdpchAP1, LaglrgdpchAP2, LaglrgdpchAP3, LaglrgdpchAP4, LaglrgdpchAP5)
TableAP <- data.frame(TableAP)

TableAP <- subset(TableAP, select = c(code, country, year, polity4,
Lagpolity4AP1, Lagpolity4AP2, Lagpolity4AP3,
Lagpolity4AP4, Lagpolity4AP5, lrgdpch, LaglrgdpchAP1, LaglrgdpchAP2,
LaglrgdpchAP3, LaglrgdpchAP4, LaglrgdpchAP5))

fix(TableAP)

# Using data from 1960 to 2000 only

################################################################
################################################################
TableAPS1 <- TableAP[ which(year > 1959),]
################################################################
################################################################

# Keeping only rows with Sample = 1
TableAP <- subset(TableAP, sample == 1)

TableAPS1 <- subset(TableAPS1, select = c(code, country, year, polity4,
Lagpolity4, lrgdpch, Laglrgdpch))

TableAPS1<- ts(TableAPS1)

# Run FE OLS with lag real GDP and lag democracy
# -1 is to subtract the intercept

library(dyn)

FEOLS <- dyn$lm(polity4 ~ -1 + Lagpolity4 + Laglrgdpch + factor(year)+
factor(code), TableAPS1)
FEOLS




--
View this message in context: http://r.789695.n4.nabble.com/Subsetting-year-range-tp4648096.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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