[R] creating NAs for some values only

Sarah Goslee sarah.goslee at gmail.com
Sun Feb 13 15:35:52 CET 2011


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.

> mydata
  V1 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")
> mydata1
     V1  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
> mydata
    V1 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



More information about the R-help mailing list