David.Gobbett at csiro.au
2010-Apr-23 04:43 UTC
[R] transpose? reshape? flipping? challenge with data frame
Greetings all, I am having difficulty transposing, reshaping, flipping (not sure which) a data frame which is read from a DBF file. I have tried using t(), reshape() and other approaches without success. Can anyone please suggest an way (elegant or not) of flipping this data around ? The initial data is like propsum (defined below), and I want it to look like tpropsum once reformed.> propsumcoverClass R209120812 R209122212 1 C NA 0.05 2 G 0.49 0.35 3 L 0.38 0.41 4 O 0.04 0.09 5 S 0.09 0.10> tpropsumImage C G L O L.1 1 R209120812 NA 0.49 0.38 0.04 0.09 2 R209122212 0.05 0.35 0.41 0.09 0.10 # Example datasets # Input propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"), R209120812=c(NA, 0.49, 0.38, 0.04, 0.09), R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10)) # Desired output tpropsum <- data.frame(Image=c("R209120812", "R209122212"), C=c(NA, 0.05), G=c(0.49, 0.35), L=c(0.38, 0.41), O=c(0.04, 0.09), L=c(0.09, 0.10)) Thanks, David [[alternative HTML version deleted]]
Ista Zahn
2010-Apr-23 04:58 UTC
[R] transpose? reshape? flipping? challenge with data frame
Hi David, There are many ways, including rownames(propsum) <- propsum$coverClass propsum$coverClass <- NULL t(propsum) Best, Ista On Fri, Apr 23, 2010 at 5:43 AM, <David.Gobbett at csiro.au> wrote:> Greetings all, > > I am having difficulty transposing, reshaping, flipping (not sure which) a data frame which is read from a DBF file. ?I have tried using t(), reshape() and other approaches without success. Can anyone please suggest an way (elegant or not) of flipping this data around ? > > The initial data is like propsum (defined below), and I want it to look like tpropsum once reformed. >> propsum > ?coverClass R209120812 R209122212 > 1 ? ? ? ? ?C ? ? ? ? NA ? ? ? 0.05 > 2 ? ? ? ? ?G ? ? ? 0.49 ? ? ? 0.35 > 3 ? ? ? ? ?L ? ? ? 0.38 ? ? ? 0.41 > 4 ? ? ? ? ?O ? ? ? 0.04 ? ? ? 0.09 > 5 ? ? ? ? ?S ? ? ? 0.09 ? ? ? 0.10 > >> tpropsum > ? ? ? Image ? ?C ? ?G ? ?L ? ?O ?L.1 > 1 R209120812 ? NA 0.49 0.38 0.04 0.09 > 2 R209122212 0.05 0.35 0.41 0.09 0.10 > > # Example datasets > # Input > propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"), > ? ? ? ? ? ? ? ? ? ? ?R209120812=c(NA, 0.49, 0.38, 0.04, 0.09), > ? ? ? ? ? ? ? ? ? ? ?R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10)) > > # Desired output > tpropsum <- data.frame(Image=c("R209120812", "R209122212"), > ? ? ? ? ? ? ? ? ? ? ?C=c(NA, 0.05), > ? ? ? ? ? ? ? ? ? ? ?G=c(0.49, 0.35), > ? ? ? ? ? ? ? ? ? ? ?L=c(0.38, 0.41), > ? ? ? ? ? ? ? ? ? ? ?O=c(0.04, 0.09), > ? ? ? ? ? ? ? ? ? ? ?L=c(0.09, 0.10)) > > Thanks, > David > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
David.Gobbett at csiro.au
2010-Apr-23 05:03 UTC
[R] transpose? reshape? flipping? challenge with data frame
Fabulous! I managed to get close with t(), but had obviously missed the step of setting the rownames first. Thanks for your help! David -----Original Message----- From: Ista Zahn [mailto:istazahn at gmail.com] Sent: Friday, 23 April 2010 2:29 PM To: Gobbett, David (CSE, Waite Campus) Cc: r-help at r-project.org Subject: Re: [R] transpose? reshape? flipping? challenge with data frame Hi David, There are many ways, including rownames(propsum) <- propsum$coverClass propsum$coverClass <- NULL t(propsum) Best, Ista On Fri, Apr 23, 2010 at 5:43 AM, <David.Gobbett at csiro.au> wrote:> Greetings all, > > I am having difficulty transposing, reshaping, flipping (not sure which) a data frame which is read from a DBF file. ?I have tried using t(), reshape() and other approaches without success. Can anyone please suggest an way (elegant or not) of flipping this data around ? > > The initial data is like propsum (defined below), and I want it to look like tpropsum once reformed. >> propsum > ?coverClass R209120812 R209122212 > 1 ? ? ? ? ?C ? ? ? ? NA ? ? ? 0.05 > 2 ? ? ? ? ?G ? ? ? 0.49 ? ? ? 0.35 > 3 ? ? ? ? ?L ? ? ? 0.38 ? ? ? 0.41 > 4 ? ? ? ? ?O ? ? ? 0.04 ? ? ? 0.09 > 5 ? ? ? ? ?S ? ? ? 0.09 ? ? ? 0.10 > >> tpropsum > ? ? ? Image ? ?C ? ?G ? ?L ? ?O ?L.1 > 1 R209120812 ? NA 0.49 0.38 0.04 0.09 > 2 R209122212 0.05 0.35 0.41 0.09 0.10 > > # Example datasets > # Input > propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"), > ? ? ? ? ? ? ? ? ? ? ?R209120812=c(NA, 0.49, 0.38, 0.04, 0.09), > ? ? ? ? ? ? ? ? ? ? ?R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10)) > > # Desired output > tpropsum <- data.frame(Image=c("R209120812", "R209122212"), > ? ? ? ? ? ? ? ? ? ? ?C=c(NA, 0.05), > ? ? ? ? ? ? ? ? ? ? ?G=c(0.49, 0.35), > ? ? ? ? ? ? ? ? ? ? ?L=c(0.38, 0.41), > ? ? ? ? ? ? ? ? ? ? ?O=c(0.04, 0.09), > ? ? ? ? ? ? ? ? ? ? ?L=c(0.09, 0.10)) > > Thanks, > David > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Patrick Hausmann
2010-Apr-23 10:16 UTC
[R] transpose? reshape? flipping? challenge with data frame
Hi David, you could use a mix of "plyr" and reshape: # Example datasets # Input propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"), R209120812=c(NA, 0.49, 0.38, 0.04, 0.09), R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10)) library(plyr) xpropsum <- melt(propsum, id.var="coverClass", variable_name = "Image") tpropsum <- reshape(xpropsum, timevar="coverClass", idvar="Image", direction="wide") colnames(tpropsum) <- sub("value.", "", colnames(tpropsum)) tpropsum Cheers Patrick Am 23.04.2010 06:43, schrieb David.Gobbett at csiro.au:> Greetings all, > > I am having difficulty transposing, reshaping, flipping (not sure which) a data frame which is read from a DBF file. I have tried using t(), reshape() and other approaches without success. Can anyone please suggest an way (elegant or not) of flipping this data around ? > > The initial data is like propsum (defined below), and I want it to look like tpropsum once reformed. >> propsum > coverClass R209120812 R209122212 > 1 C NA 0.05 > 2 G 0.49 0.35 > 3 L 0.38 0.41 > 4 O 0.04 0.09 > 5 S 0.09 0.10 > >> tpropsum > Image C G L O L.1 > 1 R209120812 NA 0.49 0.38 0.04 0.09 > 2 R209122212 0.05 0.35 0.41 0.09 0.10 > > # Example datasets > # Input > propsum<- data.frame(coverClass=c("C", "G", "L", "O", "S"), > R209120812=c(NA, 0.49, 0.38, 0.04, 0.09), > R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10)) > > # Desired output > tpropsum<- data.frame(Image=c("R209120812", "R209122212"), > C=c(NA, 0.05), > G=c(0.49, 0.35), > L=c(0.38, 0.41), > O=c(0.04, 0.09), > L=c(0.09, 0.10)) > > Thanks, > David > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Carl Witthoft
2010-Apr-23 21:20 UTC
[R] transpose? reshape? flipping? challenge with data frame
While the OP turned out to want to transpose his data, for those who are reading and would like some (ugly) code to flip a tensor about an arbitrary axis, here goes: *_* Carl #my own cheap matrix flipflopper flip<-function(x,flipdim=1) { #axis is index, so 1 is rows, 2 is cols (for matrix) #sanity check if (flipdim > length(dim(x))) stop("Dimension selected exceeds dim of input") a <-"x[" b<-paste("dim(x)[",flipdim,"]:1",collapse="") d <-"]" # want lead and follow to be a single vector #now the trick: get the right number of commas lead<-paste(rep(',',(flipdim-1)),collapse="") follow <-paste(rep(',',(length(dim(x))-flipdim)),collapse="") thestr<-paste(a,lead,b,follow,d,collapse="") flipped<-eval(parse(text=thestr)) return(invisible(flipped)) }
Henrique Dallazuanna
2010-Apr-23 21:44 UTC
[R] transpose? reshape? flipping? challenge with data frame
Try this: xtabs(values ~ ind + cover, cbind(cover = propsum$coverClass, stack(propsum))) On Fri, Apr 23, 2010 at 1:43 AM, <David.Gobbett@csiro.au> wrote:> Greetings all, > > I am having difficulty transposing, reshaping, flipping (not sure which) a > data frame which is read from a DBF file. I have tried using t(), reshape() > and other approaches without success. Can anyone please suggest an way > (elegant or not) of flipping this data around ? > > The initial data is like propsum (defined below), and I want it to look > like tpropsum once reformed. > > propsum > coverClass R209120812 R209122212 > 1 C NA 0.05 > 2 G 0.49 0.35 > 3 L 0.38 0.41 > 4 O 0.04 0.09 > 5 S 0.09 0.10 > > > tpropsum > Image C G L O L.1 > 1 R209120812 NA 0.49 0.38 0.04 0.09 > 2 R209122212 0.05 0.35 0.41 0.09 0.10 > > # Example datasets > # Input > propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"), > R209120812=c(NA, 0.49, 0.38, 0.04, 0.09), > R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10)) > > # Desired output > tpropsum <- data.frame(Image=c("R209120812", "R209122212"), > C=c(NA, 0.05), > G=c(0.49, 0.35), > L=c(0.38, 0.41), > O=c(0.04, 0.09), > L=c(0.09, 0.10)) > > Thanks, > David > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Patrick Hausmann
2010-Apr-24 05:56 UTC
[R] transpose? reshape? flipping? challenge with data frame
Ups, I mean library(reshape) not plyr, sorry # Example datasets # Input propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"), R209120812=c(NA, 0.49, 0.38, 0.04, 0.09), R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10)) library(reshape) xpropsum <- melt(propsum, id.var="coverClass", variable_name = "Image") tpropsum <- reshape(xpropsum, timevar="coverClass", idvar="Image", direction="wide") colnames(tpropsum) <- sub("value.", "", colnames(tpropsum)) tpropsum HTH, Patrick Am 23.04.2010 12:16, schrieb Patrick Hausmann:> Hi David, > > you could use a mix of "plyr" and reshape: > > # Example datasets > # Input > propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"), > R209120812=c(NA, 0.49, 0.38, 0.04, 0.09), > R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10)) > > library(plyr) > xpropsum <- melt(propsum, id.var="coverClass", variable_name = "Image") > tpropsum <- reshape(xpropsum, timevar="coverClass", idvar="Image", > direction="wide") > colnames(tpropsum) <- sub("value.", "", colnames(tpropsum)) > tpropsum > > Cheers > Patrick