R - within and ave using multiple inputs -
i have been using functions with , ave in following manner:
dt = within(dt,{sim1 = ave(sample, no, fun = sample_ret)}) this function takes data.table dt , segments variable no. after that, applies function sample_ret input sample , creates column sim1 in data.table.
is there way can make function take multiple inputs? example, instead of taking sample, how can let function sample_ret take column along sample?
if not possible, there function can accomplish want? basically, want segment data.table , apply function using more 1 column inputs?
for example, data.table dt:
col1 col2 type col4 1 2 4 5 6 8 9 10 b 12 13 14 b 16 3 34 16 1 2 c 16 and want segment dt type , how many values greater 6 in col1 , how many values greater 12 in col2 per each type. let's have function greater_than takes in 2 columns , that. how can accomplish want ave or other alternative?
a base r option be
indx <- df1[1:2] >val[col(df1[1:2])] rownames(indx) <- df1$type rowsum(indx+0l, row.names(indx)) # col1 col2 #a 0 1 #b 2 1 #c 0 0 or
library(dplyr) df1 %>% group_by(type) %>% mutate_each(funs(.>df2$.), col1:col2) %>% summarise_each(funs(sum), col1:col2) # type col1 col2 #1 0 1 #2 b 2 1 #3 c 0 0 or possible data.table option suggested @david arenburg
library(data.table) cols <- paste0("col", 1:2) sums <- function(x, y) sum(x > y) setdt(df1)[, map(sums, .sd, val), by=type, .sdcols = cols] # type col1 col2 #1: 0 1 #2: b 2 1 #3: c 0 0 data
df1 <- structure(list(col1 = c(1l, 5l, 9l, 13l, 3l, 1l), col2 = c(2l, 6l, 10l, 14l, 34l, 2l), type = c("a", "a", "b", "b", "a", "c" ), col4 = c(4l, 8l, 12l, 16l, 16l, 16l)), .names = c("col1", "col2", "type", "col4"), class = "data.frame", row.names = c(na, -6l)) df2 <- data.frame(col1=6, col2= 12) val <- c(6,12)
Comments
Post a Comment