s-news
[Top] [All Lists]

Answer to ifelse and data.frames

To: "'s-news@lists.biostat.wustl.edu'" <s-news@lists.biostat.wustl.edu>
Subject: Answer to ifelse and data.frames
From: "Bliese, Paul D MAJ WRAIR-Wash DC" <Paul.Bliese@NA.AMEDD.ARMY.MIL>
Date: Fri, 30 Nov 2001 12:10:51 -0500
Sorry about the apparent faux-pax of attaching my data to my previous post.

Thanks to those who solved my problem.  In a nutshell, my first TEMP2 was a
data.sheet, and my second TEMP2 (i.e., data.frame(TEMP2)) was a data.frame.
Apparently, using the ifelse command on a data.sheet is something to be
avoided.

Bill Dunlap provided the most comprehensive answer (listed below), but a
couple of other people suspected data.sheets.  What I've learned is that it
might be valuable to check the class attributes of my data.frame prior to
using ifelse (as in >class(TEMP2)).


Paul

Bill's message follows:

*****************************************
On Thu, 29 Nov 2001, Bliese, Paul D MAJ WRAIR-Wash DC wrote: 

> Note that this does NOT work -- the results are incorrect.  However, one 
> simple change fixes things. 
> 
> > source("g:\\temp\\temp2.dat") 
> > TEMP2<-data.frame(TEMP2)  # THIS IS THE CRITICAL CHANGE!!!!! 
> > is.data.frame(TEMP2) 
> [1] T 

The original TEMP2 is a "data.sheet" and data.sheets may have 
columns of different lengths.  The class data.sheet is a subsclass 
of data.frame so is.data.frame(TEMP2) is TRUE. 

> > TEMP2[,c(2:3)]<-ifelse(TEMP2[,c(2:3)]>1,1,0) 
> > apply(TEMP2[,2:3],2,summary.ordered) 
>      STR01 STR02 
>    0   572   453 
>    1   560   674 
> NA's    60    65 

ifelse(TEMP2[,c(2:3)]>1,1,0) produces a vector whose length is 
twice the length of a column of TEMP2.  Since columns of data frames 
must be the same length [<-.data.frame converts that long vector to 2
columns, 
each having half the vector.  Since data sheets can have columns 
of variable lengths [<-.data.sheet takes the input vector as is 
and makes it the value of both columns. 

----------------------------------------------------------------------------

Bill Dunlap                                      22461 Mt Vernon-Big Lake Rd

Insightful Corporation                           Mount Vernon, WA 98274 
bill@insightful.com                              360-428-8146 




<Prev in Thread] Current Thread [Next in Thread>