Given a dataframe
, say iris
default, how to configure purrr::map_dfr()
function to run on each row of the dataframe
and perform function foo
.
Here is one row of my df, please take into account that value is always a large JSON:
structure(list(Key = "2019/01/04/14/kuku@pupu.com_2ed026cb-8e9f-4392-9cc4-9f580b9d3aab_1345a5a4-3d5b-48a0-a678-67ed09a6f487_2019-01-04-14-52-43-537",
LastModified = "2019-01-04T14:52:44.000Z", ETag = "\"1c6269ab8b7baa85f0d2567de417f0d0\"",
Size = 35280, Owner = "e7c0d260939d15d18866126da3376642e2d4497f18ed762b608ed2307778bdf1",
StorageClass = "STANDARD", Bucket = "comp-kukupupu-streamed-data",
user_name = "kuku@pupu.com", value = list(---here goes a large json),
obs_id = 1137L), row.names = 1L, class = "data.frame")
and my function is:
extract_scroll_data <- function(df) {
tryCatch({
j <- fromJSON(unlist(df$value))
if (is_empty(fromJSON(j$sensorsData)) | is_empty(fromJSON(j$eventList))) {
return(tibble())
} else {
return(set_names(as_tibble(fromJSON(j$eventList, bigint_as_char = TRUE),
.name_repair = "unique"),
nm = c("time_stamp",
"x", "y", "size",
"pressure", "scroll", "state")) %>%
dplyr::mutate("user_name" = df$user_name,
"obs_id" = df$obs_id))
}
}, warning = function(war) {
# Warning handler picks up where error was generated:
print(paste0("Warning: occured at ", df$obs_id, war))
}, error = function(err) {
# error handler picks up where error was generated
print(paste0("Error: occured at ", df$obs_id, err))
}, finally = {
gc()
})
}
Please advise why it doesn't use the dataframe rows?
map_dfr()
, as any other member of map
family iterates over a list and data.frame
is really a list of columns. You can check that out with typeof(iris)
and as.list(iris)
. To make map_dfr()
iterate over rows instead, you have to transform your data.frame
into a list of rows with split()
function.
iris %>%
split(1:nrow(.)) %>%
purrr::map_dfr(do_stuff)