Re: [R] reshape a matrix
At 18:21 20/08/2011, Uwe Ligges wrote: On 20.08.2011 19:04, David Winsemius wrote: On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote: [snip original problem] David, I think there are some good examples on the help page. What is missing? What is not clearly explained? If a longer tutorial is needed, that may be an article for the R Help Desk in The R Journal. Anybody volunteering? Uwe I think the problem is that those of us who do not understand are in such a state of ignorance that we do not know what it is that we do not understand. I have resigned myself to the realisation that there are a very small number of things about R which I shall never understand and always solve by trial and error and the parameterisation of reshape is the leading example (closely followed by backslashes). I do not think the writers of the documentation are at fault here, it is either just inherently difficult to understand or my cortex is wired up inappropriately. Best, Uwe So Hadley wrote an alternate facility ... the reshape package that does not have a reshape function in it but rather two functions 'melt' and 'cast'. Your data is all ready molten, i.e. it is in the long format (in the terminology of the base reshape function) with identifier values in each row and a single column of values. library(reshape) cast(dataframe,A~B) Using C as value column. Use the value argument to cast to override this choice A control sample 1 d0 1e+05 1e+05 2 d1 2e+02 3e+02 3 d2 4e+02 5e+02 Basically the cast formula keeps the LHS variables in the rows and hte RHD variables get arranges in columns. (For reasons that are unclear to me the dataframe argument was placed first when using positional argument passing, unlike most other formula methods in R.) Michael Dewey i...@aghmed.fsnet.co.uk http://www.aghmed.fsnet.co.uk/home.html __ R-help@r-project.org mailing list 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.
Re: [R] reshape a matrix
On 21.08.2011 12:54, Michael Dewey wrote: At 18:21 20/08/2011, Uwe Ligges wrote: On 20.08.2011 19:04, David Winsemius wrote: On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote: [snip original problem] David, I think there are some good examples on the help page. What is missing? What is not clearly explained? If a longer tutorial is needed, that may be an article for the R Help Desk in The R Journal. Anybody volunteering? Uwe I think the problem is that those of us who do not understand are in such a state of ignorance that we do not know what it is that we do not understand. I have resigned myself to the realisation that there are a very small number of things about R which I shall never understand and always solve by trial and error and the parameterisation of reshape is the leading example (closely followed by backslashes). I do not think the writers of the documentation are at fault here, it is either just inherently difficult to understand or my cortex is wired up inappropriately. In think tha major problem is that stuff like id and time etc. may be misleading, if they do not fit to the context. Don't believe I do things correct right away here. Nevertheless, improving the wording of the reshape() documentation is not easy at all if we want to be precise. Maybe we find another volunteer or I find some time to write a short column for the next issue of The R Journal. Best wishes, Uwe Best, Uwe So Hadley wrote an alternate facility ... the reshape package that does not have a reshape function in it but rather two functions 'melt' and 'cast'. Your data is all ready molten, i.e. it is in the long format (in the terminology of the base reshape function) with identifier values in each row and a single column of values. library(reshape) cast(dataframe,A~B) Using C as value column. Use the value argument to cast to override this choice A control sample 1 d0 1e+05 1e+05 2 d1 2e+02 3e+02 3 d2 4e+02 5e+02 Basically the cast formula keeps the LHS variables in the rows and hte RHD variables get arranges in columns. (For reasons that are unclear to me the dataframe argument was placed first when using positional argument passing, unlike most other formula methods in R.) Michael Dewey i...@aghmed.fsnet.co.uk http://www.aghmed.fsnet.co.uk/home.html __ R-help@r-project.org mailing list 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] reshape a matrix
Hi all, I have a data.frame like following A-c('d0','d0','d1','d1','d2','d2') B-rep(c('control','sample'),3) C-c(rep(10,2),200,300,400,500) dataframe-data.frame(A,B,C) I want to reshape the matrix, so the matrix with 'd0', 'd1' and 'd2' in rows and 'control' and 'sample' in columns. Is there a function for doing this easily? Thank you in advance. Wendy -- View this message in context: http://r.789695.n4.nabble.com/reshape-a-matrix-tp3757179p3757179.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] reshape a matrix
On 20.08.2011 17:04, Wendy wrote: Hi all, I have a data.frame like following A-c('d0','d0','d1','d1','d2','d2') B-rep(c('control','sample'),3) C-c(rep(10,2),200,300,400,500) dataframe-data.frame(A,B,C) I want to reshape the matrix, so the matrix with 'd0', 'd1' and 'd2' in rows and 'control' and 'sample' in columns. Is there a function for doing this easily? See ?reshape reshape(data=dataframe, idvar=A, timevar=B, direction=wide) Uwe Ligges Thank you in advance. Wendy -- View this message in context: http://r.789695.n4.nabble.com/reshape-a-matrix-tp3757179p3757179.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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@r-project.org mailing list 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.
Re: [R] reshape a matrix
On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote: On 20.08.2011 17:04, Wendy wrote: Hi all, I have a data.frame like following A-c('d0','d0','d1','d1','d2','d2') B-rep(c('control','sample'),3) C-c(rep(10,2),200,300,400,500) dataframe-data.frame(A,B,C) I want to reshape the matrix, so the matrix with 'd0', 'd1' and 'd2' in rows and 'control' and 'sample' in columns. Is there a function for doing this easily? See ?reshape reshape(data=dataframe, idvar=A, timevar=B, direction=wide) Uwe Ligges Many people have experienced problems understanding the mechanics of the base function 'reshape'. If you do not and if do continue to use it, you would be doing the world a great service by writing a tutorial manual with a bunch of worked examples. I have never found a tutorial that clarified how I should use it in the variety of situations where I have needed it. So Hadley wrote an alternate facility ... the reshape package that does not have a reshape function in it but rather two functions 'melt' and 'cast'. Your data is all ready molten, i.e. it is in the long format (in the terminology of the base reshape function) with identifier values in each row and a single column of values. library(reshape) cast(dataframe,A~B) Using C as value column. Use the value argument to cast to override this choice A control sample 1 d0 1e+05 1e+05 2 d1 2e+02 3e+02 3 d2 4e+02 5e+02 Basically the cast formula keeps the LHS variables in the rows and hte RHD variables get arranges in columns. (For reasons that are unclear to me the dataframe argument was placed first when using positional argument passing, unlike most other formula methods in R.) -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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.
Re: [R] reshape a matrix
On 20.08.2011 19:04, David Winsemius wrote: On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote: On 20.08.2011 17:04, Wendy wrote: Hi all, I have a data.frame like following A-c('d0','d0','d1','d1','d2','d2') B-rep(c('control','sample'),3) C-c(rep(10,2),200,300,400,500) dataframe-data.frame(A,B,C) I want to reshape the matrix, so the matrix with 'd0', 'd1' and 'd2' in rows and 'control' and 'sample' in columns. Is there a function for doing this easily? See ?reshape reshape(data=dataframe, idvar=A, timevar=B, direction=wide) Uwe Ligges Many people have experienced problems understanding the mechanics of the base function 'reshape'. If you do not and if do continue to use it, you would be doing the world a great service by writing a tutorial manual with a bunch of worked examples. I have never found a tutorial that clarified how I should use it in the variety of situations where I have needed it. David, I think there are some good examples on the help page. What is missing? What is not clearly explained? If a longer tutorial is needed, that may be an article for the R Help Desk in The R Journal. Anybody volunteering? Best, Uwe So Hadley wrote an alternate facility ... the reshape package that does not have a reshape function in it but rather two functions 'melt' and 'cast'. Your data is all ready molten, i.e. it is in the long format (in the terminology of the base reshape function) with identifier values in each row and a single column of values. library(reshape) cast(dataframe,A~B) Using C as value column. Use the value argument to cast to override this choice A control sample 1 d0 1e+05 1e+05 2 d1 2e+02 3e+02 3 d2 4e+02 5e+02 Basically the cast formula keeps the LHS variables in the rows and hte RHD variables get arranges in columns. (For reasons that are unclear to me the dataframe argument was placed first when using positional argument passing, unlike most other formula methods in R.) __ R-help@r-project.org mailing list 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.
Re: [R] reshape a matrix
On Aug 20, 2011, at 1:21 PM, Uwe Ligges wrote: On 20.08.2011 19:04, David Winsemius wrote: On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote: On 20.08.2011 17:04, Wendy wrote: Hi all, I have a data.frame like following A-c('d0','d0','d1','d1','d2','d2') B-rep(c('control','sample'),3) C-c(rep(10,2),200,300,400,500) dataframe-data.frame(A,B,C) I want to reshape the matrix, so the matrix with 'd0', 'd1' and 'd2' in rows and 'control' and 'sample' in columns. Is there a function for doing this easily? See ?reshape reshape(data=dataframe, idvar=A, timevar=B, direction=wide) Uwe Ligges Many people have experienced problems understanding the mechanics of the base function 'reshape'. If you do not and if do continue to use it, you would be doing the world a great service by writing a tutorial manual with a bunch of worked examples. I have never found a tutorial that clarified how I should use it in the variety of situations where I have needed it. David, I think there are some good examples on the help page. What is missing? What is not clearly explained? The stumbling blocks I have encountered are in trying to figure out which of the multiple arguments are needed a) in going from wide to long and b) in going from long to wide, c) and what are the reasons for the various error messages I provoke . I am almost never able to do it correctly on the first try and rarely able to do it even on the fourth try. I bought Spector's book in hopes of understanding it better, but his efforts did not take root in my brain. In the instance above, how would I have know how to apply the help page description of timevar (below) to this problem? timevar the variable in long format that differentiates multiple records from the same group or individual. To my reading that does not distinguish the purpose of 'timevar' from the purpose of 'idvar' and then reading the 'idvar' definition just below it does not help at all. -- David. If a longer tutorial is needed, that may be an article for the R Help Desk in The R Journal. Anybody volunteering? Best, Uwe So Hadley wrote an alternate facility ... the reshape package that does not have a reshape function in it but rather two functions 'melt' and 'cast'. Your data is all ready molten, i.e. it is in the long format (in the terminology of the base reshape function) with identifier values in each row and a single column of values. library(reshape) cast(dataframe,A~B) Using C as value column. Use the value argument to cast to override this choice A control sample 1 d0 1e+05 1e+05 2 d1 2e+02 3e+02 3 d2 4e+02 5e+02 Basically the cast formula keeps the LHS variables in the rows and hte RHD variables get arranges in columns. (For reasons that are unclear to me the dataframe argument was placed first when using positional argument passing, unlike most other formula methods in R.) David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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.
Re: [R] reshape a matrix
On Aug 20, 2011, at 1:39 PM, David Winsemius wrote: On Aug 20, 2011, at 1:21 PM, Uwe Ligges wrote: On 20.08.2011 19:04, David Winsemius wrote: On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote: On 20.08.2011 17:04, Wendy wrote: Hi all, I have a data.frame like following A-c('d0','d0','d1','d1','d2','d2') B-rep(c('control','sample'),3) C-c(rep(10,2),200,300,400,500) dataframe-data.frame(A,B,C) I want to reshape the matrix, so the matrix with 'd0', 'd1' and 'd2' in rows and 'control' and 'sample' in columns. Is there a function for doing this easily? See ?reshape reshape(data=dataframe, idvar=A, timevar=B, direction=wide) Uwe Ligges Many people have experienced problems understanding the mechanics of the base function 'reshape'. If you do not and if do continue to use it, you would be doing the world a great service by writing a tutorial manual with a bunch of worked examples. I have never found a tutorial that clarified how I should use it in the variety of situations where I have needed it. David, I think there are some good examples on the help page. What is missing? What is not clearly explained? The stumbling blocks I have encountered are in trying to figure out which of the multiple arguments are needed a) in going from wide to long and b) in going from long to wide, c) and what are the reasons for the various error messages I provoke . I am almost never able to do it correctly on the first try and rarely able to do it even on the fourth try. I bought Spector's book in hopes of understanding it better, but his efforts did not take root in my brain. In the instance above, how would I have know how to apply the help page description of timevar (below) to this problem? timevar the variable in long format that differentiates multiple records from the same group or individual. I'm wondering it this would be clearer (if it is correct): timevar the variable in long format that order multiple records which vary within the same groups or individual specified in idvar. -- David To my reading that does not distinguish the purpose of 'timevar' from the purpose of 'idvar' and then reading the 'idvar' definition just below it does not help at all. -- David. If a longer tutorial is needed, that may be an article for the R Help Desk in The R Journal. Anybody volunteering? Best, Uwe So Hadley wrote an alternate facility ... the reshape package that does not have a reshape function in it but rather two functions 'melt' and 'cast'. Your data is all ready molten, i.e. it is in the long format (in the terminology of the base reshape function) with identifier values in each row and a single column of values. library(reshape) cast(dataframe,A~B) Using C as value column. Use the value argument to cast to override this choice A control sample 1 d0 1e+05 1e+05 2 d1 2e+02 3e+02 3 d2 4e+02 5e+02 Basically the cast formula keeps the LHS variables in the rows and hte RHD variables get arranges in columns. (For reasons that are unclear to me the dataframe argument was placed first when using positional argument passing, unlike most other formula methods in R.) David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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.
Re: [R] reshape a matrix
On 21/08/11 03:04, Wendy wrote: Hi all, I have a data.frame like following A-c('d0','d0','d1','d1','d2','d2') B-rep(c('control','sample'),3) C-c(rep(10,2),200,300,400,500) dataframe-data.frame(A,B,C) I want to reshape the matrix, so the matrix with 'd0', 'd1' and 'd2' in rows and 'control' and 'sample' in columns. Is there a function for doing this easily? Your question has been answered But if you remember that a *matrix* and a *data frame* are ***NOT*** the same thing, you'll get into a lot less trouble. Why do you think there are two *different* terms for these sorts of object if they are the same thing? Psigh!!! cheers, Rolf Turner __ R-help@r-project.org mailing list 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.
Re: [R] Reshape a matrix
Dear Dinesh, Have a look at ?cast for the reshape package dataset - data.frame(Col1 = LETTERS[1:3], Col2 = sort(rev(LETTERS)[1:3]), value = 1:3) dataset Col1 Col2 value 1AX 1 2BY 2 3CZ 3 library(reshape) cast(Col1 ~ Col2, data = dataset, fill = 0) Col1 X Y Z 1A 1 0 0 2B 0 2 0 3C 0 0 3 HTH, Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 [EMAIL PROTECTED] www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens dinesh kumar Verzonden: donderdag 6 november 2008 1:53 Aan: r-help@r-project.org Onderwerp: [R] Reshape a matrix Dear R users, I have a matrix like A X1 B Y2 C Z3 I want to reshape this matrix into this format X Y Z A 1 B 2 C 3 Thanks in advance for your help. Dinesh -- Dinesh Kumar Barupal Junior Specialist Metabolomics Fiehn Lab UCD Genome Center 451 East Health Science Drive GBSF Builidng University of California DAVIS 95616 http://fiehnlab.ucdavis.edu/staff/kumar [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document. __ R-help@r-project.org mailing list 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.
Re: [R] Reshape a matrix
Assuming you mean data frame and not matrix and depending on whether the empty spaces are intended to represent NA or 0 we have: DF - data.frame(V1 = LETTERS[1:3], V2 = LETTERS[24:26], V3 = 1:3) tapply(DF[[3]], DF[1:2], c) V2 V1 X Y Z A 1 NA NA B NA 2 NA C NA NA 3 xtabs(V3 ~ V1 + V2, DF) V2 V1 X Y Z A 1 0 0 B 0 2 0 C 0 0 3 On Wed, Nov 5, 2008 at 7:53 PM, dinesh kumar [EMAIL PROTECTED] wrote: Dear R users, I have a matrix like A X1 B Y2 C Z3 I want to reshape this matrix into this format X Y Z A 1 B 2 C 3 Thanks in advance for your help. Dinesh -- Dinesh Kumar Barupal Junior Specialist Metabolomics Fiehn Lab UCD Genome Center 451 East Health Science Drive GBSF Builidng University of California DAVIS 95616 http://fiehnlab.ucdavis.edu/staff/kumar [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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@r-project.org mailing list 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.
Re: [R] Reshape a matrix
On Thu, Nov 6, 2008 at 8:53 AM, dinesh kumar [EMAIL PROTECTED] wrote: Dear R users, I have a matrix like A X1 B Y2 C Z3 I want to reshape this matrix into this format X Y Z A 1 B 2 C 3 is this what you want? matrix(diag(c(1,2,3)),nrow=3,ncol=3,dimnames=list(c(A,B,C),c(X,Y,Z))) X Y Z A 1 0 0 B 0 2 0 C 0 0 3 __ R-help@r-project.org mailing list 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.