0

I have 3 column for first, middle, and last names in a dataframe. If I want to combine the 3 columns into 1 fullname column how do I do it? For example if I have

df$firstname <- c("Mike","Jake", "Bob")
df$middlename <- c("John", "Jay", "Peter")
df$lastname <- c("Turner", "Larson", "Simmons")

How do I get

> df$fullname
Mike John Turner
Jake Jay Larson
Bob Peter Simmons
David Arenburg
  • 91,361
  • 17
  • 137
  • 196

1 Answers1

2

We can use paste

df$fullname <- do.call(paste, df)

data

df <- structure(list(firstname = structure(c(3L, 2L, 1L), 
.Label = c("Bob", 
"Jake", "Mike"), class = "factor"), middlename = structure(c(2L, 
1L, 3L), .Label = c("Jay", "John", "Peter"), class = "factor"), 
lastname = structure(c(3L, 1L, 2L), .Label = c("Larson", 
"Simmons", "Turner"), class = "factor")), .Names = c("firstname", 
"middlename", "lastname"), row.names = c(NA, -3L),
class = "data.frame")
akrun
  • 874,273
  • 37
  • 540
  • 662