Allen S. Rout
2008-Feb-08 20:13 UTC
[R] Subsetting a data.frame degenerates at one column?
Greetings. At the moment, I'm applying R to some AIX 'nmon' output, trying to get a handle on some disk performance metrics. In case anyone's interested: http://docs.osg.ufl.edu/tsm/pdf/ some of them are more edifying than others. (ahem) I'm trying to develop a somewhat general framework for plotting these measures, in the hopes that it's of some use to people other than me. One obstacle I encounter is that, when I select one column out of a data.frame, the result is no longer a data.frame. So, say I've got, in data frame 'input' disk1 disk2 disk3 disk4 T0000 0 1 0 4 T0001 0 1 0 5 T0002 0 1 0 5 T0003 0 2 0 4 T0004 0 2 0 3 T0005 0 1 0 3 T0006 0 0 0 3 and somewhere I've noted a list targets <- c('disk2','disk3') I can say input[,targets] disk2 disk3 T0000 1 0 T0001 1 0 T0002 1 0 T0003 2 0 T0004 2 0 T0005 1 0 T0006 0 0 but if targets <- c('disk2') input[,targets] [1] 1 1 1 2 2 1 0 Ick. I've been reading through the indexing and data.frame docs, and remain unsatisfied so far. Where is my thinking going wrong? - Allen S. Rout
try: input[,targets, drop=FALSE] see: ?"[" for an explanation. On 2/8/08, Allen S. Rout <asr at ufl.edu> wrote:> > Greetings. > > At the moment, I'm applying R to some AIX 'nmon' output, trying to get > a handle on some disk performance metrics. In case anyone's > interested: > > http://docs.osg.ufl.edu/tsm/pdf/ > > some of them are more edifying than others. (ahem) > > I'm trying to develop a somewhat general framework for plotting these > measures, in the hopes that it's of some use to people other than me. > One obstacle I encounter is that, when I select one column out of a > data.frame, the result is no longer a data.frame. So, say I've got, > in data frame 'input' > > disk1 disk2 disk3 disk4 > T0000 0 1 0 4 > T0001 0 1 0 5 > T0002 0 1 0 5 > T0003 0 2 0 4 > T0004 0 2 0 3 > T0005 0 1 0 3 > T0006 0 0 0 3 > > and somewhere I've noted a list > > targets <- c('disk2','disk3') > > I can say > > input[,targets] > disk2 disk3 > T0000 1 0 > T0001 1 0 > T0002 1 0 > T0003 2 0 > T0004 2 0 > T0005 1 0 > T0006 0 0 > > but if > > targets <- c('disk2') > input[,targets] > [1] 1 1 1 2 2 1 0 > > Ick. > > I've been reading through the indexing and data.frame docs, and remain > unsatisfied so far. Where is my thinking going wrong? > > > > - Allen S. Rout > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
I think I understand this. Try str(input) and you will see that input$disk2 in a vector. You can force a data.frame using data.frame(input(,targets)) --- "Allen S. Rout" <asr at ufl.edu> wrote:> > Greetings. > > At the moment, I'm applying R to some AIX 'nmon' > output, trying to get > a handle on some disk performance metrics. In case > anyone's > interested: > > http://docs.osg.ufl.edu/tsm/pdf/ > > some of them are more edifying than others. (ahem) > > I'm trying to develop a somewhat general framework > for plotting these > measures, in the hopes that it's of some use to > people other than me. > One obstacle I encounter is that, when I select one > column out of a > data.frame, the result is no longer a data.frame. > So, say I've got, > in data frame 'input' > > disk1 disk2 disk3 disk4 > T0000 0 1 0 4 > T0001 0 1 0 5 > T0002 0 1 0 5 > T0003 0 2 0 4 > T0004 0 2 0 3 > T0005 0 1 0 3 > T0006 0 0 0 3 > > and somewhere I've noted a list > > targets <- c('disk2','disk3') > > I can say > > input[,targets] > disk2 disk3 > T0000 1 0 > T0001 1 0 > T0002 1 0 > T0003 2 0 > T0004 2 0 > T0005 1 0 > T0006 0 0 > > but if > > targets <- c('disk2') > input[,targets] > [1] 1 1 1 2 2 1 0 > > Ick. > > I've been reading through the indexing and > data.frame docs, and remain > unsatisfied so far. Where is my thinking going > wrong? > > > > - Allen S. Rout > > ______________________________________________ > 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. >
Henrique Dallazuanna
2008-Feb-09 18:03 UTC
[R] Subsetting a data.frame degenerates at one column?
Try this also: input[targets] On 08/02/2008, Allen S. Rout <asr at ufl.edu> wrote:> > Greetings. > > At the moment, I'm applying R to some AIX 'nmon' output, trying to get > a handle on some disk performance metrics. In case anyone's > interested: > > http://docs.osg.ufl.edu/tsm/pdf/ > > some of them are more edifying than others. (ahem) > > I'm trying to develop a somewhat general framework for plotting these > measures, in the hopes that it's of some use to people other than me. > One obstacle I encounter is that, when I select one column out of a > data.frame, the result is no longer a data.frame. So, say I've got, > in data frame 'input' > > disk1 disk2 disk3 disk4 > T0000 0 1 0 4 > T0001 0 1 0 5 > T0002 0 1 0 5 > T0003 0 2 0 4 > T0004 0 2 0 3 > T0005 0 1 0 3 > T0006 0 0 0 3 > > and somewhere I've noted a list > > targets <- c('disk2','disk3') > > I can say > > input[,targets] > disk2 disk3 > T0000 1 0 > T0001 1 0 > T0002 1 0 > T0003 2 0 > T0004 2 0 > T0005 1 0 > T0006 0 0 > > but if > > targets <- c('disk2') > input[,targets] > [1] 1 1 1 2 2 1 0 > > Ick. > > I've been reading through the indexing and data.frame docs, and remain > unsatisfied so far. Where is my thinking going wrong? > > > > - Allen S. Rout > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Allen S. Rout
2008-Feb-11 20:27 UTC
[R] Subsetting a data.frame degenerates at one column?
"jim holtman" <jholtman at gmail.com> writes:> try:> input[,targets, drop=FALSE]> see:> ?"["> for an explanation.Thanks, you who responded; this was exactly helpful, and a good reference to the part of the FM I was missing. To unpack (and demonstrate some comprehension gained.. ;) the subsetting operations on data frames, by default, use the most basic data type capable of representing the answer. Either the drop=FALSE or the inputs[targets] solution give me the result I had in mind. I mildly prefer the [targets] statement from a visual perspective. - Allen S. Rout