R programming to subtract all values in a vector from all values in another vector.



To subtract all values in a vector from all values in another vector in R, we can use sapply function with subtraction sign.

For Example, if we have two vectors say X and Y and we want to subtract all values in Y from all values in X then we can use the command given below −

sapply(X,"-",Y)

Example 1

Following snippet creates a sample data frame −

x1<-1:10 y1<-10:1 sapply(x1,"-",y1)

Output

If you execute the above given snippet, it generates the following Output −

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] -9 -8 -7 -6 -5 -4 -3 -2 -1 0 [2,] -8 -7 -6 -5 -4 -3 -2 -1 0 1 [3,] -7 -6 -5 -4 -3 -2 -1 0 1 2 [4,] -6 -5 -4 -3 -2 -1 0 1 2 3 [5,] -5 -4 -3 -2 -1 0 1 2 3 4 [6,] -4 -3 -2 -1 0 1 2 3 4 5 [7,] -3 -2 -1 0 1 2 3 4 5 6 [8,] -2 -1 0 1 2 3 4 5 6 7 [9,] -1 0 1 2 3 4 5 6 7 8 [10,] 0 1 2 3 4 5 6 7 8 9

Example 2

Following snippet creates a sample data frame −

x2<-sample(0:9,10) x2

If you execute the above given snippet, it generates the following Output −

[1] 2 3 0 8 5 6 9 4 1 7 

To subtract all values in a vector from all values in another vector in R, add the following code to the above snippet −

x2<-sample(0:9,10) y2<-sample(0:9,20,replace=TRUE) y2

If you execute the above given snippet, it generates the following Output −

[1] 9 9 6 9 7 1 8 3 6 4 8 5 2 0 0 9 9 3 2 2 

To subtract all values in a vector from all values in another vector in R, add the following code to the above snippet −

x2<-sample(0:9,10) y2<-sample(0:9,20,replace=TRUE) sapply(x2,"-",y2)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]  [1,] -7   -6   -9   -1   -4   -3    0   -5   -8   -2  [2,] -7   -6   -9   -1   -4   -3    0   -5   -8   -2  [3,] -4   -3   -6    2   -1    0    3   -2   -5    1  [4,] -7   -6   -9   -1   -4   -3    0   -5   -8   -2  [5,] -5   -4   -7    1   -2   -1    2   -3   -6    0  [6,]  1    2   -1    7    4    5    8    3    0    6  [7,] -6   -5   -8    0   -3   -2    1   -4   -7   -1  [8,] -1    0   -3    5    2    3    6    1   -2    4  [9,] -4   -3   -6    2   -1    0    3   -2   -5    1 [10,] -2   -1   -4    4    1    2    5    0   -3    3 [11,] -6   -5   -8    0   -3   -2    1   -4   -7 -1 [12,] -3   -2   -5    3    0    1    4   -1   -4 2 [13,]  0    1   -2    6    3    4    7    2 -1 5 [14,]  2    3    0    8    5    6    9    4 1 7 [15,]  2    3    0    8    5    6    9    4 1 7 [16,] -7   -6   -9   -1   -4   -3    0   -5 -8 -2 [17,] -7   -6   -9   -1   -4   -3    0   -5 -8 -2 [18,] -1    0   -3    5    2    3    6    1 -2 4 [19,]  0    1   -2    6    3    4    7    2 -1 5 [20,]  0    1   -2    6    3    4    7    2 -1 5

Example 3

Following snippet creates a sample data frame −

x3<-rpois(2,5) x3

If you execute the above given snippet, it generates the following Output −

[1] 5 2 

To subtract all values in a vector from all values in another vector in R, add the following code to the above snippet −

x3<-rpois(2,5) y3<-rpois(20,1) y3

If you execute the above given snippet, it generates the following Output −

[1] 0 1 1 0 0 0 1 1 1 0 0 0 2 1 0 1 2 0 1 1 

To subtract all values in a vector from all values in another vector in R, add the following code to the above snippet −

x3<-rpois(2,5) y3<-rpois(20,1) sapply(x3,"-",y3)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

 [,1] [,2] [1,] 5 2 [2,] 4 1 [3,] 4 1 [4,] 5 2 [5,] 5 2 [6,] 5 2 [7,] 4 1 [8,] 4 1 [9,] 4 1 [10,] 5 2 [11,] 5 2 [12,] 5 2 [13,] 3 0 [14,] 4 1 [15,] 5 2 [16,] 4 1 [17,] 3 0 [18,] 5 2 [19,] 4 1 [20,] 4 1

Example 4

Following snippet creates a sample data frame −

x4<-sample(1:20,5) x4

If you execute the above given snippet, it generates the following Output −

[1] 15 4 9 8 3 

To subtract all values in a vector from all values in another vector in R, add the following code to the above snippet −

x4<-sample(1:20,5) y4<-sample(1:20,15) y4

If you execute the above given snippet, it generates the following Output −

[1] 19 16 1 9 8 4 2 12 3 18 13 5 20 10 6 

To subtract all values in a vector from all values in another vector in R, add the following code to the above snippet −

x4<-sample(1:20,5) y4<-sample(1:20,15) sapply(x4,"-",y4)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

 [,1] [,2] [,3] [,4] [,5] [1,] -4 -15 -10 -11 -16 [2,] -1 -12 -7 -8 -13 [3,] 14 3 8 7 2 [4,] 6 -5 0 -1 -6 [5,] 7 -4 1 0 -5 [6,] 11 0 5 4 -1 [7,] 13 2 7 6 1 [8,] 3 -8 -3 -4 -9 [9,] 12 1 6 5 0 [10,] -3 -14 -9 -10 -15 [11,] 2 -9 -4 -5 -10 [12,] 10 -1 4 3 -2 [13,] -5 -16 -11 -12 -17 [14,] 5 -6 -1 -2 -7 [15,] 9 -2 3 2 - 3
Updated on: 2021-11-08T08:10:30+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements