Actually this might be shorter for working with indices: v1[names(v1)%in%names(v2)]+v2[names(v2)%in%names(v1)]
On 03.08.2012, at 15:41, Petr PIKAL wrote: > 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 > >> >> 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@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. > > ______________________________________________ > 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]]
______________________________________________ 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.