Unique elements of two vectors
It sounds like you're looking for union
:
> union(v1, v2)
[1] 1 2 3 4 5 6 10 11
Finding elements that do not overlap between two vectors
Yes, there is a way:
setdiff(list.a, list.b)
# [1] "Mary" "Jack" "Michelle"
Get the unique values of two vectors keeping the order of both original
For this example the next code works. One first has to define auxiliar vectors w1
, w2
depending on which has the first common element and another vector w
on which to append the lacking elements by order.
It would be clearer using a for
loop, which would avoid this cumbersome code, but at first, this is faster and shorter.
w <- w1 <- unlist(ifelse(intersect(v1,v2)[1] == v1[1], list(v2), list(v1)))
w2 <- unlist(ifelse(intersect(v1,v2)[1] == v1[1], list(v1), list(v2)))
unique(lapply(setdiff(w2,w1), function(elmt) w <<- append(w, elmt, after = match(w2[match(elmt,w2)-1],w)))[[length(setdiff(w2,w1))]])
[1] "Z" "A" "T" "F" "Q" "D"
Unique element pairing between two vectors maximizing the overall sum
Well, I eventually found the solution based on the Hungarian algorithm implemented in the solve_LSAP
function of the clue
R package. To have it working, transform your df
in a matrix like so:
df = matrix(sapply(df$score, function(x) x), nrow=length(unique(df$Var1)), ncol=length(unique(df$Var2)), dimnames = list(unique(df$Var1), unique(df$Var2)))
and apply the function
df.res = solve_LSAP(df, maximum = T)
> df.res
Optimal assignment:
1 => 2, 2 => 3, 3 => 1
and then get back the actual nodes or names
df.res = cbind(rownames(df), colnames(df)[df.res])
> df.res
[,1] [,2]
[1,] "A" "C"
[2,] "B" "D"
[3,] "C" "A"
>
Tadaaaaam!
how to identify the number of unique elements in two sparate vector?
union
and length
are both built-in to Julia. And union
is a set union which removes duplicates, even when applied to vectors. Consider
println(length(union(A, B)))
or, if you're feeling particularly snazzy
println(length(A ∪ B))
sample unique pairs from two vectors
You can make it a function and throw replace
in there, i.e.
f1 <- function(a, b){
m <- cbind(sample(c(a, a)), sample(c(b, b, sample(b, 2, replace = TRUE))))
m[,2] <-replace(m[,2], duplicated(m), sample(b[!b %in% m[duplicated(m),2]], 1))
return(m)
}
#which seems stable
sum(duplicated(f1(a, b)))
#[1] 0
sum(duplicated(f1(a, b)))
#[1] 0
sum(duplicated(f1(a, b)))
#[1] 0
sum(duplicated(f1(a, b)))
#[1] 0
Related Topics
Replacing the Duplicate Values Except 1 Row in R Dataframe
Writings Functions (Procedures) for Data.Table Objects
R Plot Color Combinations That Are Colorblind Accessible
Download Attachment from an Outlook Email Using R
Is It Bad Practice to Access S4 Objects Slots Directly Using @
Geom_Bar() + Pictograms, How To
How to Create a Range of Dates in R
Display an Axis Value in Millions in Ggplot
How to Produce Time Series for Each Row of a Data Frame with an Unnamed First Column
How to Learn How to Write C Code to Speed Up Slow R Functions
Reordering Columns in a Large Dataframe
R - Common Title and Legend for Combined Plots