[R] how to chage values in data frame to NA iside a function

Spencer Graves spencer.graves at pdf.com
Fri Feb 21 19:26:03 CET 2003


Thomas Blackwell's solution will also work if dropout(df$y) returns a 
logical vector of length = length(df$y).  This also allows more general 
conditions, e.g.,
	
	  select1 <- df[,1] > 0
	  select2 <- (select1) & (dr[,2] > 0)

	  df[select2, "y"] <- NA	

Spencer Graves

Thomas W Blackwell wrote:
> Petr  -
> 
> Does your function return "index" or return "y" after modifying y ?
> In the email, it looks as though it returns "index".  If so, the
> following should work:
> 
> 
>>df$y[ dropout(df$y) ] <- NA
> 
> 
> -  tom blackwell  -  u michigan medical school  -  ann arbor  -
> 
> 
> 
> On Fri, 21 Feb 2003, Petr Pikal wrote:
> 
> 
>>Dear all
>>
>>I have a function in which I would like to change some values to NA according to
>>some condition.
>>
>>dropout<-function(y, nahr=FALSE,...) {
>>
>><some stuff for computing an index>
>>
>>if (nahr) y[index]<<-NA
>>invisible(index)
>>
>>}
>>
>>in case y is a vector all works OK but if it is a part of data frame by calling
>>
>>dropout(df$y) or dropout(df[,number]) no change is done.
>>
>>Please can you help me what is wrong with my code?
>>
>>By the way
>>
>>idx<-dropout(df$y)
>>df$y[idx]<-NA
>>
>>works OK
>>
>>Thanks a lot beforehand
>>
>>Best regards.
>>
>>Petr Pikal
>>Precheza a.s., Nab?.Dr.E.Bene?e 24, 750 62 P?erov
>>tel: +420581 252 257 ; 724 008 364
>>petr.pikal at precheza.cz; p.pik at volny.cz
>>fax +420581 252 561
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list