Jas*_*lns 7 r plyr dplyr data.table
鉴于此data.frame:
set.seed(4)
df <- data.frame(x = rep(1:5, each = 2), y = sample(50:100, 10, T))
# x y
# 1 1 78
# 2 1 53
# 3 2 93
# 4 2 96
# 5 3 61
# 6 3 82
# 7 4 53
# 8 4 76
# 9 5 91
# 10 5 99
Run Code Online (Sandbox Code Playgroud)
我想编写一些简单的函数(即特征工程)来创建特征x,然后将每个结果data.frames连接在一起。例如:
library(dplyr)
count_x <- function(df) df %>% group_by(x) %>% summarise(count_x = n())
sum_y <- function(df) df %>% group_by(x) %>% summarise(sum_y = sum(y))
mean_y <- function(df) df %>% group_by(x) %>% summarise(mean_y = mean(y))
# and many more...
Run Code Online (Sandbox Code Playgroud)
这可以实现plyr::join_all,但我想知道是否有更好(或更好的性能)法dplyr或data.table?
df_with_features <- plyr::join_all(list(count_x(df), sum_y(df), mean_y(df)),
by = 'x', type = 'full')
# > df_with_features
# x count_x sum_y mean_y
# 1 1 2 131 65.5
# 2 2 2 189 94.5
# 3 3 2 143 71.5
# 4 4 2 129 64.5
# 5 5 2 190 95.0
Run Code Online (Sandbox Code Playgroud)
结合@ SimonOHanlon的data.table方法与@夏侯的Reduce和merge技术的出现,产生了最高效的结果:
library(data.table)
setDT(df)
count_x_dt <- function(dt) dt[, list(count_x = .N), keyby = x]
sum_y_dt <- function(dt) dt[, list(sum_y = sum(y)), keyby = x]
mean_y_dt <- function(dt) dt[, list(mean_y = mean(y)), keyby = x]
Reduce(function(...) merge(..., all = TRUE, by = c("x")),
list(count_x_dt(df), sum_y_dt(df), mean_y_dt(df)))
Run Code Online (Sandbox Code Playgroud)
更新以包含tidyverse/ purrr( purrr::reduce) 方法:
library(tidyverse)
list(count_x(df), sum_y(df), mean_y(df)) %>%
reduce(left_join)
Run Code Online (Sandbox Code Playgroud)