RockefellerUniversity / Intro_To_R_1Day

Introduction to R training course
https://rockefelleruniversity.github.io/Intro_To_R_1Day/
3 stars 2 forks source link

For loops #42

Open matthew-paul-2006 opened 3 years ago

matthew-paul-2006 commented 3 years ago

Questions from Mark Gad:

So I have been trying to use what we learned in class so far on my data. I want to make these binding curves for an assay that I run in lab using ggplot2. I also found a package that will do the Michaelis–Menten analysis I need for me.

Now , where I am struggling is making loops so I do not have to write code for every curve. Ideally, I can input my data from excel at the beginning and not have to rename things throughout for it work.

Is R a good language to do this? And are you able to give me tips on how to get this to work? I am such a beginner when it comes to working with R.

matthew-paul-2006 commented 3 years ago

This seems like a very good use for R. It seems that you have a good simple set of code in the html. And I'm sure the R package will allow additional options.

So as for your questions about loops, R can definitely do this. We covered loops in the course. Specifically, a 'for loop' would be useful for you: https://rockefelleruniversity.github.io/Intro_To_R_1Day/presentations/slides/introToR_Session2.html#25.

You can also use one of the apply functions, but I find this is slightly less intuitive when you are learning.

As for the tips to get it to work. Start with getting the code to work for one instance. Then you can build it into a 'for loop' that iterates over many input data. Depending on how many you are trying to plot with the loop, it is also good to start with a subset. So start by getting it to work for the first set of data. Then get it work for the first 5 or so. Once your happy then you can unleash it on the whole dataset.

One point of note that may trip you up. When you do ggplot inside a 'for loop' the plot does not appear. If you want every plot to appear in your Rmd render, then you will want to use the print function. Here's a very rough example.

for (i in 1:length(data)){

mydata <- read in the data function
p <- ggplot(mydata, aes(........)) + ..... etc
print(p)

}