Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract results from safely in purrr?

Tags:

r

purrr

Data

Following is example data set:

> dput(veh)
structure(list(Vehicle.ID2 = c("857-850", "857-850", "857-850", 
"857-850"), svel = c(12.21277, 12.22125, 12.2362, 12.26268), 
    frspacing = c(10.73435, 10.64279, 10.54999, 10.45493), Local.Y = c(394.76339, 
    395.98552, 397.20914, 398.43541), PrecVehLocalY = c(409.70444, 
    410.83501, 411.96583, 413.09704), CC0 = c(4.1374232, 4.1374232, 
    4.1374232, 4.1374232), CC8 = c(1.75, 1.75, 1.75, 1.75), CC9 = c(1.04, 
    1.04, 1.04, 1.04), PrecVehLength = c(4.2067, 4.2067, 4.2067, 
    4.2067)), .Names = c("Vehicle.ID2", "svel", "frspacing", 
"Local.Y", "PrecVehLocalY", "CC0", "CC8", "CC9", "PrecVehLength"
), class = c("tbl_df", "data.frame"), row.names = c(NA, -4L))

What I want to do:

I am applying the following function to the data frame veh:

apply_W99 <- function(df){
  for( i in ( seq_len( nrow(df)-1 ) + 1 ) ) {
    if( i <= 2L ) {
      df$Un_dt_1[i] <- df$svel[i-1] * 3.6 + 
        3.6 * ( df$CC8[i] + ( df$CC8[i] - df$CC9[i] ) * 
                  df$svel[i-1] * 3.6 / 80 ) * 0.1
      df$Un_dt_2[i] <- 3.6 * ( df$frspacing[i-1] - df$CC0[i] ) / 0.1
    } else {
      df$Un_dt_1[i] <- df$Un_dt[i-1] + 
        3.6 * ( df$CC8[i] + ( df$CC8[i] - df$CC9[i] ) * 
                  df$Un_dt[i-1] / 80 ) * 0.1
      df$Un_dt_2[i] <- 3.6 * ( df$pred_frspacing[i-1] - df$CC0[i] ) / 0.1
    }
    df$Un_dt[i] <- pmin( df$Un_dt_1[i], df$Un_dt_2[i] )
    if( i <= 2 ) {
      df$pred_Local.Y[i] <- df$Local.Y[i-1] + 
        0.5 * ( ( df$Un_dt[i] + df$svel[i-1] ) / 3.6 ) * 0.1
    } else {
      df$pred_Local.Y[i] <- df$pred_Local.Y[i-1] + 
        0.5 * ( ( df$Un_dt[i] + df$Un_dt[i-1] ) / 3.6 ) * 0.1
    }

    df$pred_frspacing[i] <- df$PrecVehLocalY[i] - df$pred_Local.Y[i] - df$PrecVehLength[i]
  }
  return(df)
}

Note that original data frame has multiple Vehicle.ID2s. I used tidyr and purrr to apply this function. Also, I am using the function safely in purrr to collect both the results and the errors.

library(tidyr)
foob <- veh %>%
  group_by(Vehicle.ID2) %>% 
  nest()

library(purrr)
foos <- foob %>% 
  mutate(joo = map(data, safely(apply_W99)))

This works fine. Now, I want to get the results. safely creates a list with result and error. For the example data:

> dput(foos$joo)
list(structure(list(result = structure(list(svel = c(12.21277, 
12.22125, 12.2362, 12.26268), frspacing = c(10.73435, 10.64279, 
10.54999, 10.45493), Local.Y = c(394.76339, 395.98552, 397.20914, 
398.43541), PrecVehLocalY = c(409.70444, 410.83501, 411.96583, 
413.09704), CC0 = c(4.1374232, 4.1374232, 4.1374232, 4.1374232
), CC8 = c(1.75, 1.75, 1.75, 1.75), CC9 = c(1.04, 1.04, 1.04, 
1.04), PrecVehLength = c(4.2067, 4.2067, 4.2067, 4.2067), Un_dt_1 = c(NA, 
44.73644328054, 45.5093762168213, 46.2847786738341), Un_dt_2 = c(NA, 
237.489364799999, 249.715278159729, 245.301888411047), Un_dt = c(NA, 
44.73644328054, 45.5093762168213, 46.2847786738341), pred_Local.Y = c(NA, 
395.554351295563, 396.807765455249, 398.082684273174), pred_frspacing = c(NA, 
11.0739587044369, 10.9513645447513, 10.8076557268256)), .Names = c("svel", 
"frspacing", "Local.Y", "PrecVehLocalY", "CC0", "CC8", "CC9", 
"PrecVehLength", "Un_dt_1", "Un_dt_2", "Un_dt", "pred_Local.Y", 
"pred_frspacing"), row.names = c(NA, -4L), class = c("tbl_df", 
"data.frame")), error = NULL), .Names = c("result", "error")))

How can I get only the result part as a new data frame?
I tried using foos <- unnest(foos, map(joo, transpose())) but that didn't work.

like image 883
umair durrani Avatar asked Mar 12 '23 15:03

umair durrani


1 Answers

per purrr tutorial from here (https://www.rstudio.com/conference/) try

 transpose(x)[["result"]]
like image 174
userJT Avatar answered Mar 23 '23 16:03

userJT