我有一个name
,就像布鲁因中的罗宾或者Loo中的维克多,这些名字都在我的会话中的dataframe
中。我需要将这些名称更改为:
<姓,名中间名>,
所以他们被调转过来了。但我不知道该怎么做。
我知道我可以将separate()
或map()
之类的东西与(tidyverse
的) PURR
一起使用。
数据:
~nr, ~name, ~prodno,
2019001, "Piet de Boer", "lux_zwez",
2019002, "Elly Hamstra", "zuv_vla",
2019003, "Sue Ellen Schilder", "zuv_vla",
2019004, "Truus Janssen", "zuv_vmlk",
2019005, "Evelijne de Vries", "lux_zwez",
2019006, "Berend Boersma", "lux_gras",
2019007, "Marius van Asten", "zuv_vla",
2019008, "Corneel Jansen", "lux_gras",
2019009, "Joke Timmerman", "zuv_vla",
2019010, "Jan Willem de Jong", "lux_zwez",
2019011, "Frederik Janssen", "zuv_vmlk",
2019012, "Antonia de Jongh", "zuv_vmlk",
2019013, "Lena van der Loo", "zuv_qrk",
2019014, "Johanna Haanstra", "lux_gras"
发布于 2019-05-28 17:26:35
我们可以在这里尝试使用sub
:
names <- c("Robin the Bruyne", "Victor from the Loo")
output <- sub("^(.*) ([A-Z][a-z]+)$", "\\2, \\1", names)
output
[1] "Bruyne, Robin the" "Loo, Victor from the"
此方法使用以下模式:
^(.*) capture everything from the start until the last space
([A-Z][a-z]+)$ capture the last name, which starts with a capital
然后,我们用交换的姓氏和名字/中间名替换,用逗号分隔。
https://stackoverflow.com/questions/56338944
复制相似问题