I have a correlation matrix that I put in a dataframe like so:
row | var1 | var2 | cor
1 | A | B | 0.6
2 | B | A | 0.6
3 | A | C | 0.4
4 | C | A | 0.4
These results are duplicated into 2 rows each, with both combinations of "var1" and "var2". I only need one, preferably with the lower variable first (e.g. rows 1 and 3).
I've been playing with dplyr for two hours and reading old threads, but not finding what I need.
# get correlation of every concept versus every concept
data.cor <- data.jobs %>%
select(-y,-X) %>%
as.matrix %>%
cor %>%
as.data.frame %>%
rownames_to_column(var = 'var1') %>%
gather(var2, value, -var1)
I would like output to look like so:
row | var1 | var2 | cor
1 | A | B | 0.6
3 | A | C | 0.4
I am trying to do this without resorting to a loop.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…