Max888
Max888

Reputation: 3770

How to use loop to create multiple columns

I am struggling with creating multiple columns in a DRY way.
I have searched google and stack exchange and I am still struggling with the below.

df <- data.frame(red = 1:10, blue=seq(1,30,3))

myfunction <- function(x){
  log(x) + 10
}

df$green <- myfunction(df$red)
df$yellow <- myfunction(df$blue)

My questions are:
how can I create the green and yellow columns using a for loop?
how can I create the green and yellow using an apply function?

Upvotes: 2

Views: 4008

Answers (2)

Max888
Max888

Reputation: 3770

As pointed out by Sotos, apply is slower than lapply. So I believe the optimal solution is:

df[,c("green","yellow")] <- lapply(df, myfunction)

Upvotes: 1

svenhalvorson
svenhalvorson

Reputation: 1080

I've spent a bit of time working on these kinds of things. Most of the time you're going to want to either know all the names of the new variables or have them work in an orderly pattern so you can just paste together the names with an indexing variable.

df <- data.frame(red = 1:10, blue=seq(1,30,3))

myfunction <- function(x){
  log(x) + 10
}

newcols = apply(X = df, MARGIN = 2, FUN = myfunction)
colnames(newcols) = c("greeen","yellow")
df = cbind(df,newcols)

# Alternative
df <- data.frame(red = 1:10, blue=seq(1,30,3))
colors = c("green", "yellow")
for(i in 1:length(colors)){
  df[,colors[i]] = myfunction(df[,i])
}

Upvotes: 1

Related Questions