Skip to content

Sum two Vectors of different length

5 messages · Alaios, arun, PIKAL Petr +1 more

#
Hi,

Please check this link:
(http://r.789695.n4.nabble.com/calculations-with-vectors-of-unequal-length-td3477848.html)


A.K.

----- Original Message -----
From: Alaios <alaios at yahoo.com>
To: R help <R-help at r-project.org>
Cc: 
Sent: Friday, August 3, 2012 9:13 AM
Subject: [R] Sum two Vectors of different length

Dear all,
in one part of my code I want to sum two vectors element-wise
the problem is that either the 1st vector or the 2nd vector always have one or two less elements

example of my problem

In TotalVector + (datalist2[[1]]$dataset$Results[[j]]$Results[[time]]$Sweep) :
? L?nge des l?ngeren Objektes
???????? ist kein Vielfaches der L?nge des k?rzeren Objektes
Browse[1]> str(TotalVector)
?int [1:10308] 3032 3048 3075 2978 3026 3012 2933 2987 3063 3038 ...
Browse[1]> str(datalist2[[1]]$dataset$Results[[j]]$Results[[time]]$Sweep)
?int [1:10307] 2 1 3 1 5 6 3 1 0 2 ...


as you can see the two vectors differ only in one element. As the sample is quite large it would be the same if I ignore the one extra element.
There are times though that the missing elements can be 2 or 3 (but always the number is small enough so to be ignored)


The major concern is that this "difference" can be either on the fist vector or either on the second vector. If I try to solve that with simple if statements the code gets too much of spaghetti... Is there a simple way when there is this length difference 


either to 


a. Ignore the extra elements
-or-

b. Add the elements missing to the vector with the smaller length( one can just duplicate some of the existing values to reach the needed length)

How I can do either a or b?

I would like to thank you in advance for your help

Regards
Alex

??? [[alternative HTML version deleted]]


______________________________________________
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.
#
Hi

Your description is quite long but almost uninformative about what you 
really want. 

You do not say which values you want to sum but you say it is completely 
equal which value you want to add to what and what shall be the final 
vector length

Based on this I would just use simple "+"

x<-1:10
y<-1:9
x+y
 [1]  2  4  6  8 10 12 14 16 18 11
Warning message:
In x + y : longer object length is not a multiple of shorter object length

warning message is not an error and tells you that the shorter vector is 
recycled e.g. 1,2 or 3 elements are used twice for the calculation. 
Therefore the last value is 11 which is 10 from x vector + 1 from y 
vector.

If you have some other constrains and failed to tell us please do it to 
get some better suited answer.

Regards
Petr
(datalist2[[1]]$dataset$Results[[j]]$Results[[time]]$Sweep) :
str(datalist2[[1]]$dataset$Results[[j]]$Results[[time]]$Sweep)
always
simple
way
can
http://www.R-project.org/posting-guide.html
#
Is the Index of what you cut off or add unimportant or do you have indices on which to compare them?

If you just want to have the same length, not depending on any indices:
 
v1<-1:9
v2<-1:10
minlength<-min(length(v1),length(v2))
v1[1:minlength]+v2[1:minlength]

If indices are important:

names(v1)<-1:9
names(v2)<-1:10
df1<-data.frame(v1,n=names(v1))
df2<-data.frame(v2,n=names(v2))

merge(df1,df2,by.x="n",by.y="n")
rowSums(merged[,-1])
On 03.08.2012, at 15:13, Alaios wrote: