I've made like this, I hope you'll get the logic ! Tell me if it's what you want
first = c('John','Michael',"Harry","Stephen","Simon",'Rachael',"Paul")
last = c("smith","Johnson","Williams","Jones","Adams","Moore","Taylor")
format = c("first.last","firstlast","last.first","f.last","flast","f_last","f_last")
names = data.frame(cbind(first,last,format))
names$first = as.character(names$first)
names$last = as.character(names$last)
names$format = as.character(names$format)
library(stringr)
for (i in 1:dim(names)[1]){
if (names[i,"format"] == "first.last"){
names[i,"new_var"] = paste(tolower(names[i,"first"]),tolower(names[i,"last"]), sep = '.')
}else if (names[i,"format"] == "firstlast"){
names[i,"new_var"]= paste(tolower(names[i,"first"]),tolower(names[i,"last"]), sep = '')
}else if (names[i,"format"] == "last.first"){
names[i,"new_var"] = paste(tolower(names[i,"last"]),tolower(names[i,"first"]), sep = '.')
}else if (names[i,"format"] == "f.last"){
names[i,"new_var"] = paste(tolower(str_sub(names[i,"first"],1,1)),tolower(names[i,"last"]),sep=".")
}else if (names[i,"format"] == "flast"){
names[i,"new_var"] = paste(tolower(str_sub(names[i,"first"],1,1)),tolower(names[i,"last"]),sep="")
}else{
names[i,"new_var"] = paste(tolower(str_sub(names[i,"first"],1,1)),tolower(names[i,"last"]),sep="_")
}
}
names
first last format new_var
1 John smith first.last john.smith
2 Michael Johnson firstlast michaeljohnson
3 Harry Williams last.first williams.harry
4 Stephen Jones f.last s.jones
5 Simon Adams flast sadams
6 Rachael Moore f_last r_moore
7 Paul Taylor f_last p_taylor
>