Cross-difference between arrays without using for loop

Using the following code:

a <- seq(1, 10, 1)
b <- seq(2, 20, 2)

I would like to subtract a[i - 1] from b[i] for each i, in order to obtain something like

c <- NULL
for(i in 1:length(b)) {
   c[i] <- b[i] - a[i - 1]

but I would like to do this without using for() loop.

Anyone knows how to do it in just one command line?


Since your a and b are the same length, I've assumed you'd like to first trim the last element off of b. (Try b - a[-1] to see why that's probably desirable.)

b[-length(b)] - a[-1]
# [1] 0 1 2 3 4 5 6 7 8

Need Your Help

ruby: `read': Invalid argument -(Errno::EINVAL) at

ruby directory crc crc32

I'm doing a simple script to check crc of all files...

Update UIPickerView Number of Rows

iphone objective-c nsmutablearray uipickerview

I have a UIPickerView that is populated by an NSMutableArray called sectionNamesArray. When an item is added to sectionNamesArray, how do I manually get this delegate to be called?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.