sum of two lists

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

sum of two lists

zhenjiang xu
Hi R users,

Suppose I have two lists and the names of list 'm' are a subset of those of
'n', how can I sum the two lists with corresponding elements added together
to get list 'o'?

> n = list("a"=1,"b"=3,"c"=5)
> m = list('b'=4)
> o
$a
[1] 1

$b
[1] 7

$c
[1] 5

Thanks

--
Best,
Zhenjiang

        [[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: sum of two lists

Henrique Dallazuanna
Try this:

as.list(colSums(merge(m, n, all = TRUE), na.rm = TRUE))

On Mon, Aug 29, 2011 at 7:39 PM, zhenjiang xu <[hidden email]>wrote:

> Hi R users,
>
> Suppose I have two lists and the names of list 'm' are a subset of those of
> 'n', how can I sum the two lists with corresponding elements added together
> to get list 'o'?
>
> > n = list("a"=1,"b"=3,"c"=5)
> > m = list('b'=4)
> > o
> $a
> [1] 1
>
> $b
> [1] 7
>
> $c
> [1] 5
>
> Thanks
>
> --
> Best,
> Zhenjiang
>
>        [[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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[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: sum of two lists

zhenjiang xu
Thanks, Henrique. It works.

On Mon, Aug 29, 2011 at 6:45 PM, Henrique Dallazuanna <[hidden email]> wrote:

> Try this:
> as.list(colSums(merge(m, n, all = TRUE), na.rm = TRUE))
>
> On Mon, Aug 29, 2011 at 7:39 PM, zhenjiang xu <[hidden email]>
> wrote:
>>
>> Hi R users,
>>
>> Suppose I have two lists and the names of list 'm' are a subset of those
>> of
>> 'n', how can I sum the two lists with corresponding elements added
>> together
>> to get list 'o'?
>>
>> > n = list("a"=1,"b"=3,"c"=5)
>> > m = list('b'=4)
>> > o
>> $a
>> [1] 1
>>
>> $b
>> [1] 7
>>
>> $c
>> [1] 5
>>
>> Thanks
>>
>> --
>> Best,
>> Zhenjiang
>>
>>        [[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.
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>



--
Best,
Zhenjiang

______________________________________________
[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.