[R] creating NAs for some values only
Bert Gunter
gunter.berton at gene.com
Sun Feb 13 16:09:12 CET 2011
Daniel:
1. Please read up on how to create reproducible examples to post in R,
e.g. via dput. What you inluded below is useless to reproduce your
example.
2. If I understand you correctly, the trick here is to get the
indices, e.g. with ?which:
# First create an example:
>mydata <- as.character(sample(20,rep=TRUE))
>mydata[sample(1:20,6)] <- "w"
> mydata
[1] "17" "16" "w" "16" "w" "15" "w" "17" "18" "w" "w" "16"
[13] "5" "20" "13" "3" "14" "12" "w" "10"
## Now use which() to get the indices of the non-w's and sample and
replace, etc.
>mydata[sample(which(mydata!="w"),5) ]<- NA
> mydata
[1] "17" "16" "w" "16" "w" NA "w" NA NA "w" "w" "16"
[13] "5" NA "13" NA "14" "12" "w" "10"
-- Bert
On Sun, Feb 13, 2011 at 5:39 AM, Daniel M. <danielmessay at yahoo.com> wrote:
> Hello,
>
> I have some data file, say, mydata
>
> 1,2,3,4,5,6,7
> 3,3,4,4,w,w,1
> w,3,6,5,7,8,9
> 4,4,w,5,3,3,0
>
> i want to replace some percentages of "mydata" file in to NAs for those values
> that are NOT w's. I know how to apply the percentage thing here but don't know
> how to select those values that are not "w"s. So far, i was able to do it but
> the result replaces the w's also which i do not want to.
>
> Here is my code that i tried to exclude those w's(within my short codes)
>
> ifelse(mydata[sample,var] != 'w',mydata[sm,var]<-NA,'w')
> Can any one help please?
>
> Thank you
>
> Daniel
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
Bert Gunter
Genentech Nonclinical Biostatistics
More information about the R-help
mailing list