Search code examples
rxlsx

How to delete last 6 characters from a column in .xlsx file using R?


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.


Solution

  • 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)