[R] Selective transformation

Allan Kamau kamauallan at yahoo.com
Tue Aug 21 19:04:31 CEST 2007



----- Original Message ----
From: Chuck Cleland <ccleland at optonline.net>
To: Allan Kamau <kamauallan at yahoo.com>
Cc: r-help at stat.math.ethz.ch
Sent: Tuesday, August 21, 2007 6:48:53 PM
Subject: Re: [R] Selective transformation

Allan Kamau wrote:
> I am looking for a way to transform select observations based on a value based criteria.
> Why - Am learning r and would like to perform regression analysis of given variables of the babies dataset (part of UsingR) for example babies$wt1, the data in the variables does contain values which should be interpreted as unknown, some variables have 999 for unknown and some have 99 for the same, since lm() expects not available data to be marked using NA.
> I would like to use a solution that does not employ loops (I think it may not be the ideal way)
> 
> I am looking at using apply() and supply the name of my function responsible for transformation, but am unable to know now to reference the element of the vector/list being currently processed by apply() so I may do in place substitution (if value is 99 or 999) of the value with NA.

  Does this do what you want?

babies$wt1 <- with(babies, replace(wt1, wt1 == 999, NA))

?replace




Thanks Chuck, the replace command is just what I was looking for.

wt1<-babies$wt1
wt1<-replace(wt1,wt1==999,NA)

I get NA in wt1 vector in place of 999




> ____________________________________________________________________________________
> Pinpoint customers who are looking for what you sell.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894






      ____________________________________________________________________________________
Luggage? GPS? Comic books?



More information about the R-help mailing list