[R] Extract one element from yahooKeystats data

David Winsemius dwinsemius at comcast.net
Tue Apr 28 05:45:36 CEST 2009


On Apr 27, 2009, at 8:16 PM, J Toll wrote:

> On Mon, Apr 27, 2009 at 5:42 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>> Using str(data) would have been more informative.
>>
>> "data" it turns out is a dataframe with a single column. which is a  
>> factor
>> with rownames. Not the most typical of constructions, but the  
>> authors must
>> have had their reasons ....
>>
>> data$Value[row.names(data)=="Float"]
>> # [1] 1.30B
>> # 52 Levels: -1.00% -11.40% -18.69% -38.03% 0.04% 0.78 06-Feb-09 09- 
>> Mar-09
>> ... NA
>>
>> # ... or to get rid of those annoying factor levels...
>> as.character(data[row.names(data)=="Float", ] )
>> # [1] "1.30B"
>
> Thank you.  I was in the middle of trying to respond to your first
> post when I got your second containing the answer.
>
> My problem is that I didn't understand the structure of the data and I
> couldn't figure out the relationship of the row names to the data so I
> was trying everything under the sun, yet nothing was yielding the
> results I wanted.
>
> On my own, I was able to retrieve the data using:
>> d.ibm$Value[38]
> [1] 1.30B
> 52 Levels: -1.00% -11.40% -18.69% -38.03% 0.04% 0.78 06-Feb-09
> 09-Mar-09 1.176 1.30% 1.30B 1.32B 1.40% 1.7 10.304 100.84B ... NA
>
> But that seemed too brittle to be used reliably.  I wanted a way to
> get to the data using what I now know to be the row name.  I'm sorry
> my initial question was so vague/uninformative.  Through brute force,
> I had tried dozens of different ways to coax out the information I
> needed.  What I really needed was a better understanding of the data
> structure.
>

And this construction using indexing might be more natural:

 > data["Float", ]
[1] 1.30B
52 Levels: -1.00% -11.40% -18.69% -38.03% 0.04% 0.78 06-Feb-09 09- 
Mar-09 ... NA
 > as.character( data["Float", ] )
[1] "1.30B"

(I never have understood the attraction of factors. But there are a  
great many things I do not understand.)

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list