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]]
Daniel, I think you might benefit from (re)reading the Intro to R materials, since you seem to misunderstand both ifelse() and possibly how subsetting works. I can't provide an actual example since I have no idea what sample or var or sm are (and it's a VERY good idea to not use names for variables that are already the names of functions). But here are two possible ways to change "w" values to NA. Note that they have different effects on the column class in a dataframe.> mydataV1 V2 V3 V4 V5 V6 V7 1 1 2 3 4 5 6 7 2 3 3 4 4 w w 1 3 w 3 6 5 7 8 9 4 4 4 w 5 3 3 0> str(mydata)'data.frame': 4 obs. of 7 variables: $ V1: chr "1" "3" "w" "4" $ V2: int 2 3 3 4 $ V3: chr "3" "4" "6" "w" $ V4: int 4 4 5 5 $ V5: chr "5" "w" "7" "3" $ V6: chr "6" "w" "8" "3" $ V7: int 7 1 9 0> # using ifelse() > mydata1 <- ifelse(mydata != "w", NA, "w") > mydata1V1 V2 V3 V4 V5 V6 V7 [1,] NA NA NA NA NA NA NA [2,] NA NA NA NA "w" "w" NA [3,] "w" NA NA NA NA NA NA [4,] NA NA "w" NA NA NA NA> str(mydata1)chr [1:4, 1:7] NA NA "w" NA NA NA NA NA NA NA NA ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:7] "V1" "V2" "V3" "V4" ...> # using direct subsetting > mydata[mydata != "w"] <- NA > mydataV1 V2 V3 V4 V5 V6 V7 1 <NA> NA <NA> NA <NA> <NA> NA 2 <NA> NA <NA> NA w w NA 3 w NA <NA> NA <NA> <NA> NA 4 <NA> NA w NA <NA> <NA> NA> str(mydata)'data.frame': 4 obs. of 7 variables: $ V1: chr NA NA "w" NA $ V2: int NA NA NA NA $ V3: chr NA NA NA "w" $ V4: int NA NA NA NA $ V5: chr NA "w" NA NA $ V6: chr NA "w" NA NA $ V7: int NA NA NA NA On Sun, Feb 13, 2011 at 8: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 > >-- Sarah Goslee http://www.functionaldiversity.org
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