[R] reshaping a large dataframe in R

jean-philippe jeanphilippe.fontaine at gssi.infn.it
Mon Nov 28 10:06:56 CET 2016


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

-- 
Jean-Philippe Fontaine
PhD Student in Astroparticle Physics,
Gran Sasso Science Institute (GSSI),
Viale Francesco Crispi 7,
67100 L'Aquila, Italy
Mobile: +393487128593, +33615653774



More information about the R-help mailing list