conditional statements - conditionally multiply a vector by another r -
conditional statements - conditionally multiply a vector by another r -
i have next vector
trans<- c(-2,3,10,-5,-2,56,0)
and want multiply each element selection of 2 vectors depending on whether initial number positive or negative
negtrans<-c(1,2,3) postrans<-c(4,5,6,7)
the result should -2 12 50 -10 -6 336 0
the key here maintain order intact
one way is
unsplit(map(`*`, split(trans, trans>=0), list(negtrans, postrans)),trans>=0) #[1] -2 12 50 -10 -6 336 0
r conditional-statements
Comments
Post a Comment