I need to re-order some of the rows of my dataframe. I have a list of observations (names) that I need to be at the top. It’s a string variable, and just a selection of them need to be at the top–the rest can be in their existing order.
Example dataset:
df <- data.frame(name = c("Alan", "Betty", "Clyde", "Diane", "Eric", "Francine", "George", "Harriett"), height = c(60, 68, 70, 66, 68, 70, 63, 64))
I can’t order from lowest to highest or vice versa, and there’s no part of the string that is unique to these observations. I only care about the order for some of the observations, not all. And for the ones I do care about, they have to be in a specific order – it’s not enough for them to be in the top in the wrong order.
Long version:
df$order[df$name == "Betty"] <- 1
df$order[df$name == "Diane"] <- 2
df$order[df$name == "Alan"] <- 3
df$order[df$name == "Clyde"] <- 4
df <- arrange(df, order)
df$order <- NULL
The above code works, but there are too many observations for me to feel comfortable copy/pasting a bunch of lines.
I tried doing the above code as a for loop (looping either over a list of the names or 1:4), but I couldn’t get it to work. Similar for ifelse(). lapply or sapply might work, but frankly I don’t understand them.
I’m hoping someone can show me how to loop/automate my solution so it’s doable for more observations or show me a different way altogether.