[R] reshaping a large dataframe in R

Daniel Nordlund djnordlund at gmail.com
Mon Nov 28 22:58:28 CET 2016


On 11/28/2016 1:06 AM, jean-philippe wrote:
> dear all,
>
> I have a dataframe of 500 rows and 4004 columns that I would like to
> reshape to a dataframe of 500500 rows and 4 columns. That is from this
> dataframe:
>
> V1 V2 V3 V4 ... V4001 V4002 V4003 V4004
>
> 1 2 3 4 ... 4001 4002 4003 4004
>
> 1 2 3 4 ... 4001 4002 4003 4004
>
> 1 2 3 4 ... 4001 4002 4003 4004
>
> ... ... ... ... ... ... ... ... ... ... ... ... ...
>
> 1 2 3 4 ... 4001 4002 4003 4004
>
> I would like :
>
>
> V1 V2 V3 V4
>
> 1 2 3 4
>
> 1 2 3 4
>
> 1 2 3 4
>
> 1 2 3 4
>
> ... ... ... ... ... ... ... ... ...
>
> 4001 4002 4003 4004
>
> 4001 4002 4003 4004
>
> 4001 4002 4003 4004
>
> ... ... ... ... ...
>
> 4001 4002 4003 4004
>
> I tried already to use y=matrix(as.matrix(dataGaus[[1]]),500500,4)
> (where dataGaus is my dataframe) but it doesn't give the expected
> result. I tried also to use reshape but I can't manage to use it to
> reproduce the result (and I have been through lot of posts on
> StackOverflow and on the net). In python, we can do this with a simple
> command numpy.array(dataGaus[[1]]).reshape(-1,4). For some reasons, I am
> doing my analysis in R, and I would like to know if there is a function
> which does the same thing as the reshape(-1,4) of numpy in Python?
>
> Thanks in advance, best
>
>
> Jean-Philippe
>

I don't know about efficiency, but it looks like you could do something 
like this:

y <- t(matrix(t(dataGaus),4))


Maybe someone will come along with something better,

Dan

-- 
Daniel Nordlund
Port Townsend, WA  USA



More information about the R-help mailing list