I've looked around and can't seem to find a decent way to solve this issue.
I have a column that has rows of names. I'd like to sort each row alphabetically so that I can later identify rows that have the same names just in different orders.
The data looks like this:
names <- c("John D., Josh C., Karl H.",
"John D., Bob S., Tim H.",
"Amy A., Art U., Wes T.",
"Josh C., John D., Karl H.")
var1 <- rnorm(n = length(names), mean = 0, sd = 2)
var2 <- rnorm(n = length(names), mean = 20, sd = 5)
df <- data.frame(names, var1, var2)
df
names var1 var2
1 John D., Josh C., Karl H. -0.3570142 15.58512
2 John D., Bob S., Tim H. -3.0022367 12.32608
3 Amy A., Art U., Wes T. -0.6900956 18.01553
4 Josh C., John D., Karl H. -2.0162847 16.04281
For example, row 4 would get sorted to look like row 1. Row 2 would get sorted as Bob, John, and Tim.
I've tried sort(df$names)
but that just orders the names in all rows into alphabetical order.