r - Using dplyr first function but ignoring a particular character -
i wish add first feature in following dataset in new column
mydf <- data.frame (customer= c(1,2,1,2,2,1,1) , feature =c("other", "a", "b", "c", "other","b", "c")) customer feature 1 1 other 2 2 3 1 b 4 2 c 5 2 other 6 1 b 7 1 c
by using dplyr
. however, wish code ignore "other" feature in data set , choose first 1 after "other".
so following code not sufficient:
library (dplyr) new <- mydf %>% group_by(customer) %>% mutate(firstfeature = first(feature))
how can ignore "other" reach following ideal output:
customer feature firstfeature 1 1 other b 2 2 3 1 b b 4 2 c 5 2 other 6 1 b b
with dplyr
can group customer
, take first feature
every group.
library(dplyr) mydf %>% group_by(customer) %>% mutate(firstfeature = feature[feature != "other"][1]) # customer feature firstfeature # <dbl> <chr> <chr> #1 1 other b #2 2 #3 1 b b #4 2 c #5 2 other #6 1 b b #7 1 c b
similarly can base r ave
mydf$firstfeature <- ave(mydf$feature, mydf$customer, fun= function(x) x[x!= "other"][1])
Comments
Post a Comment