I have 2 vectors
x <- c(2,2,5) y <- c(1,2)
I want to add each element of the vectors together to get
[1] 3 3 6 4 4 7
How can i do this?
We can use outerwith FUNas+
outer
FUN
+
c(outer(x, y, `+`)) #[1] 3 3 6 4 4 7
You can try to create each pair of x / y elements with expand.grid, and then calculate the sums of the lines:
expand.grid
rowSums(expand.grid(x, y)) # [1] 3 3 6 4 4 7
rep +:
rep
rep(x, length(y)) + rep(y, each=length(x)) [1] 3 3 6 4 4 7
+ rep, y, x.
:
as.vector(sapply(y,function(i) (i+x)))
Source: https://habr.com/ru/post/1671237/More articles:Fatal error: nil unexpectedly found while deploying optional value in UITableViewCell - iosMAMP Pro mysql cannot start - mysqlΠΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π²Π°ΠΆΠ΄Ρ ΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΡ GCM PUSH, ΠΊΠΎΠ³Π΄Π° ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π·Π°ΠΊΡΡΡΠΎ, ΠΈΠ»ΠΈ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π½Π°Ρ ΠΎΠ΄ΠΈΡΡΡ Π² ΡΠΎΠ½ΠΎΠ²ΠΎΠΌ ΡΠ΅ΠΆΠΈΠΌΠ΅ - androidThe scope of the cfr variable. Pluralsight C # test - scopeAndroid ΡΠ°ΡΡΡΠΈΡΡΠ²Π°Π΅Ρ Π΄Π½ΠΈ ΠΌΠ΅ΠΆΠ΄Ρ Π΄Π²ΡΠΌΡ Π΄Π°ΡΠ°ΠΌΠΈ - javaInstall git username and email without using .gitconfig? - gitRight join without intersection and another condition - sqlrouterLink syntax when configuring multiple router routers (primary + aux) - angularΠΠ°ΠΊΠ°Ρ Π½Π°ΠΈΠ»ΡΡΡΠ°Ρ ΠΏΡΠ°ΠΊΡΠΈΠΊΠ° ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ ΡΡΠ°ΡΠΈΡΠ΅ΡΠΊΠΎΠΉ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ Π² Π‘# Unity - c#Actioncable broadcast Π½Π΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ Ρ ΠΊΠΎΠ½ΡΠΎΠ»ΠΈ Π² Rails - ruby-on-railsAll Articles