Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

r

gsub

Imagine we have a list of variable names like following:

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

How can we remove the last part (after ".") in each element so we can get:

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

Note that there might be "." inside the names but I don't want those words to be split.

I was trying to use gsub("\\..*$", "",ls) but it omits everything after the 1st dot. I'm not sure why the $ sign is not working here. Any ideas?

> gsub("\\..*$", "",ls)
[1] "apple"  "orange" "orange" "apple"  "orange" "orange"
like image 337
Hao Avatar asked Mar 31 '15 19:03

Hao


2 Answers

You can try

sub('[.][^.]+$', '', ls)
#[1] "apple"        "orange"       "orange"       "apple.pie"    "orange.juice"
#[6] "orange.juice"
like image 51
akrun Avatar answered Nov 14 '22 21:11

akrun


Given that this is the equivalent of removing a file extension you could use

library(tools)
file_path_sans_ext(ls)
like image 22
mnel Avatar answered Nov 14 '22 21:11

mnel