data manipulation - r mark different values for same user in row -
i have info follows:
userid <-c(1,1,1,2,2,2,3,3,3) product <-c("a","a","a","b","b","c","a","b","c") result <-c(0,0,0,0,0,0,0,0,0) df<-data.frame(userid,product,result)
i want fill result 1 if userid has different products , 0 if user has same products.
so filled vector like:
result<-c(0,0,0,1,1,1,1,1,1)
i have no thought how without extensive looping, know there should faster way.
you utilize ave
base r
df$result <- with(df, ave(as.character(product), userid, fun=function(x) length(unique(x)))>1) +0 df$result [1] 0 0 0 1 1 1 1 1 1
or suggested @david arenburg, utilize transform
, create new variable result
within df
transform(df, result = (ave(as.character(product), userid, fun = function(x) length(unique(x)))>1)+0)
or
tbl <- rowsums(!!table(df[,-3]))>1 (df$userid %in% names(tbl)[tbl])+0 #[1] 0 0 0 1 1 1 1 1 1
r data-manipulation split-apply-combine
No comments:
Post a Comment