[R] select and hold missing

Val v@lkremk @ending from gm@il@com
Wed Sep 12 22:38:40 CEST 2018


I have a data
dfc <- read.table( text= 'week v1 v2
  w1  11  11
  w1  .    42
  w1  31  32
  w2  31  52
  w2  41  .
  w3  51  82
  w2  11  22
  w3  11  12
  w4  21  202
  w1  31  72
  w2  71  52', header = TRUE, as.is = TRUE, na.strings=c("",".","NA") )

I want to create this new variable diff = v2-v1  and remove rows based
on this "diff" value as shown below.
dfc$diff <-  dfc$v2 - dfc$v1
I want to   remove row values  <=0  and any value greater than  >=
100   and keep all values including NAs
dfca      <- dfc[((dfc$diff) > 0) & ((dfc$diff) < 100), ]

 However, the result is not what I wanted. I want the output as follow,
  week v1 v2 diff
  w1 NA  42  NA
  w1 31 32    1
  w2 31 52   21
  w2 41  NA  NA
  w3 51 82   31
  w2 11 22   11
  w3 11 12    1
  w1 31 72   41

However, I got this,l. Why it is setting all row values  NA?
   week v1 v2 diff
  <NA> NA NA   NA
  w1 31 32    1
 w2 31 52   21
 <NA> NA NA   NA
  w3 51 82   31
  w2 11 22   11
  w3 11 12    1
  w1 31 72   41

Any help ?
Thank you.



More information about the R-help mailing list