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
______________________________________________
R-help@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.

Reply via email to