Merge rows

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Merge rows

Ryan de Vera
Hello all,

I have a data frame filled with senders and recipients. Some of the senders
have multiple rows with different recipients and I want to merge those
rows. For example I have

[hidden email]     [hidden email]
[hidden email]     [hidden email]     [hidden email]
[hidden email]      [hidden email]
[hidden email]      [hidden email]

I want this to become

[hidden email]     [hidden email]     [hidden email]     [hidden email]
[hidden email]      [hidden email]      [hidden email]

How would I go about doing this?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Merge rows

Sarah Goslee
Hi Ryan,

We can't tell from your example what structure your original data are
in, nor what your output is intended to look like, or for that matter
how you got from one to the other. Please don't post in HTML because
it gets mangled!

Using dput() to provide your example data is the best thing, because
that preserves R structures. And an R-format example of your desired
output would also be helpful.

Sarah

On Fri, Jul 11, 2014 at 12:19 PM, Ryan de Vera <[hidden email]> wrote:

> Hello all,
>
> I have a data frame filled with senders and recipients. Some of the senders
> have multiple rows with different recipients and I want to merge those
> rows. For example I have
>
> [hidden email]     [hidden email]
> [hidden email]     [hidden email]     [hidden email]
> [hidden email]      [hidden email]
> [hidden email]      [hidden email]
>
> I want this to become
>
> [hidden email]     [hidden email]     [hidden email]     [hidden email]
> [hidden email]      [hidden email]      [hidden email]
>
> How would I go about doing this?
>
>         [[alternative HTML version deleted]]

^^^ That is part of the problem!

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Merge rows

Sarah Goslee
Hi John,

I don't think your x2 is right, but who knows?

One possible approach would be:
R> lapply(split(x2$recipients,  unique(x2$sender)), paste, collapse=", ")
$`[hidden email]`
[1] "[hidden email], [hidden email]"

$`[hidden email]`
[1] "c(\"[hidden email]\", \"[hidden email]\"), [hidden email]"

but again, who knows exactly what the OP wants?

You sent this just to me; I've copied the R-help list back again.

Sarah

On Fri, Jul 11, 2014 at 3:19 PM, John McKown
<[hidden email]> wrote:

> On Fri, Jul 11, 2014 at 11:27 AM, Sarah Goslee <[hidden email]> wrote:
>> Hi Ryan,
>>
>> We can't tell from your example what structure your original data are
>> in, nor what your output is intended to look like, or for that matter
>> how you got from one to the other. Please don't post in HTML because
>> it gets mangled!
>>
>> Using dput() to provide your example data is the best thing, because
>> that preserves R structures. And an R-format example of your desired
>> output would also be helpful.
>>
>> Sarah
>
> In a, perhaps vain, attempt to be helpful, I think the following
> output from what I constructed myself will show what the OP was
> talking about:
>
>> dput(x2)
> structure(list(sender = c("[hidden email]", "[hidden email]", "[hidden email]",
> "[hidden email]"), recipients = list("[hidden email]", c("[hidden email]",
> "[hidden email]"), "[hidden email]", "[hidden email]")), .Names = c("sender",
> "recipients"), row.names = c(NA, -4L), class = "data.frame")
>
> I probably should have called it "mailingList" instead of x2, no?
>
> Now, after "wasting" too much time, perhaps somebody will come up with
> a solution. I could "brute force" something (yes, you can write
> FORTRAN style code in R!). But have been "sensitized" from doing so by
> others.
>
> --
> There is nothing more pleasant than traveling and meeting new people!
> Genghis Khan
>
> Maranatha! <><
> John McKown


--
Sarah Goslee
http://www.sarahgoslee.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Merge rows

Jim Lemon
In reply to this post by Ryan de Vera
On Fri, 11 Jul 2014 12:19:39 PM Ryan de Vera wrote:
> Hello all,
>
> I have a data frame filled with senders and recipients. Some of the
senders
> have multiple rows with different recipients and I want to merge
those

> rows. For example I have
>
> [hidden email]     [hidden email]
> [hidden email]     [hidden email]     [hidden email]
> [hidden email]      [hidden email]
> [hidden email]      [hidden email]
>
> I want this to become
>
> [hidden email]     [hidden email]     [hidden email]     [hidden email]
> [hidden email]      [hidden email]      [hidden email]
>
> How would I go about doing this?
>
Hi Ryan,
This is a bit messy, but assuming that you do have a data frame like
this:

rdvdf<-
data.frame(sender=rep(c("[hidden email]","[hidden email]"),each=2),
 recipient1=c("[hidden email]","[hidden email]","[hidden email]","[hidden email]"),
 recipient2=c(NA,"[hidden email]",NA,NA))

you can try this:

newdat<-list()
senderno<-1
for(sndr in unique(rdvdf$sender)) {
 newvec<-
  as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
 newdat[[senderno]]<-newvec[!is.na(newvec)]
 senderno<-senderno+1
}

Jim

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Merge rows

dkStevens
This is a (very) slightly modified version of Jim's reply that takes the
sender's email our of the list element and uses it as the name so it can
be accessed as newdat$'senders email' or newdat[['senders email']]

newdat<-list()
for(sndr in unique(rdvdf$sender)) {
  newvec<-
   as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
  newdat[[(sndr)]]<-newvec[which(!is.na(newvec))][-1]
}

David

On 7/12/2014 1:07 AM, Jim Lemon wrote:

> On Fri, 11 Jul 2014 12:19:39 PM Ryan de Vera wrote:
>> Hello all,
>>
>> I have a data frame filled with senders and recipients. Some of the
> senders
>> have multiple rows with different recipients and I want to merge
> those
>> rows. For example I have
>>
>> [hidden email]     [hidden email]
>> [hidden email]     [hidden email]     [hidden email]
>> [hidden email]      [hidden email]
>> [hidden email]      [hidden email]
>>
>> I want this to become
>>
>> [hidden email]     [hidden email]     [hidden email]     [hidden email]
>> [hidden email]      [hidden email]      [hidden email]
>>
>> How would I go about doing this?
>>
> Hi Ryan,
> This is a bit messy, but assuming that you do have a data frame like
> this:
>
> rdvdf<-
> data.frame(sender=rep(c("[hidden email]","[hidden email]"),each=2),
>   recipient1=c("[hidden email]","[hidden email]","[hidden email]","[hidden email]"),
>   recipient2=c(NA,"[hidden email]",NA,NA))
>
> you can try this:
>
> newdat<-list()
> senderno<-1
> for(sndr in unique(rdvdf$sender)) {
>   newvec<-
>    as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
>   newdat[[senderno]]<-newvec[!is.na(newvec)]
>   senderno<-senderno+1
> }
>
> Jim
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
David K Stevens, P.E., Ph.D.
Professor and Head, Environmental Engineering
Civil and Environmental Engineering
Utah Water Research Laboratory
8200 Old Main Hill
Logan, UT  84322-8200
435 797 3229 - voice
435 797 1363 - fax
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Merge rows

dkStevens
Cautionary note on the solution below. Be sure the 'sndr' is either
factor or character because, if sndr is numeric, as the list is
populated, R will fill in non-adjacent list items with NULLs, leaving a
list with many empty entries. So, the modified line is

       newdat[[as.character(vnam)]]<-newvec[-1]

David

On 7/12/2014 8:33 AM, David Stevens wrote:

> This is a (very) slightly modified version of Jim's reply that takes
> the sender's email our of the list element and uses it as the name so
> it can be accessed as newdat$'senders email' or newdat[['senders email']]
>
> newdat<-list()
> for(sndr in unique(rdvdf$sender)) {
>  newvec<-
>   as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
>  newdat[[(sndr)]]<-newvec[which(!is.na(newvec))][-1]
> }
>
> David
>
> On 7/12/2014 1:07 AM, Jim Lemon wrote:
>> On Fri, 11 Jul 2014 12:19:39 PM Ryan de Vera wrote:
>>> Hello all,
>>>
>>> I have a data frame filled with senders and recipients. Some of the
>> senders
>>> have multiple rows with different recipients and I want to merge
>> those
>>> rows. For example I have
>>>
>>> [hidden email]     [hidden email]
>>> [hidden email]     [hidden email]     [hidden email]
>>> [hidden email]      [hidden email]
>>> [hidden email]      [hidden email]
>>>
>>> I want this to become
>>>
>>> [hidden email]     [hidden email]     [hidden email]     [hidden email]
>>> [hidden email]      [hidden email]      [hidden email]
>>>
>>> How would I go about doing this?
>>>
>> Hi Ryan,
>> This is a bit messy, but assuming that you do have a data frame like
>> this:
>>
>> rdvdf<-
>> data.frame(sender=rep(c("[hidden email]","[hidden email]"),each=2),
>> recipient1=c("[hidden email]","[hidden email]","[hidden email]","[hidden email]"),
>>   recipient2=c(NA,"[hidden email]",NA,NA))
>>
>> you can try this:
>>
>> newdat<-list()
>> senderno<-1
>> for(sndr in unique(rdvdf$sender)) {
>>   newvec<-
>>    as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
>>   newdat[[senderno]]<-newvec[!is.na(newvec)]
>>   senderno<-senderno+1
>> }
>>
>> Jim
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>

--
David K Stevens, P.E., Ph.D.
Professor and Head, Environmental Engineering
Civil and Environmental Engineering
Utah Water Research Laboratory
8200 Old Main Hill
Logan, UT  84322-8200
435 797 3229 - voice
435 797 1363 - fax
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.