tags:

views:

215

answers:

3

I have a guest list that has a last name in one column and then in another column I have the first names or the full names (first space last) of each person in the family. I am wanting to get the other column to just have the first names.

gsub(guest.w$Last.Name,"",guest.w$Party.Name.s.)

That would work perfectly if I just had one row but how do it do it for each row in the dataframe. Do I have to write a for loop? Is there a way to do it in parallel similarly to the way pmax() relates to max().

My problem is similar in a way to a previously asked question by JD Long but that question was a piece of cake compared to mine.

Example

:

Smith; Joe Smith, Kevin Smith, Jane Smith
Alter; Robert Alter, Mary Alter, Ronald Alter

Becomes

Smith; Joe, Kevin, Jane
Alter; Robert, Mary, Ronald

A: 

I am not sure it will work on a dataframe, but you could try one of the apply functions:

`y1 <- sapply(dataframe, gsub(guest.w$Last.Name,"",guest.w$Party.Name.s.))`
twolfe18
sapply(guest.w,gsub(guest.w$Last.Name,"",guest.w$Party.Name.s.))No. I tried thatError in match.fun(FUN) : 'gsub(guest.w$Last.Name, "", guest.w$Party.Name.s.)' is not a function, character or symbolIn addition: Warning message:In gsub(guest.w$Last.Name, "", guest.w$Party.Name.s.) : argument 'pattern' has length > 1 and only the first element will be used
Farrel
+1  A: 

Using hadleys adply:

library(plyr)
df <- data.frame(rbind(c('Smith', 'Joe Smith, Kevin Smith, Jane Smith'), c('Alter', 'Robert Alter, Mary Alter, Ronald Alter')))
names(df) <- c("last", "name")
adply(df,1,transform, name=gsub(last, '', name))

You will probably need to clean up the spaces in your new vector.

Eduardo Leoni
A: 
Izzy