How do I sum portions of a vector into another vector? E.g., for --8<---------------cut here---------------start------------->8---> vec <- 1:10 > breaks <- c(3,8,10)--8<---------------cut here---------------end--------------->8--- I want to get a vector of length 3 with content --8<---------------cut here---------------start------------->8--- 6 = 1+2+3 30 = 4+5+6+7+8 19 = 9+10 --8<---------------cut here---------------end--------------->8--- Obviously, I could write a loop, but I would rather have a vectorized version. Thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://ffii.org http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ One can find Holy Grail or Higgs boson, but not the second sock.
On Dec 10, 2012, at 11:29 AM, Sam Steingold wrote:> How do I sum portions of a vector into another vector? > E.g., for > --8<---------------cut here---------------start------------->8--- >> vec <- 1:10 >> breaks <- c(3,8,10) > --8<---------------cut here---------------end--------------->8--- > I want to get a vector of length 3 with content > --8<---------------cut here---------------start------------->8--- > 6 = 1+2+3 > 30 = 4+5+6+7+8 > 19 = 9+10 > --8<---------------cut here---------------end--------------->8--- > Obviously, I could write a loop, but I would rather have a vectorized > version.> split(vec, findInterval(seq_along(vec), breaks+.001, right=TRUE) ) $`0` [1] 1 2 3 $`1` [1] 4 5 6 7 8 $`2` [1] 9 10 Needed to push the breaks slightly rightward since findInterval generally returns left-closed interval values. -- David Winsemius Alameda, CA, USA
How about?> vec <- 1:10 > breaks <- c(3,8,10) > g <- cut(vec, c(0, breaks)) > sums <- aggregate(vec, list(g), sum)$x > nums <- tapply(vec, g, paste0, collapse="+") > results <- paste0(sums, " = ", nums) > results[1] "6 = 1+2+3" "30 = 4+5+6+7+8" "19 = 9+10" ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Sam Steingold > Sent: Monday, December 10, 2012 1:29 PM > To: r-help at r-project.org > Subject: [R] sum portions of a vector > > How do I sum portions of a vector into another vector? > E.g., for > --8<---------------cut here---------------start------------->8--- > > vec <- 1:10 > > breaks <- c(3,8,10) > --8<---------------cut here---------------end--------------->8--- > I want to get a vector of length 3 with content > --8<---------------cut here---------------start------------->8--- > 6 = 1+2+3 > 30 = 4+5+6+7+8 > 19 = 9+10 > --8<---------------cut here---------------end--------------->8--- > Obviously, I could write a loop, but I would rather have a vectorized > version. > Thanks! > > -- > Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X > 11.0.11103000 > http://www.childpsy.net/ http://palestinefacts.org http://ffii.org > http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ > One can find Holy Grail or Higgs boson, but not the second sock. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
On Dec 10, 2012, at 11:29 AM, Sam Steingold wrote:> How do I sum portions of a vector into another vector? > E.g., for > --8<---------------cut here---------------start------------->8--- >> vec <- 1:10 >> breaks <- c(3,8,10) > --8<---------------cut here---------------end--------------->8--- > I want to get a vector of length 3 with content > --8<---------------cut here---------------start------------->8--- > 6 = 1+2+3 > 30 = 4+5+6+7+8 > 19 = 9+10 > --8<---------------cut here---------------end--------------->8--- > Obviously, I could write a loop, but I would rather have a vectorized > version.> tapply(vec, cut(vec, breaks=c(-Inf, breaks), include.lowest=TRUE), sum) [-Inf,3] (3,8] (8,10] 6 30 19 -- David Winsemius, MD Alameda, CA, USA
Hi, May be this also helps: do.call(rbind,lapply(split(vec,findInterval(vec,c(4,9))),function(x) paste0(sum(x),"=",paste(x,collapse="+")))) ? [,1]????????? #0 "6=1+2+3"???? #1 "30=4+5+6+7+8" #2 "19=9+10"???? a.K. ----- Original Message ----- From: Sam Steingold <sds at gnu.org> To: r-help at r-project.org Cc: Sent: Monday, December 10, 2012 2:29 PM Subject: [R] sum portions of a vector How do I sum portions of a vector into another vector? E.g., for --8<---------------cut here---------------start------------->8---> vec <- 1:10 > breaks <- c(3,8,10)--8<---------------cut here---------------end--------------->8--- I want to get a vector of length 3 with content --8<---------------cut here---------------start------------->8--- 6 = 1+2+3 30 = 4+5+6+7+8 19 = 9+10 --8<---------------cut here---------------end--------------->8--- Obviously, I could write a loop, but I would rather have a vectorized version. Thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://palestinefacts.org http://ffii.org http://jihadwatch.org http://www.PetitionOnline.com/tap12009/ One can find Holy Grail or Higgs boson, but not the second sock. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Dec 10, 2012, at 1:29 PM, Sam Steingold <sds at gnu.org> wrote:> How do I sum portions of a vector into another vector? > E.g., for > --8<---------------cut here---------------start------------->8--- >> vec <- 1:10 >> breaks <- c(3,8,10) > --8<---------------cut here---------------end--------------->8--- > I want to get a vector of length 3 with content > --8<---------------cut here---------------start------------->8--- > 6 = 1+2+3 > 30 = 4+5+6+7+8 > 19 = 9+10 > --8<---------------cut here---------------end--------------->8--- > Obviously, I could write a loop, but I would rather have a vectorized > version. > Thanks!See ?findInterval.> findInterval(seq(along = vec), breaks + 1)[1] 0 0 0 1 1 1 1 1 2 2> as.vector(sapply(split(vec, findInterval(seq(along = vec), breaks + 1)),sum)) [1] 6 30 19 Did you just want the above, or did you really want:> as.vector(sapply(split(vec, findInterval(seq(along = vec), breaks + 1)),function(x) paste(sum(x), "=", paste(x, collapse = "+")))) [1] "6 = 1+2+3" "30 = 4+5+6+7+8" "19 = 9+10" Regards, Marc Schwartz