To join two columns in R without including the middle name, you can use the paste()
function along with the gsub()
function. Here's an example:
Suppose you have a data frame where the first name, middle name, and last name are separate columns:
main.r178 chars4 lines
You can create a new column that concatenates the first and last names, but excludes the middle name, like this:
main.r90 chars2 lines
In the gsub()
function, we use a regular expression argument to match and remove any string that contains spaces (i.e. the middle name). The \\s
characters match any whitespace, and .*
matches any characters (except a newline), zero or more times. The paste()
function then concatenates the first and last names with the modified middle name.
The resulting data frame would look like this:
main.r196 chars5 lines
Note that the third row does not have a middle name, and therefore the resulting full_name
is just the concatenation of the first and last names.
gistlibby LogSnag