Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 447

How to apply multiple columns to a function, one at a time

$
0
0

I have a tibble with two columns. For each row, I want to use the values from the two columns in a function. What is the proper way to do this using tidyverse? As I will describe in more detail below, I don't think the function (call to an API) can be vectorized.

To set ideas, suppose that I have these data:

library(tidyverse)d <- tibble(a=c("a", "b", "d"), b=c("x", "y", "z"))

I then want to apply a function (here, just a very simple thing). Using base R, I can do:

for (i in 1:nrow(d)) {    d[i, "value"] <- paste0(d[i,"a"], d[i, "b"])}

What is the best way---tidyverse solution is hoped for, but not necessary---to do this for each row, passing to the function two arguments?

Note that I am aware that in the above example, I could do d <- d %>% mutate(value = paste0(a, b)), however my actual problem involves an R function that performs an API call to a specific API that I think needs to be run one row at a time. Each call to the API returns a list, which I want to store in my tibble.

Also note that I may need to have a small lag between each API call.


Viewing all articles
Browse latest Browse all 447

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>