Building on the question how to replace NA with 0. My data set below has date, station 1, flags for station 1, station 2, flags for station 2, etc...
I would like to make the values in the station columns equal to 1 and the NA in the station columns equal to 0 and then sum each row for the number of 1 present in the row. head(data.matrix, n=10) date 05AE005 flg_05AE005 05AF010 flg_05AF010 05BM014 flg_05BM014 1 1900-01-01 NA <NA> NA <NA> NA <NA> 2 1900-01-02 NA <NA> NA <NA> .23 <NA> 3 1900-01-03 NA <NA> NA <NA> .45 <NA> 4 1900-01-04 NA <NA> NA <NA> NA <NA> 5 1900-01-05 NA <NA> NA <NA> NA <NA> 6 1900-01-06 NA <NA> NA <NA> NA <NA> 7 1900-01-07 0.75 <NA> .09 <NA> NA <NA> 8 1900-01-08 0.87 <NA> .23 <NA> NA <NA> 9 1900-01-09 0.26 <NA> .78 <NA> NA <NA> 10 1900-01-10 0.23 <NA> NA <NA> NA <NA> # figure out which columns the data are in colpos <- seq(2, by = 2, length.out = n) # make value 1 and NA 0 colpos[!is.na(colpos)] <- 1.0 colpos[is.na(colpos)] <- 0.0 # sum number of values on a given day rowsum(colpos, ) The above script is what i have tried - and does not work. The values are not being replaced with 1s and the NAs with 0s and the rows are not being summed. any help would be greatly appreciated. E -- View this message in context: http://n4.nabble.com/replace-NA-value-with-0-tp834446p1596779.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.