R equivalent of Stata's for-loop over local macro list of stubnames

esiotrot

I'm a Stata user that's transitioning to R and there's one Stata crutch that I find hard to give up. This is because I don't know how to do the equivalent with R's "apply" functions.

In Stata, I often generate a local macro list of stubnames and then loop over that list, calling on variables whose names are built off of those stubnames.

For a simple example, imagine that I have the following dataset:

study_id year varX06 varX07 varX08 varY06 varY07 varY08
   1       6   50     40     30     20.5  19.8   17.4
   1       7   50     40     30     20.5  19.8   17.4
   1       8   50     40     30     20.5  19.8   17.4
   2       6   60     55     44     25.1  25.2   25.3
   2       7   60     55     44     25.1  25.2   25.3
   2       8   60     55     44     25.1  25.2   25.3 
   and so on...

I want to generate two new variables, varX and varY that take on the values of varX06 and varY06 respectively when year is 6, varX07 and varY07 respectively when year is 7, and varX08 and varY08 respectively when year is 8.

The final dataset should look like this:

study_id year varX06 varX07 varX08 varY06 varY07 varY08 varX varY
   1       6   50     40     30     20.5  19.8   17.4    50  20.5
   1       7   50     40     30     20.5  19.8   17.4    40  19.8
   1       8   50     40     30     20.5  19.8   17.4    30  17.4 
   2       6   60     55     44     25.1  25.2   25.3    60  25.1
   2       7   60     55     44     25.1  25.2   25.3    55  25.2
   2       8   60     55     44     25.1  25.2   25.3    44  25.3 
   and so on...

To clarify, I know that I can do this with melt and reshape commands - essentially converting this data from wide to long format, but I don't want to resort to that. That's not the intent of my question.

My question is about how to loop over a local macro list of stubnames in R and I'm just using this simple example to illustrate a more generic dilemma.

In Stata, I could generate a local macro list of stubnames:

local stub varX varY

And then loop over the macro list. I can generate a new variable varX or varY and replace the new variable value with the value of varX06 or varY06 (respectively) if year is 6 and so on.

foreach i of local stub {
    display "`i'"  
    gen `i'=.      
    replace `i'=`i'06 if year==6  
    replace `i'=`i'07 if year==7
    replace `i'=`i'08 if year==8
}

The last section is the section that I find hardest to replicate in R. When I write 'x'06, Stata takes the string "varX", concatenates it with the string "06" and then returns the value of the variable varX06. Additionally, when I write 'i', Stata returns the string "varX" and not the string "'i'".

How do I do these things with R?

I've searched through Muenchen's "R for Stata Users", googled the web, and searched through previous posts here at StackOverflow but haven't been able to find an R solution.

I apologize if this question is elementary. If it's been answered before, please direct me to the response.

Thanks in advance,
Tara

jlhoward

Well, here's one way. Columns in R data frames can be accessed using their character names, so this will work:

# create sample dataset
set.seed(1)    # for reproducible example
df <- data.frame(year=as.factor(rep(6:8,each=100)),   #categorical variable
                 varX06 = rnorm(300), varX07=rnorm(300), varX08=rnorm(100),
                 varY06 = rnorm(300), varY07=rnorm(300), varY08=rnorm(100))

# you start here...
years   <- unique(df$year)
df$varX <- unlist(lapply(years,function(yr)df[df$year==yr,paste0("varX0",yr)]))
df$varY <- unlist(lapply(years,function(yr)df[df$year==yr,paste0("varY0",yr)]))

print(head(df),digits=4)
#   year  varX06  varX07  varX08   varY06  varY07  varY08    varX     varY
# 1    6 -0.6265  0.8937 -0.3411 -0.70757  1.1350  0.3412 -0.6265 -0.70757
# 2    6  0.1836 -1.0473  1.5024  1.97157  1.1119  1.3162  0.1836  1.97157
# 3    6 -0.8356  1.9713  0.5283 -0.09000 -0.8708 -0.9598 -0.8356 -0.09000
# 4    6  1.5953 -0.3836  0.5422 -0.01402  0.2107 -1.2056  1.5953 -0.01402
# 5    6  0.3295  1.6541 -0.1367 -1.12346  0.0694  1.5676  0.3295 -1.12346
# 6    6 -0.8205  1.5122 -1.1367 -1.34413 -1.6626  0.2253 -0.8205 -1.34413

For a given yr, the anonymous function extracts the rows with that yr and column named "varX0" + yr (the result of paste0(...). Then lapply(...) "applies" this function for each year, and unlist(...) converts the returned list into a vector.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Stata local macro not defined

From Dev

Stata: Generating variables in a loop using tuples local macro

From Dev

R: Is there an equivalent of Stata's ibn. function?

From Dev

How to subset a local macro in Stata

From Dev

R equivalent of Stata *

From Dev

What is the equivalent to Stata's portmanteau (Q) test for white noise in R?

From Dev

What is the equivalent to Stata's portmanteau (Q) test for white noise in R?

From Dev

List Indexing in R over a loop

From Dev

is there an equivalent to Stata's egen function?

From Dev

pandas equivalent of Stata's encode

From Dev

R - How to print progress in a loop over list?

From Dev

For loop conversion from Stata to R

From Dev

Loop containing macro variables which are specified with character list/vector in R

From Dev

SAS Loop over a list of variables inside a macro (read one each time)

From Dev

Stata: return a macro containing a list of all scalars stored in e()

From Dev

How do I store a LONG list of names in a macro in Stata?

From Dev

Loop over list of years

From Dev

for loop over a list

From Dev

Split dataframe based on value in column - loop over list of id's

From Dev

C macro to run a loop over a range

From Dev

R: Loop over list of data frames and create plot of columns with constraints

From Dev

Is there an equivalent to C's __LINE__ macro in Pascal?

From Dev

Is there an equivalent to C's __LINE__ macro in Pascal?

From Dev

Loop over list in list in Ansible

From Dev

In Stata, how can I append to a local varlist during a loop?

From Dev

Loop over decimals in R

From Dev

How can I iterate over a list with a macro?

From Dev

Arrays / Vector equivalent in Stata

From Dev

For loop over a List of Data frames

Related Related

HotTag

Archive