1

Looked over several posts on this topic, but still couldn't figure out. Thought I'd just ask:

I wrote a for-loop:

for (i in 0:5) {
     est16_y2016$pov50_[i] <- est16_y2016$pop[i]*est16_y2016$ITPR_0.5
               }

to achieve the same results as the following code:

 est16_y2016$pov50_0 <- est16_y2016$pop0 * est16_y2016$ITPR_0.5
 est16_y2016$pov50_1 <- est16_y2016$pop1 * est16_y2016$ITPR_0.5
 est16_y2016$pov50_2 <- est16_y2016$pop2 * est16_y2016$ITPR_0.5 
 est16_y2016$pov50_3 <- est16_y2016$pop3 * est16_y2016$ITPR_0.5 
 est16_y2016$pov50_4 <- est16_y2016$pop4 * est16_y2016$ITPR_0.5 
 est16_y2016$pov50_5 <- est16_y2016$pop5 * est16_y2016$ITPR_0.5 

But the loop doesn't work. No error message, no new variables generated either. Help! Thanks.

15
  • 1
    Try paste0('est16_y2016$pov50_',i) Commented Aug 24, 2018 at 18:28
  • 4
    Seems like an XY problem. What are you trying to do? Please provide a little more context. Commented Aug 24, 2018 at 18:31
  • @JohnColeman I'm trying to generate some new variables (you probably can tell from my code), and I want to use for-loop to make the code more compact. Commented Aug 24, 2018 at 18:37
  • @A.Suliman where do I put it in the loop? Thanks. Commented Aug 24, 2018 at 18:38
  • 2
    @EricWang Sorry, this should help for (i in 0:5) est16_y2016[,paste0('pov50_',i)] <- est16_y2016[,paste0('pop',i)]*est16_y2016$ITPR_0.5 } Commented Aug 24, 2018 at 18:49

3 Answers 3

3

Though the construct data$column_name is very convenient when in an interactive R session, when programming it may cause some problems. @A.Suliman's comment presents a way to solve those problems, here is another one.

for(i in 0:5){
    target <- paste("pov50", i, sep = "_")
    pop <- paste0("pop",i)
    est16_y2016[[target]] <- est16_y2016[[pop]]*est16_y2016[["ITPR_0.5"]]
}
Sign up to request clarification or add additional context in comments.

Comments

0

It's kind of hard to answer your question without a reproducible example but I will give it a shot. est16_y2016$pop[i] will give you the ith element of est16_y2016$pop (which probably doesn't even exist. What you want instead is est16_y2016[paste0("pop",i)] so you code should look like this:

for (i in 0:5) 
    {
     est16_y2016[[paste0("pov50_",i)]] <- est16_y2016[[paste0("pop",i)]]*est16_y2016$ITPR_0.5
     }

(edited)

6 Comments

Do the same with paste0("pov50_",i). And this looks like a df, use double [[ as in est16_y2016[[paste0("pop",i)]]. You should edit the answer.
GordonShumway, your assignment is wrong. The OP wants columns named pov50_0, pov50_1, etc. The suggestion to use paste0("pop",i) is for the lhs, not rhs.
Please see @A. Suliman 's answer above. Not all questions require a reproducible example.
@EricWang Yes they do, see the amount of comments, with a reproducible example it would all have been more clear to start with.
@RuiBarradas they do? We've already got a solution. Matter of fact, the very first comment by A.Suliman is a solution. It's funny that so many people who don't know the answer just keep commenting.
|
-1

It is easy to create a new variable name with paste(), and the problem is how to use the corresponding variable instead of the name of a variable.

  for (i in 0:5){ 
    # Create new variable names
    pov.name = paste0("est16_y2016$pov50_",i)
    pop.name = paste0("est16_y2016$pop",i)

    assign(pov.name,eval(parse(text = pop.name))*est16_y2016$ITPR_0.5)
    }

In this code,

eval(parse(text = pop.name) uses the string "pop.name" as a variable name

assign(pov.name,value1)creates a variable named pov.name and assigns value1 to pov.name

This way, you can get six new variables without using a dataframe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.