[R] replacing with NA

Petr PIKAL petr.pikal at precheza.cz
Thu May 17 15:18:40 CEST 2012


> 
> x[is.na(z)] <- NA
> 
> This might send you a nasty bug if x and z are different lengths
> though -- just a head's up.

Another option
x*!is.na(z)*z

Regards
Petr


> 
> Michael
> 
> On Wed, May 16, 2012 at 12:55 PM, Mintewab Bezabih
> <Mintewab.Bezabih at economics.gu.se> wrote:
> > Dear R users,
> >
> > I was wondering  how I can replace the values of a vector with the 
> values from in another vector in the same row
> >
> > For example, how can I replace the value of x below with NA when the 
> value of Z in the same row is NA?
> > x <-1:20
> > z<- c(11, 15, 17, 2, 18, 6, 7, NA, 12, 10,21, 25, 27, 12, 28, 16,17, 
NA, 12, 10)
> >
> >
> > Many thanks
> > Mintewab
> >
> > ________________________________________
> > Från: Mintewab Bezabih
> > Skickat: den 15 maj 2012 15:53
> > Till: r-help at r-project.org
> > Kopia: r-help at r-project.org
> > Ämne: missing observations
> >
> > Dear R users,
> >
> > I have missing observations in my data that I remove in my analysis. I 

> am able to run my codes alright but I want the non missing values to be 
> correctly identified and therefore want to tag my id vector along in my 
> results. Since the vector of ids has no role in the analysis, I dont 
know 
> how to include it.
> >
> >
> >
> > Here is my reprducable example:and my id is the vector I want to add 
to 
> the analysis somehow so that my missing values are identified. I cannot 
> use  na.action function and that is why I have to drop my missing 
> obesevations beforehand.
> >
> >
> > library(fields)
> > x <-1:20
> > y<- runif(20)
> > z<- c(11, 15, 17, 2, 18, 6, 7, NA, 12, 10,21, 25, 27, 12, 28, 16,
> > 17, NA, 12, 10)
> > id <-1:20
> >
> > mydataset<-data.frame(x, y, z)
> > temperature[complete.cases(mydataset),]
> >
> >  x<- temperature[, c(1)]
> > y<- temperature[, c(2)]
> > z<- temperature[, c(3)]
> >
> > tpsfit <- Tps(cbind(x, y), z, scale.type="unscaled")
> >
> >
> >
> >
> > Many thanks as always.
> > Regards,
> > Mintewab
> >
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list