regex - Remove the last part of a string after the last "." in R -


imagine have list of variable names following:

ls<-c("apple.mean", "orange.mean", "orange.sd", "apple.pie.mean", "orange.juice.n", "orange.juice.p%") 

how can remove last part (after ".") in each element can get:

"apple" "orange" "orange" "apple.pie" "orange.juice" "orange.juice" 

note there might "." inside names don't want words split.

i trying use gsub("\\..*$", "",ls) omits after 1st dot. i'm not sure why $ sign not working here. ideas?

> gsub("\\..*$", "",ls) [1] "apple"  "orange" "orange" "apple"  "orange" "orange" 

you can try

sub('[.][^.]+$', '', ls) #[1] "apple"        "orange"       "orange"       "apple.pie"    "orange.juice" #[6] "orange.juice" 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -