[R] setting attributes

Michael Sumner mdsumner at gmail.com
Fri Nov 5 05:44:08 CET 2010


David did help already, but if you want literally what the
Introduction uses then you'll need a vector with as many elements as
the final matrix (in David's example recyling applies to give 100
elements from the original 10).

 z <- rep(0:9, each = 10)
attr(z, "dim") <- c(10,10)

That's simply a different route to the one shown by David, where the
repetition of z values is made explicit.

Cheers, Mike.

On Fri, Nov 5, 2010 at 3:32 PM, Stephen Liu <satimis at yahoo.com> wrote:
> Hi David,
>
> I'm learning R following the tutorial mentioned, nothing else.  I got an error
> running the code.  Please help.
>
> B.R.
> Stephen L
>
>
>
>
> ----- Original Message ----
> From: David Winsemius <dwinsemius at comcast.net>
> To: Stephen Liu <satimis at yahoo.com>
> Cc: r-help at r-project.org
> Sent: Fri, November 5, 2010 12:18:23 PM
> Subject: Re: [R] setting attributes
>
>
> On Nov 5, 2010, at 12:05 AM, Stephen Liu wrote:
>
>> Hi folks,
>>
>> An Introduction to R
>>
>> 3.3 Getting and setting attributes
>> http://cran.r-project.org/doc/manuals/R-intro.html#Vectors-and-assignment
>>
>>> z <- 0:9
>>> z
>> [1] 0 1 2 3 4 5 6 7 8 9
>>
>>> attr(z, "dim") <- c(10,10)
>> Error in attr(z, "dim") <- c(10, 10) :
>>  dims [product 100] do not match the length of object [10]
>>
>> Please help me to understand what mistake I committed?  TIA
>
> If you want a 10 x 10 matrix then:
>
> zm <- matrix(z, 10, 10)  # positional arguments to nrow and ncol.
>
> If you were trying for something else, then please explain in plain
> English rather than simply showing code that throws an error.
>
> David Winsemius, MD
> West Hartford, CT
>
>
>
> ______________________________________________
> 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.
>



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-help mailing list