[R] NROW doesn't equal length(x)

MacQueen, Don macqueen1 at llnl.gov
Fri Oct 28 19:37:59 CEST 2011


And to further the example, length() of matrix is not equal to the number
of rows either.

> mm <- matrix(1:6, ncol=2)
> length(mm)
[1] 6
> dim(mm)
[1] 3 2


Also, NROW() and nrow() are different; I'd be cautious about using NROW
without making sure I understood the difference.

> NROW
function (x) 
if (is.array(x) || is.data.frame(x)) nrow(x) else length(x)
<environment: namespace:base>
> 
> 
> nrow
function (x) 
dim(x)[1L]
<environment: namespace:base>
>




-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 10/27/11 12:23 PM, "R. Michael Weylandt <michael.weylandt at gmail.com>"
<michael.weylandt at gmail.com> wrote:

>Data frame is list internally so length(df) = ncol(df)
>
>M
>
>On Oct 27, 2011, at 2:44 PM, Muhammad Abuizzah <izzah100 at yahoo.com> wrote:
>
>> Hi,
>> 
>> I am converting a data.frame to xts.  the data.frame is 4 columns and
>>1000 rows.  I get a message that "NROW (x) must match length(order.by)
>> class is data.frame, mode is list
>> 
>> when I run 
>> dim(x)   # I get
>> 1000     4   #which is consistent with 1000 rows and 4 columns
>> 
>> NROW (x)  # I get
>> 
>> 1000  # which is the right answer
>> 
>> When I run length on each of columns in x separately using the "$" I
>>get 1000, which is the right number too.
>> So length on each of the columns individually gives me the right
>>answer, but length on the data.frame gives me the number of columns
>>instead of the number of rows, is there an explanation
>> 
>> 
>> 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.
>
>______________________________________________
>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.



More information about the R-help mailing list