提取名称并转换为R中的电子邮件地址

时间:2017-05-09 13:57:23

标签: r character

我有以下字符串:"约翰安德鲁托马斯"(约翰之前有4个空位),我需要拆分并连接它,所以我的输出是" John@gmail.com; Andrew@gmail.com; Thomas@gmail.com",我还需要删除所有空格。

我最好的猜测是:

test = unlist(lapply(names, strsplit, split = " ", fixed = FALSE))
paste(test, collapse = "@gmail.com")

但我把它作为输出:

"@gmail.com@gmail.com@gmail.com@gmail.comJohn@gmail.comAndrew@gmail.comThomas"

4 个答案:

答案 0 :(得分:3)

names <- "    John Andrew Thomas"
test <- unlist(lapply(names, strsplit, split = " ", fixed = FALSE))
paste(test[test != ""],"@gmail.com",sep = "",collapse = ";")

对粘贴行进行小幅调整将删除多余的空格,并用分号分隔电子邮件地址。

输出如下:

[1] "John@gmail.com;Andrew@gmail.com;Thomas@gmail.com"

答案 1 :(得分:2)

使用stringr,我们可以使用其str_trim函数来处理您的前导空格,并假设您的字符串为x

library(stringr)

paste(sapply(str_split(str_trim(x), " "), function(i) sprintf("%s@gmail.com", i)), collapse = ";")

这是一个管道版本,因此更容易理解:

library(dplyr)
library(stringr)

x %>%
    # get rid of leading and trailing whitespace
    str_trim() %>%
    # make a list with the elements of the string, split at " "
    str_split(" ") %>%
    # get an array of strings where those list elements are added to a fixed chunk via sprintf
    sapply(., function(i) sprintf("%s@gmail.com", i)) %>%
    # concatenate the resulting array into a single string with semicolons
    paste(., collapse = ";")

答案 2 :(得分:1)

使用基础R的trimws函数的另一种方法

paste0(unlist(strsplit(trimws(names)," ")),"@gmail.com",collapse = ";")
#[1] "John@gmail.com;Andrew@gmail.com;Thomas@gmail.com"

数据

names <- "    John Andrew Thomas"

答案 3 :(得分:1)

使用modprobe的另一个想法:

<img />

给出了:

stringi