Re: [R] How to copy data from data.frame to matrix

From: Dmitrij Kudriavcev <dimitrij.kudriavcev_at_ntsg.lt>
Date: Sat, 05 Mar 2011 16:02:17 +1100

Hello,

Thank you for response

I have try reshape function, but i do not like the way how it generate columns and row names. Currently

> tapply(s$value, list(s$day, s$symbol), mean)

seems to generate exactly what i need

Cheers,
Dima

2011/3/5 Uwe Ligges <ligges_at_statistik.tu-dortmund.de>

>
>
>
> On 04.03.2011 11:38, Dmitrij Kudriavcev wrote:
>
>> Hello
>>
>> Let's say, my data.frame is
>>
>> symbol,day,value
>> A, 2010-01-01, 0.8888
>> A, 2010-01-02, 0.6666
>> B, 2010-01-01, 0.7777
>>
>> i need to get matrix as
>>
>
>
> See ?reshape, in this case if you data.frame is in dat:
>
> reshape(dat, v.names="value", direction="wide",
> idvar="day", timevar="symbol")
>
> Uwe Ligges
>
>
>
>
> , A, B
>> 2010-01-01, 0.8888, 0.7777
>> 2010-01-02, 0.6666, NA
>>
>> where A and B is columns name and date used as row name
>>
>> I found a way how to do it with tapply function, is it a best way (i will
>> need to do this pretty offen and wish to save some time)
>>
>> Cheers,
>> Dima
>>
>>
>> 2011/3/4 Ivan Calandra<ivan.calandra_at_uni-hamburg.de>
>>
>> Hi,
>>>
>>> Let's say your data.frame is called df:
>>> df<- data.frame(a=rnorm(10), b=rnorm(10))
>>> data.matrix<- as.matrix(df)
>>>
>>> This should work, but be careful with coercion if you have different
>>> modes
>>> in your data.frame
>>>
>>> HTH,
>>> Ivan
>>>
>>> PS: next time, provide a reproducible example, using dput() for example
>>>
>>> Le 3/4/2011 07:08, Dmitrij Kudriavcev a écrit :
>>>
>>> Hello
>>>>
>>>> I'm a new in R
>>>> I have a large data.frame "s" (this is actualy just a table in mysql) :
>>>>
>>>> names(s)
>>>>
>>>>>
>>>>> [1] "symbols", "day", "value"
>>>>
>>>> I need to convert it to simple matrix. I have define this matrix like
>>>> this:
>>>>
>>>> data.matrix<- matrix(nrow=nDays, ncol=nSymbols, dimnames=list(days,
>>>>
>>>>>
>>>>> symbols))
>>>>
>>>> then i just copy values to the matrix using for() loop, but it seems to
>>>> take
>>>> very long time. Is is a more fast way to do it in R? I know, what i can
>>>> just
>>>> gyve s$value as source data to the matrix, but problem is, what for some
>>>> symbols couple days could be just missed.
>>>>
>>>> Cheers,
>>>> Dima
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>>
>>>> --
>>> Ivan CALANDRA
>>> PhD Student
>>> University of Hamburg
>>> Biozentrum Grindel und Zoologisches Museum
>>> Abt. Säugetiere
>>> Martin-Luther-King-Platz 3
>>> D-20146 Hamburg, GERMANY
>>> +49(0)40 42838 6231
>>> ivan.calandra_at_uni-hamburg.de
>>>
>>> **********
>>> http://www.for771.uni-bonn.de
>>> http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
>>>
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>
>> [[alternative HTML version deleted]]
>>
>>
>>
>>
>> ______________________________________________
>> 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.
>>
>

        [[alternative HTML version deleted]]



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. Received on Sat 05 Mar 2011 - 07:58:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 05 Mar 2011 - 10:10:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive