generating a vector of difference between two vectors
You are looking for the function setdiff
setdiff(vectorB$id, vectorA$id)
If you did not want this reduced to unique values, you could create a not in
function
(kudos to @joran here Match with negation)
'%nin%' <- Negate('%in%')
vectorB$id[vectorB$id %nin% vectorA$id]
Difference between two vectors in R
You can use setdiff
setdiff(b,a)
#[1] 2 6 8
Find the difference between all values of two vectors
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
.
How to compare 2 vectors and create a separate vector based on the comparison
If, as in your example, the ranges are sorted then you can use std::set_symmetric_difference
. It will return all elements that are not shared between the two ranges. Using you example you would use
std::set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
So putting it all together we have
int main()
{
std::vector<int> v1 = { 1,2,3,4,5,6,7,8,9 };
std::vector<int> v2 = { 1,2,6 };
std::vector<int> v3; //V3 should equal = {3,4,5,7,8,9}.
std::set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
for (auto e : v3)
std::cout << e << " ";
return 0;
}
Output:
3 4 5 7 8 9
Live example
Comparing two vectors and calculating differences to corresponding elements
Calculate the difference between each element to get the time in the corresponding state and then sum over the times ordered by state.
state <- c(0, 1, 0, 1, 2, 1, 0, 1, 2, 3)
time <- c(0.00, 0.03, 0.12, 0.78, 0.87, 0.94, 0.97, 1.18, 1.23, 1.30)
df <- data.frame(state=state,time=time)
df$diff <- c(diff(df$time),0)
tim.p.state <- data.frame(time=sapply(split(df,df$state),function(df){return(sum(df$diff))}))
tim.p.state$proportion <- tim.p.state$time/time[length(time)]
How to generate a random vector between two vectors?
import random
vec3 = vec1 + random.uniform(0, 1) * (vec2 - vec1)
C++ get the difference between two vectors
//from cppreference
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
int main() {
std::vector<int> v1 {1,1,2,3,3,4,5,5,6};
std::vector<int> v2 {1,2,3,4,5,6};
std::vector<int> diff;
//no need to sort since it's already sorted
//but you can sort with:
//std::sort(std::begin(v1), std::end(v1))
std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(),
std::inserter(diff, diff.begin()));
for (auto i : v1) std::cout << i << ' ';
std::cout << "minus ";
for (auto i : v2) std::cout << i << ' ';
std::cout << "is: ";
for (auto i : diff) std::cout << i << ' ';
std::cout << '\n';
}
Related Topics
Doing a Plyr Operation on Every Row of a Data Frame in R
Dplyr - Summary Table for Multiple Variables
How to Break Out of a Foreach Loop
Extract Rgb Channels from a Jpeg Image in R
R Dplyr Filter Not Masking Base Filter
Multiple Functions in a Single Tapply or Aggregate Statement
R Reshape a Vector into Multiple Columns
Create Data Set from Clicks in Shiny Ggplot
How to Get Last Subelement of Every Element of a List
R: Generate All Permutations of Vector Without Duplicated Elements
How to Create, Structure, Maintain and Update Data Codebooks in R
Using Predict with a List of Lm() Objects
How to Change X-Axis Tick Label Names, Order and Boxplot Colour Using R Ggplot
Adding Curved Flight Path Using R's Leaflet Package
How to Self Join a Data.Table on a Condition
Summarise_At Using Different Functions for Different Variables