Part 7 (Class 8): Lists/Functions/Intro to Purrr

Materials from class on Wednesday, February 23, 2022

R Project files

Please download the part7 sub-folder from this dropbox link. Be sure to unzip if necessary. Knit the part7.Rmd to install any required packages.

Class Video

View last year’s class and materials here.

Post-Class

Please fill out the following survey and we will discuss the results during the next lecture. All responses will be anonymous.

  • Clearest Point: What was the most clear part of the lecture?
  • Muddiest Point: What was the most unclear part of the lecture to you?
  • Anything Else: Is there something you’d like me to know?

https://forms.gle/4tVx1mL7SzQx7MCu5

Muddiest Points

The number of responses so far are low, so I will try to update this later.

I am now a bit confused on lists vs. vectors vs. matrices. I’m a little bit unsure about when to use [ ] and when to use [[ ]]

I am still a little confused about the indexing of things in lists (single vs double brackets). I think it’s going to take me some trial-and-error practice to see how it works! I remember reading the first couple chapters of the R for data science book last summer and being hopeless confused by indexing, so it’s not your explanations! It’s just…wierd and confusing.

It is extremely weird and confusing! I am hoping homework 7 will help since it requires that you explore a little bit with indexing and what the results are. I will try to keep going over this in class with more examples.

The function portion of the lecture since there are so many parts.

complex function example

I hope to have more examples this week for practice.

function. Could we get multiple outcome (different value or type) in one function?

Yes! I meant to show this after we learned about lists. Here is an example of how you would do that, by returning a list (usually we use a named list for clarity)

# trivial function that returns the mean, length of a vector, as well as the input vector
my_new_fun <- function(vec) {
  v_mean <- mean(vec)
  l_len <- length(vec)
  return(list(mean = v_mean, length = l_len, input_vec = vec))
}

# save the output from that function applied to a vector
myout <- my_new_fun(c(1,4,2,40))

myout
## $mean
## [1] 11.75
## 
## $length
## [1] 4
## 
## $input_vec
## [1]  1  4  2 40
myout$mean
## [1] 11.75
myout$length
## [1] 4
myout$input_vec
## [1]  1  4  2 40

I am finding it very difficult to figure out how to take even a clean dataset and translate it into something useable. I think I am understanding the logic of what I want to do, but the syntax is very confusing to me.

Do you mean the syntax of lists and functions? Or of data cleaning? Or all the syntax of R? Definitely reach out to me and Colin if you want to talk about anything more specific and in depth. If you are brand new to R before this class, it will definitely take more than the 11*3 hours of learning to get used to the syntax, as I agree it can be extremely confusing! It will take many iterations of practice, struggle, succeed. Eventually it will start to click! But it really takes practice writing your own code over and over.

Clearest Points

simple function example

function( ){ } - I love this!! I needed help with functions to help streamline my work and this class was so incredibly helpful!

Yay! functions are the most useful thing!

How purrr works –this has NEVER made any sense to me before, no matter what I read, so thank you!!

So glad!

The list section of the lecture

Well that’s good, I hope next class doesn’t make it less clear =)

Other messages

I need to find I use for vembedr::embed_url( ) because it looks pretty dang cool

All our Rmds should have videos from now on.

I really appreciated the way you walked through making a function out of the loading & cleaning mouse data. I find writing anything but the simplest functions tricky, so seeing how you approach it was really helpful! It makes much more sense to me now that you’d first write code that works in particular instance, then go back and “generic-ize” the inputs, and then troubleshoot. I have a hard time thinking about how to do stuff generically, but it’s much easier to write a specific case and then go back and make it generic! Thank you!!

I like to hear this, because I think this is the most common way we create and use functions in analysis work. You realize you need to do something again and probably again, and so you decide to make a function. Then, you manipulate existing code into a function. Rinse and repeat!