R matrix: unique by one column, add the values on other column and concatenate the values on other -
in r have matrix
> id size pattern [1,] "1" "24" "100" [2,] "2" "10" "111" [3,] "3" "2" "111"
and want unique pattern, add size , concatenate id:
> id size pattern [1,] "1" "24" "100" [2,] "2-3" "12" "111" (this pattern duplicated, add size , concatenate id)
i can do:
> = unique(a[,"pattern"])
but couldn't figure out how add , concatenate.
thanks in advance!
if have columns want treat numeric (adding values together) , want treat character (pasting them) matrix isn't right structure data. should using data frame.
using plyr:
library(plyr) ddply(data.frame(a), .(pattern), summarise, id=paste(id, collapse="-"), size=sum(as.numeric(size)))
Comments
Post a Comment