Wednesday, August 08, 2007

Extract information from a vector in R

Suppose we have a vector of numbers that has 6 elements (e.g. 200001). We know from the codebook that the first 4 positions is year and the following 2 positions is country id. To extract information from this vector accordingly, we use command "substr."

# example
longvector <- 200001
country <- substr(longvector, start=5, stop=6)

1 comment:

Chris Albon said...

Thanks for posting these R code snippits I find them very useful.