[R] Creating a dataframe from a vector of character strings

From: Cliff Clive <cliffclive_at_gmail.com>
Date: Thu, 14 Apr 2011 14:04:49 -0700 (PDT)

I have a vector of character strings that I would like to split in two, and place in columns of a dataframe.

So for example, I start with this:

beatles <- c("John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr")

and I want to end up with a data frame that looks like this:

> Beatles = data.frame(firstName=c("John", "Paul", "George", "Ringo"),

                       lastName=c("Lennon", "McCartney", "Harrison",
> Beatles

  firstName lastName
1      John    Lennon
2      Paul McCartney
3    George  Harrison
4     Ringo     Starr

I tried string-splitting the first vector on the spaces between first and last names, and it returned a list:

> strsplit(beatles, " ")

[1] "John" "Lennon"

[1] "Paul" "McCartney"

[1] "George" "Harrison"

[1] "Ringo" "Starr"

Is there a fast way to convert this list into a data frame? Right now all I can think of is using a for loop, which I would like to avoid, since the real application I am working on involves a much larger dataset.

View this message in context: http://r.789695.n4.nabble.com/Creating-a-dataframe-from-a-vector-of-character-strings-tp3450716p3450716.html
Sent from the R help mailing list archive at Nabble.com.

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 14 Apr 2011 - 21:36:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 14 Apr 2011 - 23:10:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive