2

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?

1
  • lapply would be standard practice for that Commented Jul 6, 2017 at 14:21

2 Answers 2

1

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])
}
Sign up to request clarification or add additional context in comments.

9 Comments

It's better not to use apply on data.frames. The usual operations are sapply or lapply
I use all of those frequently. Why do you mention this @Sotos
Because apply will coerce to matrix
Okay... at the end of the script df is a data frame as intended!
Yes, I know. Have a look at this It will clarify it
|
1

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

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

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.