sapply - Count previous occurences of a string across multiple columns in R -
i have 4 column matrix chronological index, , 3 columns of names (strings). here toy data:
x = rbind(c(1,"sam","harry","joe"), c(2,"joe","sam","jack"),c(3,"jack","joe","jill"),c(4,"harry","jill","joe"))
i want create 3 additional vectors count (for each row) previous (but not subsequent) occurrences of name. here desired result toy data:
y = rbind(c(0,0,0),c(1,1,0),c(1,2,0),c(1,1,3))
i @ loss of how approach problem, , have searched stack overflow relevant examples. dplyr provides answers finding total counts, (as far can tell) not on row row basis.
i attempted write function deal problem in single-column space, no luck, i.e.
thing = sapply(x,function(i)length(grep(i,x[x[1:i]])))
any tips appreciated.
this typical ave
+ seq_along
type of problem, need convert data vectors first:
t(`dim<-`(ave(rep(1, prod(dim(x[, -1]))), c(t(x[, -1])), fun = seq_along) - 1, rev(dim(x[, -1])))) # [,1] [,2] [,3] # [1,] 0 0 0 # [2,] 1 1 0 # [3,] 1 2 0 # [4,] 1 1 3
perhaps more readable:
## x without first column vector x_vec <- c(t(x[, -1])) ## values looking obtain... y_vals <- ave(rep(1, length(x_vec)), x_vec, fun = seq_along) - 1 ## ... in format want obtain them matrix(y_vals, ncol = ncol(x) - 1, byrow = true) # [,1] [,2] [,3] # [1,] 0 0 0 # [2,] 1 1 0 # [3,] 1 2 0 # [4,] 1 1 3
Comments
Post a Comment