-1

I have an .xlsx file with 845 rows and 61 columns, and one of the columns is a list of location names. Each cell in this column, however, ends with six unwanted characters like ' (001)', ' (014)' ' (013)', etc..., having a space, brackets and numbers, which i want to delete. Also, the header does not have this issue and I need all other columns intact, so these should not be affected.

enter image description here

I can do this in MS Excel with the following formula

=LEFT(A1,LEN(A1)-6)

(https://www.mrexcel.com/board/threads/how-can-i-remove-the-last-5-characters-from-a-cell.272639/)

But i want to do it in R. (PS - am an amateur at R)

Please help.

N M
  • 1
  • 1
  • 2
  • 1
    Does this answer your question? [Extracting the last n characters from a string in R](https://stackoverflow.com/questions/7963898/extracting-the-last-n-characters-from-a-string-in-r). e.g., looking at [this answer,](https://stackoverflow.com/a/7964027/17303805) you would just use `end = -6` instead of `start = -6`. – zephryl Feb 13 '22 at 13:41

1 Answers1

0

I suggest looking at the answer from another post:

How to remove last n characters from every element in the R vector

I adapted the solution from @nfmcclure to your situation.

#Example dataframe 
df <- structure(list(location = c("Akola (001)", "Jamkhed (014)", "Karjat (013)", 
                                  "Kopargaeon (003)")), class = "data.frame", row.names = c(NA, 
                                                                                            -4L))
#Solution
substr(df$location,1,nchar(df$location)-5)
Mel G
  • 132
  • 1
  • 10