0

I'm new to R and am trying to learn how to create my own functions.

I'm wondering why my following function and subsequent function call

func <- function(data) {
 new_data <- data
 return(new_data)
}
func(iris)

does not create the data frame new_data?

If I submit the following stand alone statement

new_data <- iris

, new_data is created.

I'm using RStudio IDE V2021.9.1.372 with R v4.1.2 on Windows 10.

Thanks for any insights.

  • 1
    `new_data <- func(iris)`. You need to assign result to something. – Park Jul 07 '22 at 05:32
  • 6
    `new_data` is created inside the function environment, it's not available outside of it unless you define it outside. Review R scoping rules https://bookdown.org/rdpeng/rprogdatascience/scoping-rules-of-r.html – guasi Jul 07 '22 at 05:33
  • Thanks, Park and Gausi! I had assumed that the "return(data)" would release the created data frame to the global environment. Park's suggested assignment statement new_data <- func(iris) did the trick. – Bill Knowlton Jul 08 '22 at 04:18

0 Answers0