Hi All- I have a data set (spdco2) > spdco2 [,1] [,2] [,3] [1,] 1 5.4 382.4212 [2,] 2 5.1 383.0315 [3,] 3 4.8 383.9520 [4,] 4 4.7 384.4376 [5,] 5 4.7 384.5929 [6,] 6 4.4 384.8864 [7,] 7 4.1 385.2156 [8,] 8 3.8 385.2919 [9,] 9 3.7 385.5925 [10,] 10 3.9 385.6801 I am subsetting it to output when [,2] is >= 4.7 . x<-subset(spdco2,spdco2[,2]>=4.7) This works, but I would like to add 'NA' to the data that it does exclude based on my subset criteria. I have searched through the archives and two R books, but I cannot figure out how to add the NA. Any suggestions would be appreciated- thanks! sherri x<-subset(spdco2,spdco2[,2]>=4.7)
Dear Sherri, Perhaps: spdco2=matrix(scan(),ncol=3,byrow=TRUE) 1 5.4 382.4212 2 5.1 383.0315 3 4.8 383.9520 4 4.7 384.4376 5 4.7 384.5929 6 4.4 384.8864 7 4.1 385.2156 8 3.8 385.2919 9 3.7 385.5925 10 3.9 385.6801 spdco2[,2]<-ifelse(spdco2[,2]>=4.7,spdco2[,2],NA) spdco2 HTH, Jorge On Thu, Sep 4, 2008 at 3:21 PM, Sherri Heck <sheck@ucar.edu> wrote:> Hi All- > > I have a data set (spdco2) > > > spdco2 > [,1] [,2] [,3] > [1,] 1 5.4 382.4212 > [2,] 2 5.1 383.0315 > [3,] 3 4.8 383.9520 > [4,] 4 4.7 384.4376 > [5,] 5 4.7 384.5929 > [6,] 6 4.4 384.8864 > [7,] 7 4.1 385.2156 > [8,] 8 3.8 385.2919 > [9,] 9 3.7 385.5925 > [10,] 10 3.9 385.6801 > > > I am subsetting it to output when [,2] is >= 4.7 . > x<-subset(spdco2,spdco2[,2]>=4.7) > > This works, but I would like to add 'NA' to the data that it does exclude > based on my subset criteria. I have searched through the archives and two R > books, but I cannot figure out how to add the NA. > Any suggestions would be appreciated- > > thanks! > > sherri > > > > > > x<-subset(spdco2,spdco2[,2]>=4.7) > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Sherri Heck wrote:> Hi All- > > I have a data set (spdco2) > > > spdco2 > [,1] [,2] [,3] > [1,] 1 5.4 382.4212 > [2,] 2 5.1 383.0315 > [3,] 3 4.8 383.9520 > [4,] 4 4.7 384.4376 > [5,] 5 4.7 384.5929 > [6,] 6 4.4 384.8864 > [7,] 7 4.1 385.2156 > [8,] 8 3.8 385.2919 > [9,] 9 3.7 385.5925 > [10,] 10 3.9 385.6801 > > > I am subsetting it to output when [,2] is >= 4.7 . > > x<-subset(spdco2,spdco2[,2]>=4.7) > > This works, but I would like to add 'NA' to the data that it does > exclude based on my subset criteria. I have searched through the > archives and two R books, but I cannot figure out how to add the NA. > Any suggestions would be appreciated-Use is.na(): x <- subset(spdco2, spdco2[,2] >= 4.7 | is.na(spdco2[,2])) Make sure that the inclusion or exclusion of the row can be computed: TRUE | NA is TRUE, but FALSE | NA is NA. Duncan Murdoch Duncan Murdoch
Try this: is.na(spdco2[,2]) <- which(spdco2[,2] < 4.7) spdco2 On Thu, Sep 4, 2008 at 4:21 PM, Sherri Heck <sheck@ucar.edu> wrote:> Hi All- > > I have a data set (spdco2) > > > spdco2 > [,1] [,2] [,3] > [1,] 1 5.4 382.4212 > [2,] 2 5.1 383.0315 > [3,] 3 4.8 383.9520 > [4,] 4 4.7 384.4376 > [5,] 5 4.7 384.5929 > [6,] 6 4.4 384.8864 > [7,] 7 4.1 385.2156 > [8,] 8 3.8 385.2919 > [9,] 9 3.7 385.5925 > [10,] 10 3.9 385.6801 > > > I am subsetting it to output when [,2] is >= 4.7 . > x<-subset(spdco2,spdco2[,2]>=4.7) > > This works, but I would like to add 'NA' to the data that it does exclude > based on my subset criteria. I have searched through the archives and two R > books, but I cannot figure out how to add the NA. > Any suggestions would be appreciated- > > thanks! > > sherri > > > > > > x<-subset(spdco2,spdco2[,2]>=4.7) > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]