Reputation: 556
Using the below toy data, how can I sort x if y=a, y=b etc. I prefer to use the sample code to do so but I could not involve an if statement. I belive a loop would be really efficient.
#my data
dataframe <- data.frame(
x = c("apple", "orange", "banana", "strawberry"),
y = c("a", "b", "a", "b"),
)
# my prefered code without if statement
data.frame$x <- factor(
data.frame$x,
levels = c(
"apple", "orange", "banana", "strawberry"),
labels = c(
"apple", "banana", "orange", "strawberry")
)
Upvotes: 0
Views: 41
Reputation: 887118
We can arrange
on a logical vector and then change the levels
library(dplyr)
dataframe1 <- dataframe %>%
arrange(y == 'b', x) %>%
mutate(x = factor(x, levels = unique(x)))
dataframe1$x
#[1] apple banana orange strawberry
#Levels: apple banana orange strawberry
If there are more than 2 unique elements in 'y', then the option is to use factor
on the 'y' with levels
specified in that order
dataframe1 <- dataframe %>%
arrange(factor(y, levels = c('a', 'b')), x) %>%
mutate(x = factor(x, levels = unique(x)))
dataframe <- structure(list(x = c("apple", "orange", "banana", "strawberry"
), y = c("a", "b", "a", "b")), class = "data.frame", row.names = c(NA,
-4L))
Upvotes: 1