Getting function variables from data frame in R

df <- data.frame(os = c("android", "android", "ios", "ios", "windows", "windows"),

platform = c("fb","fb", "google", "tiktok", "tiktok", "tiktok"),

country = c("de", "at", "ch", "gb", "fi", "es"))

output<- function (a, b, c){ # a = os, b = platform, c = country

colSums(tables[[b]][[c]][[a]][[month.abb[month(date)]]][, 2:12])

}

output("android", "fb", "de") #works]

how to loop the function rows of df??

apologies for not having a reproducible example.

I'm not 100% sure what you're trying to do, but I think the function pmap from the purrr package is probably what you're looking for.

If you had a fn <- function(os, platform, country) {...} you could pmap(df, fn) and it will evaluate the fn on each "row".

1 Like

Thank you!! It did the the trick. I guess its time to learn more about "purrr" library. Although I have to mention that id did not work before I changed the column names to the same as in the function. Maybe this is what was needed with other libraries as well...

You're welcome! purrr is an amazing package in my opinion, I highly recommend learning it. The map family of functions is the meat of it.

Once you become proficient, you may find that you never need to write a loop every again...

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.