I am trying to sort the Sex column. When I try to sort, I get all Male and all Female together. But I want to sort the Sex column between the countries. Also, please note the IND should be at the bottom of the country.
Suppose, the dataset is
Country<- c("France","France", "Germnay", "Germnay", "UK", "UK", "IND", "IND")
Sex <-c("Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male")
A<- c(10, 20, 30 ,40 ,50 ,60 ,70 ,80)
B <- c(20, 30, 40, 50, 56, 84, 75, 85)
dt <- data.frame(Country, Sex, A, B)
When I try sorting with order()
, I get the below output (screenshot is an example not with the same dataset)
dt <-dt[with(dt, order(dt$sex, decreasing = TRUE)),]
The desired output is
Country<- c("France","France", "Germnay", "Germnay", "UK", "UK", "IND", "IND")
Sex <-c("Male", "Female", "Male", "Female", "Male", "Female", "Male", "Female)
A<- c(10, 20, 30 ,40 ,50 ,60 ,70 ,80)
B <- c(20, 30, 40, 50, 56, 84, 75, 85)
output <- data.frame(Country, Sex, A, B)