Neil Neil - 5 months ago 31
R Question

how to substring text from last

I have following column in r dataframe

01.01.2017 -SS DPR.xlsx
02.01.2017 -SS DPR.xlsx
03.01.2017 -SS DPR.xlsx
04.01.2017 -SS DPR.xlsx
05.01.2017 -SS DPR.xlsx
06.01.2017 -SS DPR.xlsx

I want to extract only names not the extension from above column.

01.01.2017 -SS DPR
02.01.2017 -SS DPR
03.01.2017 -SS DPR
04.01.2017 -SS DPR
05.01.2017 -SS DPR
06.01.2017 -SS DPR

How can I subset the dataframe from last 4 characters in r?

Answer Source

we can use sub

df1$file_name <- sub("\\.xlsx", "", df1$file_name)

Or use the file_path_sans_ext from tools

df1$file_name <- tools::file_path_sans_ext(df1$file_name)
#[1] "01.01.2017 -SS DPR" "02.01.2017 -SS DPR" "03.01.2017 -SS DPR" 
#[4] "04.01.2017 -SS DPR" "05.01.2017 -SS DPR"
#[6] "06.01.2017 -SS DPR"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download