Home > Mobile >  Standard Definition of Vectorization in R
Standard Definition of Vectorization in R

Time:10-14

I want to understand better what is meant by some functions in R being "vectorized". This term is disclosed a lot but I have always understood this simply to mean "unbalanced" or "loose" arguments are recycled.

However, as one simple example both paste() and glue::glue() both say they are vectorized however there ability to recycle "loose" inputs differ.

I understand from the error code why glue::glue() doesn't work but I'm trying to understand are there different flavors of "vectorization" and how would you know which ones are better at handling loose inputs vs. others.

Reference to glue's vectorization https://www.tidyverse.org/blog/2017/10/glue-1.2.0/#:~:text=glue is also vectorized over its inputs.&text=glue_data() works like glue,a data frame or tibble

greetings <- c("hello","howdy")

names <- c("jim","james","johnny")

#this works
paste(greetings,names,"blue")

#this does not work
glue::glue(greetings,names,"blue")

CodePudding user response:

Vectorization means that the code can handle vectors with multiple elements without any special map or lapply function, and it is often performed quickly using parallel processing and multithreading. It doesn't necessarily have anything to do with recycling uneven inputs. A slight modification of your sample data makes this clearer:

greetings <- c("hello","howdy")

firstnames <- c("jim","james")

colors <- c("blue", "red")

#this works - vectorized
paste(greetings,firstnames,colors)
[1] "hello jim blue"  "howdy james red"

#this errors - not vectorized
glue::glue(greetings,firstnames,colors)
Error: All unnamed arguments must be length 1

#this works - doesn't need to be vectorized
glue::glue(greetings[1], firstnames[1], colors[1])
hellojimblue

As you can see, paste can handle vectors with multiple elements simultaneously, whereas glue can only handle vectors with one element at a time.

However, if we use mapply with glue, we can apply the function to vectors but in a slower, more artificial way:

#Artificial vectorization - slower
mapply(function(x,y,z) {glue::glue(x,y,z)}, greetings, firstnames, colors, USE.NAMES = FALSE)
[1] "hellojimblue"  "howdyjamesred"

I would not say that glue::glue is vectorized at all in the way you are attempting to use it, even though it can handle many arguments. It should be able to handle many vectors of arguments as above to call itself fully vectorized.

  •  Tags:  
  • r
  • Related