2

Is there a way to create multiple variables in a loop. For example, if I have a variable, called 'test' among others, in my data frame, how can I create a series of new variables called say 'test1', 'test2', ... 'testn' that are defined as test^1, test^2... test^n

As an example

mynum <- 1:10
myletters <- letters[1:10]
mydf <- data.frame(mynum, myletters)

mydf
   mynum myletters
1      1         a
2      2         b
3      3         c
4      4         d
5      5         e
6      6         f
7      7         g
8      8         h
9      9         i
10    10         j

for (i in 1:5)
    {paste0(var, i) <- mynum^i
    }

But it errors out.

I am trying to create variables like var1, var2, var3 etc which are mynum^1, mynum^2, mynum^3 etc.

Best regards

Deepak

3 Answers 3

3

You can use lapply to create new columns and combine them using do.call + cbind.

n <- 1:5
mydf[paste0('var', n)] <- do.call(cbind, lapply(n, function(x) mydf$mynum^x))
mydf

#   mynum myletters var1 var2 var3  var4   var5
#1      1         a    1    1    1     1      1
#2      2         b    2    4    8    16     32
#3      3         c    3    9   27    81    243
#4      4         d    4   16   64   256   1024
#5      5         e    5   25  125   625   3125
#6      6         f    6   36  216  1296   7776
#7      7         g    7   49  343  2401  16807
#8      8         h    8   64  512  4096  32768
#9      9         i    9   81  729  6561  59049
#10    10         j   10  100 1000 10000 100000

Or with purrr's map_dfc

mydf[paste0('var', n)] <- purrr::map_dfc(n, ~mydf$mynum^.x)
Sign up to request clarification or add additional context in comments.

Comments

2

Try this, you have to take into account that you have to move the position of the new variables. That is why I use i+2 in the loop. Here the code:

#Data
mynum <- 1:10
myletters <- letters[1:10]
mydf <- data.frame(mynum, myletters,stringsAsFactors = F)

The loop:

#Loop
for (i in 1:5)
{
  mydf[,i+2]  <- mydf[,'mynum']^i
  names(mydf)[i+2] <- paste0('var',i)
}

Output:

   mynum myletters var1 var2 var3  var4   var5
1      1         a    1    1    1     1      1
2      2         b    2    4    8    16     32
3      3         c    3    9   27    81    243
4      4         d    4   16   64   256   1024
5      5         e    5   25  125   625   3125
6      6         f    6   36  216  1296   7776
7      7         g    7   49  343  2401  16807
8      8         h    8   64  512  4096  32768
9      9         i    9   81  729  6561  59049
10    10         j   10  100 1000 10000 100000

2 Comments

Oh, thank you! This was exactly what I was looking for. Seems simple after you see the answer, isn't it?
@DeepakAgarwal Yes, it is like a matrix. If you feel this answer was useful, you could accept it by ticking the click on the left side of this answer. It is up to you :)
1

An option with map

library(dplyr)
library(purrr)
map_dfc(1:5, ~ mydf$mynum^.x) %>% 
                 rename_all(~ str_replace(., '\\.+', 'var')) %>% 
                 bind_cols(mydf, .)

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.