Reputation: 41
How can I change all names like: Finland:Aland Islands:Foglo into only: Finland? I tried using lapply(gsub , pattern = "Finland*", replace = "Finland") But it won't work.
I would also do the same with other countries, like Germany, Denmark.. e.t.c. And also, is there a way to order these countries alphabetically in a list?
Thank you in advance
1 Finland:Aland Islands:Foglo
2 Finland:Aland Islands:Eckero
3 Finland:Aland Islands:Fasta Aland
4 Austria
5 Belgium
6 Bulgaria
7 Switzerland
8 Cyprus:Northern Cyprus
9 Cyprus
10 Czech Republic
11 Germany:Usedom
12 Germany:Fehmarn
13 Germany:Rugen
14 Germany:4
15 Germany
16 Germany:6
17 Denmark:Lolland
18 Denmark:2
19 Denmark:Mon
20 Denmark:Als
21 Denmark:Langeland
22 Denmark:Bornholm
23 Denmark:Fyn
24 Denmark:8
25 Denmark:Samso
26 Denmark:Sjaelland
27 Denmark:Laeso
28 Denmark
29 Spain:Formentera
30 Spain:Ibiza
31 Spain:Majorca
32 Spain:Minorca
33 Spain
34 Estonia:Saaremaa
35 Estonia:Muhu
36 Estonia:Hiiumaa
37 Estonia
38 Finland:1
39 Finland:2
40 Finland:3
41 Finland:4
42 Finland:5
43 Finland:Vallgrund
44 Finland:Halluoto
45 Finland
46 France:Corsica
47 France:Ile d'Oleron
48 France
49 UK:Isle of Wight
50 UK:Wales:Anglesey
Upvotes: 0
Views: 38
Reputation: 146224
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
list
class result??. Do you want to include duplicates? Are there other columns that need to stay with the corresponding rows??sort
and ?order
or maybe just the FAQ on [How to sort a data frame?].Have a look at the resources I mention, have a try, and if you're still having trouble ask a new question.
Upvotes: 3
Reputation: 96
test<-c("Finland:Aland Islands:Foglo","Germany","Finland:Aland Islands:Eckero")
test<-sort(gsub(":.*","",test))
Upvotes: 0