Поскольку Николас2 предоставил данные:
data.frame(
impressions = c("26580", "5741", "7335", "4123"),
date_start = c("2018-10-14", "2018-10-14", "2018-10-14", "2018-10-14"),
action_values = I(list(
data.frame(
action_type = c("app_custom_event.fb_mobile_add_to_cart", "app_custom_event.fb_mobile_add_to_wishlist", "app_custom_event.fb_mobile_content_view"),
value = c("40505.79", "9262.82", "470464"),
stringsAsFactors = FALSE
),
data.frame(
action_type = c("app_custom_event.fb_mobile_add_to_cart", "app_custom_event.fb_mobile_add_to_wishlist", "app_custom_event.fb_mobile_content_view"),
value = c("26309.21", "6970.84", "697029"),
stringsAsFactors = FALSE
),
data.frame(
action_type = c("app_custom_event.fb_mobile_add_to_cart", "app_custom_event.fb_mobile_add_to_wishlist", "app_custom_event.fb_mobile_content_view"),
value = c("253737.97", "44378.59", "575184.59"),
stringsAsFactors = FALSE
),
data.frame(
action_type = c("app_custom_event.fb_mobile_add_to_cart", "app_custom_event.fb_mobile_add_to_wishlist", "app_custom_event.fb_mobile_content_view"),
value = c("42180.84", "4852.95", "282354"),
stringsAsFactors = FALSE
)
)),
stringsAsFactors = FALSE
) -> xdf
вот решение с 0-зависимой базой R:
do.call(
rbind.data.frame,
lapply(
1:nrow(xdf), # row by row
function(.i) {
x <- xdf$action_values[[.i]] # extract the data frame
x$action_type <- gsub("app_custom_event.", "", x$action_type) # clean colnames
x <- as.data.frame(t(unstack(x, value ~ action_type, stringsAsFactors = FALSE)), stringsAsFactors=FALSE) # reshape it
x$impressions <- xdf$impressions[[.i]] # add the other two columns
x$date_start <- xdf$date_start[[.i]]
rownames(x) <- NULL # not necessary but i like proper rownames
x
}
)
)
## fb_mobile_add_to_cart fb_mobile_add_to_wishlist fb_mobile_content_view impressions date_start
## 1 40505.79 9262.82 470464 26580 2018-10-14
## 2 26309.21 6970.84 697029 5741 2018-10-14
## 3 253737.97 44378.59 575184.59 7335 2018-10-14
## 4 42180.84 4852.95 282354 4123 2018-10-14
Непосредственно:
library(microbenchmark)
microbenchmark(
base = do.call(
rbind.data.frame,
lapply(
1:nrow(xdf),
function(.i) {
x <- xdf$action_values[[.i]]
x$action_type <- gsub("app_custom_event.", "", x$action_type)
x <- as.data.frame(t(unstack(x, value ~ action_type, stringsAsFactors = FALSE)), stringsAsFactors=FALSE)
x$impressions <- xdf$impressions[[.i]]
x$date_start <- xdf$date_start[[.i]]
rownames(x) <- NULL
x
}
)
),
tidyverse = purrr::pmap_df(
list(xdf$impressions, xdf$date_start, xdf$action_values),
function(x,y,z) z %>%
tidyr::spread("action_type", "value") %>%
dplyr::mutate(impressions = x, date_start = y) %>%
dplyr::select(impressions, date_start, dplyr::everything()) %>%
dplyr::rename_at(
dplyr::vars(dplyr::matches("^app_custom_event\\.")),
dplyr::funs(stringr::str_replace(.,"^app_custom_event\\.",""))
)
)
) %>% { print(.) ; . } %>% autoplot()
## Unit: milliseconds
## expr min lq mean median uq max neval
## base 1.641284 1.770162 2.320804 1.836188 2.077139 9.536442 100
## tidyverse 17.152142 17.908212 21.554416 18.503356 22.255375 46.630603 100
Также: ?? за то, что вы подтолкнули нас к выполнению вашей работы за вас.Я надеялся, что люди позволят это сделать до тех пор, пока вы не покажете, что попробовали что-то , но не могли оставить решение 57-пакет-зависимостью-с-компиляцией-на-линуксе единственным ответом.