从变量创建电子邮件R
问题描述:
我正在寻找一些帮助来创建一个数据框中的多个名称变量的电子邮件。想知道是否有快速的方法来使用正则表达式来做到这一点?请注意,该ID需要包括在内。例如,if id == 1, return an email address is the following format: [email protected]
任何人可以提供帮助将不胜感激。从变量创建电子邮件R
#Starting
id = c(1,2)
firstnm = c("tim", "mary")
midnm = c("chris","sally")
lastnm = c("smith","jane")
email = c("","")
st_df = data.frame(id,firstnm,midnm,lastnm,email)
#Ending
id = c(1,2)
firstnm = c("tim", "mary")
midnm = c("chris","sally")
lastnm = c("smith","jane")
email = c("[email protected]","[email protected]")
end_df = data.frame(id,firstnm,midnm,lastnm,email)
答
我们可以做一个ifelse
st_df$email <- with(st_df, ifelse(id==1, paste0(firstnm, ".",
substr(midnm, 1, 1),".", lastnm, "@email.com"),
paste0(firstnm, ".", lastnm, "@email.com")))
st_df$email
#[1] "[email protected]" "[email protected]"
解释为什么蒂姆中间的名字成为一个人物,但玛丽的中间名完全消失。 –
嗨@TimBiegeleisen。对您的问题的简短回答是,某些组ID会使用电子邮件格式,但不会在电子邮件地址中使用中间名,尽管它已发布在数据框中。 – richiepop2
然后你需要提供逻辑,如果你想能够处理这种情况。 –