Feb 16, 2011

Stata or R - How to create dynamic variables in R?

As we dig deeper into Stata or R debate, a few questions have come up.


Question 1: One of the things Stata does well is the way it constructs new variables (see example below). How to do this in R?



We can rewrite it as-is using for loops in R, which is slow and not elegant. What's the elegant way to write this in R? I haven't used plyr yet... Time to learn it?

Link to question on StackOverflow

6 comments:

  1. Assuming popA1989, popB1989 ... already exist in data.frame dat.

    require(stringr)
    pop <- LETTERS[1:4]
    years <- 1990:2000

    for(i in years){
    dat[str_c(str_c("pop",pop),i)] <- dat[str_c(str_c("pop",pop),i-1)]*(1+dat[rep(str_c("trend",i-1),length(pop))])
    }

    ReplyDelete
  2. Thank you for your attention in informing well to known facts. Nice review! Also turn your attention that it needs to search for mobile software development companies if you need .

    ReplyDelete
  3. Nice code. Thanks for sharing

    ReplyDelete
  4. your giving code very good now i solve my equation thanks for share it annotated bibliography generator .

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Writing bibliography does not always require citing text; it’s rather an objective review of each used source: newspaper or magazine article, audio, lecture, video/film, have a peek at this web-site to find more information!

    ReplyDelete