separar - Agregue si la cadena contiene texto específico en R
gsub r (3)
He visto muchas publicaciones sobre este tema, así que me disculpo si es un duplicado pero no pude resolver mi problema.
yo tengo
df <- data.frame(name = c(''bike+ride'',''shoe+store'',''ride'',''mountian%20bike'',''ride+along''),
count = c(2,5,8,7,6))
y desea sumar cada count
si su name
contiene un group
cuerdas
group <- data.frame(group = c(''ride'',''bike''))
Entonces el resultado final se ve así:
Group Count
bike 9
ride 16
¿Alguien puede ayudar?
Una forma de usar los paquetes tidyverse
purrr
, dplyr
y tidyr
:
library(tidyverse) # for dplyr, purr and tidyr
groups <- c(''ride'',''bike'')
map_df(groups, ~setNames(summarize_(df, interp(~sum(df$count[grepl(var, name)], na.rm = TRUE), var = .x)), .x)) %>%
gather(group, count, na.rm = TRUE)
Una forma es
do.call(rbind, sapply(group$group, FUN = function(x, df) {
out <- df[grepl(pattern = x, x = df$name), ]
data.frame(group = x, count = sum(out$count))
}, df = df, simplify = FALSE))
group count
1 ride 16
2 bike 9
En dos pasos:
# make a data.frame which locates where each group level is located
grp <- as.data.frame(sapply(group$group, FUN = function(x) grepl(pattern = x, x = df$name)))
names(grp) <- group$group
# based on above location (TRUE/FALSE), sum accordingly
data.frame(count = apply(grp, MARGIN = 2, FUN = function(x, df) {
sum(df[x, "count"])
}, df = df))
count
ride 16
bike 9
Una idea base R
sapply(sapply(as.character(group$group), function(i) grep(i, df$name)), function(i) sum(df$count[i]))
#or make it a function
aggr1 <- function(var1, grp, cnt){
m1 <- sapply(as.character(grp), function(i) grep(i, var1))
final_d <- sapply(m1, function(i) sum(cnt[i]))
return(data.frame(Group = names(final_d),
Count = as.integer(final_d), stringsAsFactors = FALSE)
)
}
aggr1(df$name, group$group, df$count)
# Group Count
#1 ride 16
#2 bike 9