我正在寻找一些帮助,可以在数据框中创建来自多个名称变量的电子邮件。想知道是否有快速方法用正则表达式做到这一点?请注意,需要包含id。例如,if id == 1, return an email address is the following format: tim.c.smith@email.com.
非常感谢任何人提供的任何帮助。
#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("tim.c.smith@email.com","mary.jane@email.com")
end_df = data.frame(id,firstnm,midnm,lastnm,email)
答案 0 :(得分:1)
我们可以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] "tim.c.smith@email.com" "mary.jane@email.com"