How to remove undesired space before certain observations in a dataframe column – General

Hi,

So I have an excel file with a column that is peoples surnames. Certain entries have an undesired blank space at the start of the entry. I want to remove this space from all the impacted entries. (Not real data, only for demo purposes)

So I have read in the excel file to R and stored the data in a data frame called EU_DATA. No issues here.

I now want to remove the blank space from the start of the relevant entries

I am using the following code to trim the white space but it doesn’t do anything
EU_DATA$Op_s.name <- trimws(EU_DATA$Op_s.name, which = “left”)

I have als tried the following code using gsub but it also doesn’t do anything
EU_DATA$Op_s.name <- gsub(“^\s+”,””,EU_DATA$Op_s.name)

I then created my own character vector a <- c(” Lyons”,” Murphy”)
and then when I run the above code on the vector a, then it successfully removes the blank spaces. So I am really confused as to what is going on.

Any help in solving this would be much appreciated

Regards,
Colm

Read more here: Source link