r - Average on concatenated string with some conditions -
i wish average on concatenated string conditions. data:
id path events 1 a, b 2, 3 2 c, 3, 4 3 b 5 i'd take average of rows do not have particular path, instance average of rows not have c (i.e row 1 , 3) (2 + 3 + 5) /3 = 3.33
and similar others desired output be
path avg 5 b 3.5 c 3.33 before tried not concatenated data , worked
output <- sapply(as.character(unique(df$path)), function(x) mean(subset(df, !path %in% x)$events)) but not come idea situation
this data
mydata <- data.frame(id =c(1,2,3), path= c("a,b", "c,a", "b"), events =c (("2,3"), ("3,4"), ("5")))
here's tidyverse approach:
library(tidyverse) mydata %>% separate_rows(path, events, convert = true) %>% # unnest rows group_by(path) %>% # set grouping summarise(avg = mean(.$events[!.$id %in% id])) # summarize groups ## # tibble: 3 × 2 ## path avg ## <chr> <dbl> ## 1 5.000000 ## 2 b 3.500000 ## 3 c 3.333333 note summarization uses .$[column name] refer entire column, , [column name] refer values group.
Comments
Post a Comment