Hi to all, how could I to rotate automatically a data sheet which was imported by read.xls? x1 x2 x3 .... xn y1 1 4 7 ... xn/y1 y2 2 5 8 .... xn/y2 y3 3 6 9 ....xn/y2 yn ... ... ... Xn/Yn to y1 y2 y3 .... yn x1 1 2 3 ..... Yn/x1 x2 4 5 6 .... Yn/x2 x3 7 8 9 .... Yn/x2 xn ... ... ... ..... Yn/xn Kind regards Knut
On Fri, Mar 25, 2011 at 11:43:31AM +0100, Knut Krueger wrote:> Hi to all, > how could I to rotate automatically a data sheet which was imported > by read.xls? > > x1 x2 x3 .... xn > y1 1 4 7 ... xn/y1 > y2 2 5 8 .... xn/y2 > y3 3 6 9 ....xn/y2 > yn ... ... ... Xn/Yn > > > to > > y1 y2 y3 .... yn > x1 1 2 3 ..... Yn/x1 > x2 4 5 6 .... Yn/x2 > x3 7 8 9 .... Yn/x2 > xn ... ... ... ..... Yn/xnIf all the columns (x) are of the same type (e.g. all numeric) you can use t(). Example: dat <- data.frame(x1=1:10, x2=(1:10)*2, x3=10:1) dat2 <- as.data.frame(t(dat)) If the comlumns are of differnt types (e.g. some numeric, some factors) I don't think you can do this at all, because columns of a data.frame represent vectors, i.e. all value sin a column need to be of the same type. cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan Maximus-von-Imhof-Forum 3 85354 Freising, Germany http://webclu.bio.wzw.tum.de/~pagel/
Am 25.03.2011 12:31, schrieb Philipp Pagel:> > If all the columns (x) are of the same type (e.g. all numeric) you can > use t(). Example:Unfortunately we have mixed types f.e text , dates times , and numbers Knut
Hi Knut, On Fri, Mar 25, 2011 at 10:43 AM, Knut Krueger <rh at knut-krueger.de> wrote:> Hi to all, > how could I ?to rotate automatically a data sheet which was imported by > read.xls? > > ? ? x1 x2 x3 .... xn > y1 1 ? 4 ?7 ? ... ?xn/y1 > y2 2 ? 5 ?8 ? .... xn/y2 > y3 3 ? 6 ?9 ? ?....xn/y2 > yn ... ... ... ? ? ? ? Xn/Yn > > > to > > ? ? ?y1 y2 ?y3 ? .... ?yn > x1 ?1 ?2 ? ?3 ? ? ..... Yn/x1 > x2 ?4 ?5 ? ?6 ? ? .... ?Yn/x2 > x3 ?7 ?8 ? 9 ? ? ?.... ?Yn/x2 > xn ?... ? ... ?... ? ..... ?Yn/xnBut think first why exactly do you need it? If for printing and/or presentation rather than further analysis, then you can you can transform your data frame to a "character" matrix and use use it this way. t(df) would do it, or t(sapply(df, as.character)) might be safer if you have factors in there. If you want to use the transformed data frame in an analysis, I can see two sensible possibilities: 1. Just modify the analysis so that instead of x[i,j] it would use x[j,i] -- in many cases this would be the best solution 2. Extract the numeric (or whatever other type) columns from your data frame, transform it to matrix with as.matrix and then "rotate". t(as.matrix(df[,sapply(df, is.numeric)])) or something alike Regards, Kenn
It does not appear to have been clearly noted in this thread that one CANNOT "rotate" a data frame. Data frames are designed to contain mixed types in their columns, which means that to "rotate" the frame, the rows would have to be coerced to a single type, thereby most likely losing the information. So the answer to the question is: read ?data.frame to understand why the question is bogus. matrices (or arrays) are a single type and can be simply "rotated" (transposed) by ?t (or more generally have their indices permuted by ?aperm). You would have to manually handle row and column names, however. See ?dimnames -- Bert On Fri, Mar 25, 2011 at 9:52 AM, Knut Krueger <rh at knut-krueger.de> wrote:> Am 25.03.2011 14:51, schrieb Philipp Pagel: >> >> The frame$y2 notation still only works for columns, of course. >> Maybe, if you tell us some more about your actual analysis, >> more help can be provided. > > Thank,s ?but the only question was to use a common notation like for columns > if the excel sheet was build rotated. > > Its more easy for beginners to use the column notation than the row notation > from a data.frame. > One solution is to rotate the excel sheet, but than you have multiple data. > So I was looking for a solution to rotate the data set in R > > I think I will explain them the row notation for a data.frame > > Thank you Knut > > ______________________________________________ > R-help at 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. >-- Bert Gunter Genentech Nonclinical Biostatistics