--3-Functional programming

#1-Why functional programming

​Why functional programming videoarrow-up-right: a long video about functional programming!

#2-Using a for loop to remove duplication

df <- data.frame(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)

#We've provided some code to get you started. 
#Fill in the body of the for loop to calculate the median of each column and 
#store the results in output.

# Initialize output vector
output <- vector("double", ncol(df))  

# Fill in the body of the for loop
for (i in seq_along(df)) {            
  output[[i]] <- median(df[[i]])
}

# View the result
output

#3-Turning the for loop into a function

#4-What about column means?

#5-What about column standard deviations?

#6-Uh oh...time to write a function again

#7-Functions can be arguments too

​Functions can be arguments too videoarrow-up-right​

#8-Using a function as an argument

#9-Introducing purrr

​Introducing purrrarrow-up-right: a long video about purrr package!

​https://www.rdocumentation.org/packages/purrr/versions/0.2.2.2arrow-up-right​

​http://data.library.virginia.edu/getting-started-with-the-purrr-package-in-r/arrow-up-right​

​https://www.youtube.com/watch?v=A8UaL47UXYEarrow-up-right​

#10-The map functions

#11-The ... argument to the map functions

#12-Picking the right map function

#13-Shortcuts

​Rstudio keyboard shortcutsarrow-up-right​

#14-Solve a simple problem first

#15-Using an anonymous function

#16-Using a formula

#17-Using a string

#18-Using a numeric vector

#19-Putting it together with pipes

Last updated