r - performing calculation by calling values with .$ and .[ ] -


this purely conceptual question, how refer elements of vector c(2,3) in calculation using .$ or .[]?

library(tidyverse)  c(2, 3) %>%    .[1] * .[2] 

this code works fine requires temporary object (v) created:

v <- c(2,3) v[1] * v[2] 

and i'd know how perform calculation in tidyverse without creating temporary object v.

we need place braces avoid operator precedence

c(2, 3) %>%         {.[1] * .[2]} #[1] 6 

also, use map2 multiply corresponding elements (tidyverse)

map2_dbl(2, 3, `*`) #[1] 6  map2_dbl(2:5, 6:9, `*`) #[1] 12 21 32 45 

or reduce

c(2, 3) %>%      reduce(`*`) #[1] 6  list(2:5, 6:9) %>%             reduce(`*`) #[1] 12 21 32 45 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -