Created
October 27, 2013 15:34
-
-
Save mrdwab/7183841 to your computer and use it in GitHub Desktop.
Shuffle items in a character string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
moveme <- function(invec, movecommand) { | |
movecommand <- lapply(strsplit(strsplit(movecommand, ";")[[1]], ",|\\s+"), | |
function(x) x[x != ""]) | |
movelist <- lapply(movecommand, function(x) { | |
Where <- x[which(x %in% c("before", "after", "first", "last")):length(x)] | |
ToMove <- setdiff(x, Where) | |
list(ToMove, Where) | |
}) | |
myVec <- invec | |
for (i in seq_along(movelist)) { | |
temp <- setdiff(myVec, movelist[[i]][[1]]) | |
A <- movelist[[i]][[2]][1] | |
if (A %in% c("before", "after")) { | |
ba <- movelist[[i]][[2]][2] | |
if (A == "before") { | |
after <- match(ba, temp)-1 | |
} else if (A == "after") { | |
after <- match(ba, temp) | |
} | |
} else if (A == "first") { | |
after <- 0 | |
} else if (A == "last") { | |
after <- length(myVec) | |
} | |
myVec <- append(temp, values = movelist[[i]][[1]], after = after) | |
} | |
myVec | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment