Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the difference between all values of two vectors

Tags:

r

vector

I am trying to find the difference between many pairs of numbers.

These numbers are in two vectors of unequal length. For example,

    a<- c(1:5)
    b<- c(1:10)

Now, I need some way of calculating the a[[1]] - b and then a[[2]] - b and so on until a[[5]] - b. Each of these calculations should result in a vector, 10 numbers long.

Each of these vectors of differences should be stored together as columns in a dataframe. The first column should be the position of ´b´ subtracted and the subsequent columns should be titled with the position of ´a´ (so there are 5 columns and 10 rows).

         a[1] a[2] ... a[5]
    b[1]
    b[2]
    ...
    b[10]

I am very new to writing functions in R. I am also new to using the *apply group of functions. I have been trying to combine what I have learned about writing functions and the *apply group of functions to solve this but it just isn´t happening yet. Thank you for your help!

P.S. Sorry if this has been asked before. I searched but could not find the answer.

like image 285
Jim Fredrickson Avatar asked Jan 24 '18 12:01

Jim Fredrickson


People also ask

How do you find the difference between two vectors?

The difference of two vector x=b−a is formed by placing the tails of the two vectors together. Then, the vector x goes from the head of a to the tail of b.

How do you find the difference between vectors in R?

Method 1: Using setdiff() method The setdiff() method in R is used to retrieve the elements of vector X, which are not contained in Y. This method can be applied where the two vectors may belong to different data types, as well, where the elements of the first argument vector are returned unmodified.

How do I compare two vectors by an element in R?

intersect() function is used to return the common element present in two vectors. Thus, the two vectors are compared, and if a common element exists it is displayed.

How do I check if two vectors match in R?

setequal() function in R Language is used to check if two objects are equal. This function takes two objects like Vectors, dataframes, etc. as arguments and results in TRUE or FALSE, if the Objects are equal or not.


2 Answers

Its a job for outer:

t(outer(a, b, '-'))

     # [,1] [,2] [,3] [,4] [,5]
 # [1,]    0    1    2    3    4
 # [2,]   -1    0    1    2    3
 # [3,]   -2   -1    0    1    2
 # [4,]   -3   -2   -1    0    1
 # [5,]   -4   -3   -2   -1    0
 # [6,]   -5   -4   -3   -2   -1
 # [7,]   -6   -5   -4   -3   -2
 # [8,]   -7   -6   -5   -4   -3
 # [9,]   -8   -7   -6   -5   -4
# [10,]   -9   -8   -7   -6   -5
like image 132
989 Avatar answered Sep 21 '22 14:09

989


sapply(a, "-", b)
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    0    1    2    3    4
# [2,]   -1    0    1    2    3
# [3,]   -2   -1    0    1    2
# [4,]   -3   -2   -1    0    1
# [5,]   -4   -3   -2   -1    0
# [6,]   -5   -4   -3   -2   -1
# [7,]   -6   -5   -4   -3   -2
# [8,]   -7   -6   -5   -4   -3
# [9,]   -8   -7   -6   -5   -4
#[10,]   -9   -8   -7   -6   -5

Explanation

Taking advantage of the fact that a scalar minus a vector in R is an element-wise subtraction between said scalar and each element of the vector, we can simply apply the minus - operator to each value in a against the whole vector b.

like image 34
catastrophic-failure Avatar answered Sep 22 '22 14:09

catastrophic-failure