[R] Macro or Loop info/help needed
Jeff
r at jp.pair.com
Thu Mar 22 01:18:44 CET 2012
I'm very new to R having recently made the transition from SPSS and SAS.
In a dataset named t4, I have about 20 variables that are named in a
somewhat chronological order - (e.g., q100ayr, q101ayr, q102ayr, q103ayr,
etc.)
Each variable contains a 2 digit year designation (e.g., 73, 74,75, 76,
etc), which corresponds to the year something occurred (e.g., 73=1973).
Each of the 20 variables is similarly designated, but corresponds to a
different type of event/occurrence.
I need to reorder the data in this fashion:
# first event
t4 $ eventX1973 <- 0
t4$ eventX1973[t4 $ q100ayr== 73 ] <- 1
t4 $ eventX1974 <- 0
t4$ eventX1974[t4 $ q100ayr== 74 ] <- 1
t4 $ eventX1975 <- 0
t4$ eventX1975[t4 $ q100ayr== 75 ] <- 1
etc. for years between 73 and 88
# second event
t4 $ eventZ1973 <- 0
t4$ eventZ1973[t4 $ q101ayr== 73 ] <- 1
t4 $ eventZ1974 <- 0
t4$ eventZ1974[t4 $ q101ayr== 74 ] <- 1
t4 $ eventZ1975 <- 0
t4$ eventZ1975[t4 $ q101ayr== 75 ] <- 1
etc.
The code above will work, but would be many lines long. I'm hoping someone
can give me a quick introduction to what would work best in R. I assume some
type of macro or loop.
Thanks in advance.
Jeff
More information about the R-help
mailing list