bestnas.blogg.se

Write a loop in r
Write a loop in r













write a loop in r

There are many different ways to do this, but I will suggest two similar methods: one using base R, and one using the tidyverse.

Write a loop in r code#

To achieve this, we need to build an expression, which is simply code which has not yet been evaluated. Instead, we need to use NSE ourselves to substitute pkg with the name of the package itself, as if you had written it directly into the console. Remember, the issue is that library uses non-standard evaluation on package names, so we can’t use library(pkg). So, now with a brief understanding of NSE, let’s try to use the library function in a loop again. mtcars %>%įor more details on non-standard evaluation, I recommend reading the Advanced R book. So when this code is used in the mutate function, R is now able to find disp, because dplyr has changed where R looks for the variable. When using this code in the mutate function, dplyr helpfully prevents evaluation, and later re-evaluates by first looking in the provided data, and then in the evaluation environment. R is unable to find the disp variable because it exists as a column in the mtcars dataset, not in the evaluation environment. Standard evaluation in R would find the disp variable and compute the division, so let’s try that: disp / 61.0237 #> Error in eval(expr, envir, enclos): object 'disp' not found The mutate function is calculating disp / 61.0237 and saving the result as a column called displ_l. As an example, let’s look at the dplyr code above. So what is non-standard evaluation? As the name may suggest, it is code which is evaluated in a non-standard way. Mutate(displ_l = disp / 61.0237) library(ggplot2) Try to identify the NSE parts in the following code examples: library(dplyr) Most tidyverse packages also leverage NSE to simplify the typing needed to transform a dataset or plot some data. In fact, NSE is used each time you load in a package without quoting the package name. You may not know what non-standard evaluation is, but you have definitely used it before (perhaps without even realising). In the loop, R tries to be helpful by loading pkg instead of the value stored inside (“ggplot2”, and then “dplyr”).įor most R users, an understanding of non-standard evaluation (NSE) is rarely needed. That is what allows you to use library(tidyverse) instead of library("tidyverse"). If it were that simple, it wouldn’t warrant a blog post! This doesn’t work because the library function uses non-standard evaluation. So to load packages in a loop, one might try: packages Error in library(pkg): there is no package called 'pkg' Most R users would load packages using the library function, such as library(tidyverse).

write a loop in r

If you really want have fun, try loading packages in a loopĪlthough not a pop-quiz, it is certainly a challenge, and a common cause of confusion for R users. As part of this discussion, podcast guest Roger Peng ( noted that:

write a loop in r

In Nick Tierney ( and Saskia Freytag’s ( second Credibly Curious podcast, they briefly delve into the confusing world of non-standard evaluation (NSE).















Write a loop in r