[R] reshaping a large dataframe in R

David L Carlson dcarlson at tamu.edu
Mon Nov 28 20:27:17 CET 2016


There may be a simpler way of getting there, but this works:

> rows <- 500
> cols <- 4004
> dat <- as.data.frame(t(replicate(rows, 1:cols)))
> dat[c(1:3, 500), c(1:4, 4001:4004)]
    V1 V2 V3 V4 V4001 V4002 V4003 V4004
1    1  2  3  4  4001  4002  4003  4004
2    1  2  3  4  4001  4002  4003  4004
3    1  2  3  4  4001  4002  4003  4004
500  1  2  3  4  4001  4002  4003  4004
> dat2 <- array(as.matrix(dat), dim=c(rows, 4, cols/4))
> dat3 <- as.data.frame(matrix(aperm(dat2, c(1, 3, 2)), rows*cols/4, 4))
> head(dat3)
  V1 V2 V3 V4
1  1  2  3  4
2  1  2  3  4
3  1  2  3  4
4  1  2  3  4
5  1  2  3  4
6  1  2  3  4
> tail(dat3)
         V1   V2   V3   V4
500495 4001 4002 4003 4004
500496 4001 4002 4003 4004
500497 4001 4002 4003 4004
500498 4001 4002 4003 4004
500499 4001 4002 4003 4004
500500 4001 4002 4003 4004

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of jean-philippe
Sent: Monday, November 28, 2016 3:07 AM
To: r-help at r-project.org
Subject: [R] reshaping a large dataframe in R

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

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list