-1

I have a column of a data frame, Game, made up of the following: (No quotation marks)

"Euro Round 1 West"
"Euro Round 2 Broncos"        
"Euro Round 3 Tigers"      
"Euro Round 4 Tigers"      
"Euro Round 5 Broncos"       
"Euro Round 6 West"     
"Premier12 Round 1 Eagles"  
"Premier12 Round 10 Mac"   
"Premier12 Round 11 Cats"  
"Premier12 Round 12 Owls"  
"Premier12 Round 13 Devils"   
"Premier12 Round 14 Zebras"     
"Premier12 Round 15 Cats"   
"Premier12 Round 16 Zebras"     
"Premier12 Round 17 Owls" 
"Premier12 Round 2 Eagles" 
 ...

I would like the DataFrame to be ordered by this column.

First alphabetically by 'Euro' or 'Premier12' and then numerically by 'Round _'

However as you can see above the second part, i.e numerical order is like so: 1, 10, 11, 12, 13, 14, 15, 16, 17, 2, 3, 4, ...

How can I fix this?

Also, would it be possible to have this but also to change the characters order and add in brackets like so:

"West (Euro Round 1)"
"Broncos (Euro Round 2)"        
"Tigers (Euro Round 3)" 
"Tigers (Euro Round 4)"      
"Broncos (Euro Round 5)"       
"West (Euro Round 6)"     
"Eagles (Premier12 Round 1)"  
"Eagles (Premier12 Round 2)"   
"Devils (Premier12 Round 3)"  
... 

Or else:

"West, Euro Round 1"
"Broncos, Euro Round 2"        
"Tigers, Euro Round 3" 
"Tigers, Euro Round 4"      
"Broncos, Euro Round 5"       
"West, Euro Round 6"     
"Eagles, Premier12 Round 1"  
"Eagles, Premier12 Round 2"   
"Devils, Premier12 Round 3"  
... 

Thanks

I've read How to sort a character vector where elements contain letters and numbers in R? , which is similar to my problem, but I can't figure it out

Community
  • 1
  • 1

1 Answers1

1

first make a list of all your names

mylist <- list()

put the names in the list

mylist <- "Premier12 Round 16 Zebras" 

the function you are looking for is strsplit

name_string <- strsplit(mylist[1], " ")

the above will split your names at the spaces.

simply pick he 3rd word in each vector to get the number.

name_string[3]

loop across your list.

make an empty dataframe

mydata <- data.frame()

rownames(mydata) <- c(1:20)

store each name in its appropriate rowname as you loop

the opposite of strsplit is paste0() . this is where you can insert brackets

Alex Bădoi
  • 830
  • 2
  • 9
  • 24