1

I have a column of names in R that are separated by a comma.

For example:

    John, Doe
    Rebecca, Homes

I'd like to separate the first and last names into separate columns.

One additional problem I have is that sometimes there will be a name that doesn't have a comma. For example:

    John, Doe
    Rebecca, Homes
    Organization LLC

I've looked at using strsplit(a, ","), but I get the following error Error in strsplit(wn, ",") : non-character argument.

Here is an example within Stack Convert comma separated entry to columns

Any help regarding solving this simple problem will be greatly appreciated. Thanks.

Community
  • 1
  • 1

1 Answers1

1

In 2 steps :

  1. You can use read.table with fill=TRUE, to read all lines (You can also use readLines)
  2. treat without commas as seprator.

The code is something like this :

aa <- read.table(text='John, Doe
Rebecca, Homes
Organization LLC',sep=',',fill=TRUE,colClasses='character')

## treat lines without comma
aa[nchar(aa$V2) ==0,] <- 
      do.call(rbind,strsplit(aa[nchar(aa$V2) ==0,]$V1,' ')) ## space as separator :I assume you 
                                                               don't have compound  name

> aa
            V1     V2
1         John    Doe
2      Rebecca  Homes
3 Organization    LLC

EDIT better method : I use a reglar expression to replace any space by a comma to have regular separator. I assume that you don't have any compound name.

ff <- readLines(textConnection('John, Doe
Rebecca, Homes
Organization LLC'))
do.call(rbind,
strsplit(gsub('[ ]|, |,[ ]',',',ff),','))
agstudy
  • 119,832
  • 17
  • 199
  • 261