首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在dataframe中转换字符串

如何在dataframe中转换字符串
EN

Stack Overflow用户
提问于 2019-05-28 17:23:42
回答 1查看 58关注 0票数 0

我有一个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"
EN

回答 1

Stack Overflow用户

发布于 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

然后,我们用交换的姓氏和名字/中间名替换,用逗号分隔。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56338944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档