I have a data.frame. It looks like this:
name state
Lily NY
Tom NY,NJ,
John PA,NJ
David SC,PA,NY,
Jim FL,PA
......
There are more than 100 rows. I just want to remove the last comma in each string if there is. My goal is not to remove all the last character.
Remove Commas From String Using the re Package in Python In the re pacakge of Python, we have sub() method, which can also be used to remove the commas from a string. It replaces all the , in the string my_string with "" and removes all the commas in the string my_string .
The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.
Use a regular expression? Assuming your data frame is DF
:
DF$state <- gsub(",$", "", DF$state)
The regular expression ,$
means every comma that occurs at the end of a string. The command gsub
replaces every instance of the first argument with the second argument (in this case, nothing) that occurs in the third argument (DF$state
).
With R 3.6.0
, we can also use trimws
with whitespace
parameter specifying the ,
DF$state <- trimws(DF$state, whitespace = ",")
DF$state
#[1] "NY" "NY,NJ" "PA,NJ" "SC,PA,NY" "FL,PA"
DF <- structure(list(name = c("Lily", "Tom", "John", "David", "Jim"
), state = c("NY", "NY,NJ,", "PA,NJ", "SC,PA,NY,", "FL,PA")),
class = "data.frame", row.names = c(NA, -5L))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With