s-news
[Top] [All Lists]

ifelse and data.frames

To: "'s-news@lists.biostat.wustl.edu'" <s-news@lists.biostat.wustl.edu>
Subject: ifelse and data.frames
From: "Bliese, Paul D MAJ WRAIR-Wash DC" <Paul.Bliese@NA.AMEDD.ARMY.MIL>
Date: Thu, 29 Nov 2001 12:14:06 -0500
Curious about a problem that I'm having with S-PLUS 2000 in a Windows
environment.

I've been using ifelse to replace values in specific columns in a data.frame
called TEMP2 but the commands behave inconsistently.  Note the behavior
below:

> source("g:\\temp\\temp2.dat")  #get the TEMP2 data.frame (source file
attached to this message).
> is.data.frame(TEMP2)
[1] T
> dim(TEMP2)
[1] 1192    6
> apply(TEMP2[,2:3],2,summary.ordered)
     STR01 STR02 
   0   348   327
   1   224   126
   2   188   106
   3   155   110
   4   109    70
   5   108   388
NA's    60    65
> TEMP2[,c(2:3)]<-ifelse(TEMP2[,c(2:3)]>1,1,0)
> apply(TEMP2[,2:3],2,summary.ordered)
     STR01 STR02 
   0  1025  1025
   1  1234  1234
NA's   125   125

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
> dim(TEMP2)
[1] 1192    6
> apply(TEMP2[,2:3],2,summary.ordered)
     STR01 STR02 
   0   348   327
   1   224   126
   2   188   106
   3   155   110
   4   109    70
   5   108   388
NA's    60    65
> 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

Now it works!  Somehow data.framing a data.frame fixes things. I've been
wracking my brain trying to figure out what is going on here.  Any
suggestions?  I've attached the offending data.frame.

Paul Bliese
paul.bliese@na.amedd.army.mil



 <<temp2.dat>> 

Attachment: temp2.dat
Description: Binary data

<Prev in Thread] Current Thread [Next in Thread>
  • ifelse and data.frames, Bliese, Paul D MAJ WRAIR-Wash DC <=