[R-sig-DB] Data manipulation: transforming SQL query in an appropriate dataframe

Gabor Grothendieck ggrothend|eck @end|ng |rom gm@||@com
Fri Sep 5 16:39:37 CEST 2008


Correcting the inconsistencies in the data, try this
(and also see ?reshape and the reshape package for
other approaches):

DF <- structure(list(Id = 1:4, Fund = structure(c(1L, 1L, 2L, 2L),
.Label = c("Fund01",
"Fund02"), class = "factor"), Return = c(0.5, 0.4, -0.3, -0.4
), Period = structure(c(2L, 1L, 2L, 1L), .Label = c("june/08",
"may/08"), class = "factor")), .Names = c("Id", "Fund", "Return",
"Period"), class = "data.frame", row.names = c(NA, -4L))
DF

tapply(DF$Return, DF[c(2, 4)], c)


On Fri, Sep 5, 2008 at 10:13 AM, Hebbertt de Farias Soares
<hebbertt.soares using rosasoffice.com> wrote:
> Dear list,
>
> I am a beginner with R and I have  a question which may sound stupid: How can I transform my SQL query into an appropriate dataframe?
>
> I've looking around, and haven't found any solution.
>
> My return data is stored in a SQL database. When I query it, it  gives me the following output:
> Id            Fund                     Return                 Period
> 1             Fund01                0,5                         may/08
> 2             Fund01                0,4                         june/08
> 3             Fund 02               -0,3                        may/08
> 4             Fund02                -0,4                        june/2008
>
> However this data is not suitable for analysis. I would like to transform it to a format like:
>
> Period                  Fund01                Fund02
> May/08                0,5                         0,4
> June/08               -0,3                        -0,4
>
> Is it possible to do this with R? Could you please recommend me references or packages which are useful for handling this kind of data?
>
> Thank you very much for your time.
>
> Regards,
>
> Hebbertt
>
>
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-DB mailing list -- R Special Interest Group
> R-sig-DB using stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-db
>




More information about the R-sig-DB mailing list