1

In my data.frame I want to add automatically some variabeles inside a loop. For instance "abc_1", "abc_2", "abc_3".

data.frame:

x <- c(1,2,3)
y <- c(4,5,6)
test<- data.frame("V1"=x, "V2"=y)

formula to create new variables:

for (i in 1:3){
  paste("test$abc",i,sep="") <- 5
}

Maybe the loop isn't the best method, but ok. So on the left side of the formula in the loop I try to concatenate 3 variables based on the "i" and some text. But paste doesn't work. Anybody got a bether idea?

2
  • Perhaps you need [ or may be test[paste0("abc", 1:3)] <- 5 Commented Mar 5, 2016 at 17:47
  • Perfect, thanks! Still figuring out how and when to use brackets;) Commented Mar 5, 2016 at 17:53

1 Answer 1

1

We could do this without a for loop

test[paste0("abc", 1:3)] <- 5
Sign up to request clarification or add additional context in comments.

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.