Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
993 views
in Technique[技术] by (71.8m points)

lapply - How can I change all the names in a list to one in R?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ds.

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

  1. 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?
  2. 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?].
  3. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...