[R] Length of data.frame column

David Winsemius dwinsemius at comcast.net
Fri Oct 14 14:43:50 CEST 2011


On Oct 14, 2011, at 5:11 AM, Petr PIKAL wrote:

> Hi
>
>>
>> I have a related question...I have a data frame similar with 74 rows
> that I
>> created with "header=TRUE", but when I try to coerce one of the data
> frame
>> columns into a vector, it shows up as having length 1, even though  
>> when
> I
>> print it, it shows 74 elements:
>>
>>> VAL <- c(DailyDiary[1])
>>> VAL
>>> [1] 3 3 3 2 3 3 4 4 3 3 2 1 1 4 3 3 2 3 3 2 2 3 3 3 1 2 2
>> [28] 1 1 3 3 3 2 4 3 2 3 4 3 3 3 3 4 3 2 3 2 3 1 2 1 2 3 1
>> [55] 0 3 2 4 3 1 2 3 1 1 1 4 3 2 1 2 3 3 3 2
>>> length(VAL)
>> [1] 1
>
> your VAL is still data frame. See ?"[" and link to data.frame  
> method. Use
>
> str(object)

Or at least a list. The "c" function stripped its attributes:

 > str( c( data.frame(a=1:10) ) )
List of 1
  $ a: int [1:10] 1 2 3 4 5 6 7 8 9 10

Which also is an explanation for why its length is still reported as 1.


> to see the structure of your object not just printing it to console as
> print is a function which has also different methods for different  
> objects
> and does not inform you about nature of your object.
>
> You shall profit from reading R-intro which I believe you have  
> overlooked.
> It shall be installed in doc/manual directory of your R installation.
>
> Regards
> Petr
>
>>
>> On the other hand, I can easily coerce the row names to a vector of
> length
>> 74
>>
>>> partf <- row.names(DailyDiary)
>>> length(partf)
>> [1] 74
>>
>> What I would like to do is make VAL into a vector with length 74  
>> instead
> of
>> length 1 so I can sort it using use "partf" as factors.  I tried
>> "as.vector", but it doesn't let you specify the length.  Any ideas?

One might think that as.vector might be able to extract a vector from  
a list if length 1 but one would be wrong and should then got to :

?unlist



> Thanks,
>> and sorry if I'm being unclear or stupid, I'm a newbie :)
>>
>> Logan

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list