Hi
probably not at all simpler
> dat2.p<-split(t(dat), rep(1:(ncol(dat)/4), each=4))
> dat3.p<-as.data.frame(do.call(rbind, lapply(dat2.p, function(x)
t(matrix(x, 4,nrow(dat))))))
>
> all.equal(dat3.p, dat3)
[1] TRUE
Cheers
Petr
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of David L
> Carlson
> Sent: Monday, November 28, 2016 8:27 PM
> To: jean-philippe <jeanphilippe.fontaine at gssi.infn.it>; r-help at
r-project.org
> Subject: Re: [R] reshaping a large dataframe in R
>
> 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.
>
> ______________________________________________
> 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.
________________________________
Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny
pouze jeho adres?t?m.
Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho
odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho
syst?mu.
Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email
jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat.
Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i
zpo?d?n?m p?enosu e-mailu.
V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?:
- vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a
to z jak?hokoliv d?vodu i bez uveden? d?vodu.
- a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout;
Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce
s dodatkem ?i odchylkou.
- trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m
dosa?en?m shody na v?ech jej?ch n?le?itostech.
- odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost
??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn?
pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn?
osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi
?i osob? j?m zastoupen? zn?m?.
This e-mail and any documents attached to it may be confidential and are
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender.
Delete the contents of this e-mail with all attachments and its copies from your
system.
If you are not the intended recipient of this e-mail, you are not authorized to
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by
modifications of the e-mail or by delay with transfer of the email.
In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately
accept such offer; The sender of this e-mail (offer) excludes any acceptance of
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into
any contracts on behalf of the company except for cases in which he/she is
expressly authorized to do so in writing, and such authorization or power of
attorney is submitted to the recipient or the person represented by the
recipient, or the existence of such authorization is known to the recipient of
the person represented by the recipient.