[R] reshaping a large dataframe in R
PIKAL Petr
petr.pikal at precheza.cz
Tue Nov 29 09:53:46 CET 2016
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.
More information about the R-help
mailing list