[R] Adding variables

tzygmund mcfarlane tzygmund at googlemail.com
Sun Sep 27 16:25:02 CEST 2009


> So why were you trying to add a factor variable to a numeric, anyway?

For no other reason than to illustrate the task of addition. It is,
admittedly, meaningless.

> Well, I had never seen any help pages use "attenu" as an example.

I literally went to:
http://stat.ethz.ch/R-manual/R-patched/library/datasets/html/00Index.html
and picked a random dataset. I haven't enough R experience to know
which datasets are more commonly used to illustrate issues than
others.

My original question was about the efficiency of simple arithmetic for
large datasets and managing them. While the advice I have received so
far is very good for my R style, I was wondering if you might add
something regarding this.

PS. Thanks Henrique, but again the example was chosen completely at
random to provide reproducible code & I wasn't thinking about numeric
and factor variables.

On Sun, Sep 27, 2009 at 3:14 PM, David Winsemius <dwinsemius at comcast.net> wrote:
> Well, I had never seen any help pages use "attenu" as an example. Like Jim I
> assumed that you were offering code that was operating on some private copy
> of data, If you look at the number of datasets, I think it unreasonable to
> expect the great majority of potentially helpful persons to know all of them
> either.
>
> So why were you trying to add a factor variable to a numeric, anyway? If
> your hope was to convert those codes to numeric then it's a FAQ:
>
> http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f
>
> If the problem were more basic, and you did not know what was in that
> dataset then the answer might have bee:
>
> str(attenu)
>
> --
> David
>
>
>
>
> On Sep 27, 2009, at 7:47 AM, tzygmund mcfarlane wrote:
>
>> Jim,
>>
>> Both my emails contained reproducible code (the first one wasn't
>> completely reproducible - it required one to know that "attenu" is a
>> base R dataset).
>>
>> Anyway, thanks for your help.
>>
>> On Sat, Sep 26, 2009 at 8:11 PM, jim holtman <jholtman at gmail.com> wrote:
>>>
>>> I assumed (since you did not provide reproducible code) that 'mag' was
>>> a matrix.  If 'station' is a matrix, then
>>>
>>> mag + rowSums(station)
>>>
>>> will work.  If that does not work, then you need to tell us what your
>>> data objects are.
>>>
>>> On Sat, Sep 26, 2009 at 11:39 AM, tzygmund mcfarlane
>>> <tzygmund at googlemail.com> wrote:
>>>>
>>>> Hi Jim,
>>>>
>>>> I might be missing something but your command gives the error:
>>>> Error in rowSums(mag) : 'x' must be an array of at least two dimensions
>>>>
>>>> #############################
>>>> data(attenu)
>>>> attach(attenu)
>>>> rowSums(mag) + rowSums(station)
>>>> attenu$new<-rowSums(cbind(mag, station))
>>>> #############################
>>>>
>>>> Thanks
>>>>
>>>>
>>>> On Sat, Sep 26, 2009 at 4:30 PM, jim holtman <jholtman at gmail.com> wrote:
>>>>>
>>>>> Probably more efficient if you remove the 'cbind' which would create a
>>>>> combined matrix.  Use the following:
>>>>>
>>>>> rowSums(mag) + rowSums(station)
>>>>>
>>>>> On Sat, Sep 26, 2009 at 11:16 AM, tzygmund mcfarlane
>>>>> <tzygmund at googlemail.com> wrote:
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> For very large matrices, is this the most efficient way to add two
>>>>>> variables together?
>>>>>>
>>>>>> #############################
>>>>>> attach(attenu)
>>>>>> new<-rowSums(cbind(mag, station))
>>>>>> #############################
>>>>>>
>>>>>> Also, could I be directed to some resources for working with very
>>>>>> large datasets?
>>>>>>
>>>>>> Thanks
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at r-project.org 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.
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Jim Holtman
>>>>> Cincinnati, OH
>>>>> +1 513 646 9390
>>>>>
>>>>> What is the problem that you are trying to solve?
>>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Jim Holtman
>>> Cincinnati, OH
>>> +1 513 646 9390
>>>
>>> What is the problem that you are trying to solve?
>>>
>>
>> ______________________________________________
>> R-help at r-project.org 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 Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>




More information about the R-help mailing list