Hi I am trying to create a function that is able to calculate this sum: a<-c(2,3,5) b<-(8,7) with "a" meaning 235 and "b" 87. So the result of this sum would be 235 + 87 = 322. I've searched a function like strsplit but that worked for integers and in reverse - not spliting but combining. Can you give me a hand on this please? thanks AD -- View this message in context: http://r.789695.n4.nabble.com/sum-the-values-in-a-vector-as-a-complete-number-tp3250470p3250470.html Sent from the R help mailing list archive at Nabble.com.
> I am trying to create a function that is able to calculate this sum: > > a<-c(2,3,5) > b<-(8,7) > > with "a" meaning 235 and "b" 87. So the result of this sum would be 235 + 87 > = 322.a <- c(2,3,5) b <- c(8,7) vectorToScalar <- function(x) { as.numeric(paste(x, collapse = "")) } vectorToScalar(a) + vectorToScalar(b)
Jorge Ivan Velez
2011-Feb-01 06:02 UTC
[R] sum the values in a vector as a complete number
Hi AD, You might try the following: # data a <- c(2,3,5) b <- c(8,7) # you got this wrong ;) # option 1 foo <- function(x) as.numeric(paste(x, sep = "", collapse = "")) # examples foo(a) # [1] 235 foo(b) # [1] 87 foo(a) + foo(b) # [1] 322 # option 2 foo2 <- function(x, y) foo(x) + foo(y) # example foo2(a, b) # [1] 322 See ?paste and ?as.numeric for more information. HTH, Jorge On Mon, Jan 31, 2011 at 11:22 PM, ADias <> wrote:> > Hi > > I am trying to create a function that is able to calculate this sum: > > a<-c(2,3,5) > b<-(8,7) > > with "a" meaning 235 and "b" 87. So the result of this sum would be 235 + > 87 > = 322. > > I've searched a function like strsplit but that worked for integers and in > reverse - not spliting but combining. > > Can you give me a hand on this please? > > thanks > AD > -- > View this message in context: > http://r.789695.n4.nabble.com/sum-the-values-in-a-vector-as-a-complete-number-tp3250470p3250470.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Henrique Dallazuanna
2011-Feb-01 11:27 UTC
[R] sum the values in a vector as a complete number
Try this: sum(a * c(100, 10, 1)) + sum(b * c(10, 1)) On Tue, Feb 1, 2011 at 2:22 AM, ADias <diasandre@gmail.com> wrote:> > Hi > > I am trying to create a function that is able to calculate this sum: > > a<-c(2,3,5) > b<-(8,7) > > with "a" meaning 235 and "b" 87. So the result of this sum would be 235 + > 87 > = 322. > > I've searched a function like strsplit but that worked for integers and in > reverse - not spliting but combining. > > Can you give me a hand on this please? > > thanks > AD > -- > View this message in context: > http://r.789695.n4.nabble.com/sum-the-values-in-a-vector-as-a-complete-number-tp3250470p3250470.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]