简体   繁体   中英

Simple Addition using sapply in R

I am trying to use sapply to add 0.84 until 1.7 is reached, so that I can avoid using a for loop.

What I already have tried:

my_vector2 <- sapply(-2.5:1.7, function(x) x + 0.84)

I am expecting to see -1.66, -0.82, 0.02, 0.86, 1.7 but the output is -1.66 -0.66 0.34 1.34 2.34 .

What am I missing?

seq() can do what you want:

> seq(-2.5,1.7,by = .84)[-1]
[1] -1.66 -0.82  0.02  0.86  1.70

The point of the [-1] is to throw away the first number, -2.5 . With round-off error, you might need to be careful with the final number as well. Type ?seq at the prompt for additional information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM