0

I am trying to add a new column to a dataframe, however I need to create many columns for 5 or so dataframes. So I want to write a function. Since all the columns will be the same for each dataframe, this is what I had in mind:

 n = c(2,3,5)
 f = c("two", "three", "five")
 q = c(1,1.5,2.5)
 df= data.frame(n,f,q)
 fxn_foo <- function(x){
      x$egret <- (x$n)/2
 }

 fxn_foo(df)

 df$egret

Why does this produce Null? Are my arguments misspecified? Do I need to specify that the argument is a dataframe?

0

1 Answer 1

3

You need a return statement:

fxn_foo <- function(x){
  x$egret <- (x$n)/2
  return(x)
  }

Then

df <- fxn_foo(df)
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.