gsub
is vectorized so you don't need lapply
(unless you are wanting to apply the function to multiple columns). In regex, *
is a quantifier indicating 0 or more of the thing that comes before it, so the pattern "Finland*"
matches "Finlan"
followed by any number of d
s.
Instead, let's replace the first :
and anything after it .*
with an empty string. (.
in regex means "any character", so .*
is "any number of any characters".)
gsub(pattern = ":.*", replacement = "", x = your_data$your_column)
And also, is there a way to order these countries alphabetically in a list?
I'm going to mostly ignore this as a separate question for a few reasons
- It's not clear what you mean -- do you really want a
list
class result??. Do you want to include duplicates? Are there other columns that need to stay with the corresponding rows?
- It's not clear you've tried anything. Have a look at
?sort
and ?order
or maybe just the FAQ on [How to sort a data frame?].
- It's unrelated to the main question, and it's bad practice to combine unrelated questions.
Have a look at the resources I mention, have a try, and if you're still having trouble ask a new question.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…