R: Use dplyr :: mutate / dplyr :: transmute a function that acts on the entire line

I have a data frame. For an argument, let's say this is a data frame datasets::women. I want to create a vector from a frame by applying a function to each row.

It seems that the usual way to do this is to use dplyrand call, mutateor transmute, for example:

dplyr::transmute(women, some_index = 2 * height + weight)

Excellent: it works. But what if I pulled the calculation some_indexinto a function acting on a string:

calc_some_index <- function(woman) {
    2 * woman$height + woman$weight
}

Is there a way I should call mutate/ transmuteso that it calls this function on every line of its input?

Of course, I see that I get the correct result if I call

dplyr::transmute(women, some_index=calc_some_index(women))

, "", , , transmute. , , :

dplyr::transmute(head(women, n=10), some_index=calc_some_index(women))
+1
1

, .

library(dplyr)
transmute(head(women, n=10),
          some_index=calc_some_index(head(women,10)))

( )

, :

head(women, 10) %>%
   transmute(calc_some_index(.))
+2

Source: https://habr.com/ru/post/1678174/


All Articles