![]() ![]() Summarize_means summarize ( across ( diamonds |> group_by ( cut ) |> summarize_means ( ) #> # A tibble: 5 × 9 #> cut carat depth table price x y z n #> #> 1 Fair 1.05 64.0 59.1 4359. That means the function name should never be followed by (). It’s important to note that we’re passing this function to across(), so across() can call it we’re not calling it ourselves. This is one of the features that makes R a functional programming language. This is a pretty special feature of R: we’re passing one function ( median, mean, str_flatten, …) to another function ( across). In simple cases, as above, this will be a single existing function. The second argument to across() defines how each column will be transformed. For example, !where(is.numeric) selects all non-numeric columns, and starts_with("a") & where(is.logical) selects all logical columns whose name starts with “a”. Just like other selectors, you can combine these with Boolean algebra. where(is.logical) selects all logical columns.where(is.POSIXct) selects all date-time columns.where(is.Date) selects all date columns.where(is.character) selects all string columns.where(is.numeric) selects all numeric columns.Where() allows you to select columns based on their type: Note grouping columns ( grp here) are not included in across(), because they’re automatically preserved by summarize(). There are two additional selection techniques that are particularly useful for across(): everything() and where(). This uses the same specifications as select(), Section 4.3.2, so you can use functions like starts_with() and ends_with() to select columns based on their name. We’ll also discuss two important variations, if_any() and if_all(), which work with filter(). names argument when you need additional control over the names of output columns, which is particularly important when you use across() with mutate(). fns, specifies what to do with each column. cols, specifies which columns you want to iterate over, and the second argument. You’ll use the first two every time you use across(): the first argument. We’re just going to use a couple of purrr functions in this chapter, but it’s a great package to explore as you improve your programming skills.ĭf |> summarize ( n = n ( ), across ( a : d, median ), ) #> # A tibble: 1 × 5 #> n a b c d #> #> 1 10 -0.246 -0.287 -0.0567 0.144Īcross() has three particularly important arguments, which we’ll discuss in detail in the following sections. You’ve seen dplyr before, but purrr is new. In this chapter, we’ll focus on tools provided by dplyr and purrr, both core members of the tidyverse. Learning functional programming can easily veer into the abstract, but in this chapter we’ll keep things concrete by focusing on three common tasks: modifying multiple columns, reading multiple files, and saving multiple objects. Now it’s time to learn some more general tools, often called functional programming tools because they are built around functions that take other functions as inputs. unnest_wider() and unnest_longer() create new rows and columns for each element of a list-column.group_by() plus summarize() computes summary statistics for each subset.facet_wrap() and facet_grid() draws a plot for each subset.This book has already given you a small but powerful number of tools that perform the same action for multiple “things”: ![]() In most other languages, you’d need to explicitly double each element of x using some sort of for loop. For example, if you want to double a numeric vector x in R, you can just write 2 * x. Iteration in R generally tends to look rather different from other programming languages because so much of it is implicit and we get it for free. ![]() In this chapter, you’ll learn tools for iteration, repeatedly performing the same action on different objects. You can find the complete first edition at. ![]() This chapter is largely complete and just needs final proof reading. You are reading the work-in-progress second edition of R for Data Science. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |