How to sort letters in a string?

Maybe not the most simple answer, but this will work: paste(sort(unlist(strsplit(s, “”))), collapse = “”) Or modify the strReverse function that is defined in the help page for ?strsplit to suit our needs. We’ll call it strSort: strSort <- function(x) sapply(lapply(strsplit(x, NULL), sort), paste, collapse=””)

rbind dataframes with a different column name

My favourite use of mapply: Example Data a <- data.frame(a=runif(5), b=runif(5)) > a a b 1 0.8403348 0.1579255 2 0.4759767 0.8182902 3 0.8091875 0.1080651 4 0.9846333 0.7035959 5 0.2153991 0.8744136 and b b <- data.frame(c=runif(5), d=runif(5)) > b c d 1 0.7604137 0.9753853 2 0.7553924 0.1210260 3 0.7315970 0.6196829 4 0.5619395 0.1120331 5 0.5711995 0.7252631 … Read more

Custom sorting (non-alphabetical)

The levels should be specified explicitly: A$animal <- factor(A$animal, levels = c(“dog”, “elephant”,”cat”)) A$color <- factor(A$color, levels = c(“green”, “blue”, “red”)) Then you order by the 2 columns simultaneously: A[order(A$animal,A$color),] # animal color # 6 dog green # 4 dog blue # 5 dog red # 9 elephant green # 7 elephant blue # 8 … Read more