Dear All, I am new to "R" and search for a solution to exclude cases if a certain variable contains NA for a case. Example No Name Turnover 1 Smith 1500 2 Mayor 200 3 Miller 4 Batic 750 I would like to create a subset excluding case 3 Miller NA. I tried to following: new_dataset <- subset(dataset, subset = Turnover != NA) This does not work. The new_dataset contains all variables but not cases are left. R responds "Variables with all observations missing". How could I do it right? Kind regards Georg [[alternative HTML version deleted]]
On Thu, Dec 17, 2015 at 01:50:29PM +0100, G.Maubach at weinwolf.de wrote:> Dear All, > > I am new to "R" and search for a solution to exclude cases if a certain > variable contains NA for a case. > ... > I would like to create a subset excluding case 3 Miller NA. > > I tried to following: > > new_dataset <- subset(dataset, subset = Turnover != NA) > > This does not work. The new_dataset contains all variables but not cases > are left. R responds "Variables with all observations missing". > > How could I do it right? > ....Please review the documentation for na.omit() (and related functions. Peace, david -- David H. Wolfskill r at catwhisker.org Those who would murder in the name of God or prophet are blasphemous cowards. See http://www.catwhisker.org/~david/publickey.gpg for my public key. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 603 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20151217/14e53d97/attachment.bin>
You need the is.na() function:> dataset <- data.frame(No = 1:4, Name = c("Smith", "Mayor", "Miller", "Baltic"), Turnover = c(1500, 200, NA, 750)) > datasetNo Name Turnover 1 1 Smith 1500 2 2 Mayor 200 3 3 Miller NA 4 4 Baltic 750 [1] TRUE TRUE FALSE TRUE> dataset[complete.cases(dataset), ]No Name Turnover 1 1 Smith 1500 2 2 Mayor 200 4 4 Baltic 750> dataset[!is.na(dataset$Turnover), ]No Name Turnover 1 1 Smith 1500 2 2 Mayor 200 4 4 Baltic 750> subset(dataset, !is.na(Turnover))No Name Turnover 1 1 Smith 1500 2 2 Mayor 200 4 4 Baltic 750 On Thu, Dec 17, 2015 at 7:50 AM, <G.Maubach at weinwolf.de> wrote:> Dear All, > > I am new to "R" and search for a solution to exclude cases if a certain > variable contains NA for a case. > > Example > > No Name Turnover > 1 Smith 1500 > 2 Mayor 200 > 3 Miller > 4 Batic 750 > > I would like to create a subset excluding case 3 Miller NA. > > I tried to following: > > new_dataset <- subset(dataset, subset = Turnover != NA) > > This does not work. The new_dataset contains all variables but not cases > are left. R responds "Variables with all observations missing". > > How could I do it right? > > Kind regards > > Georg >
use the 'is.na' function: new_dataset <- subset(dataset, subset = !is.na(Turnover)) Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Thu, Dec 17, 2015 at 7:50 AM, <G.Maubach at weinwolf.de> wrote:> Dear All, > > I am new to "R" and search for a solution to exclude cases if a certain > variable contains NA for a case. > > Example > > No Name Turnover > 1 Smith 1500 > 2 Mayor 200 > 3 Miller > 4 Batic 750 > > I would like to create a subset excluding case 3 Miller NA. > > I tried to following: > > new_dataset <- subset(dataset, subset = Turnover != NA) > > This does not work. The new_dataset contains all variables but not cases > are left. R responds "Variables with all observations missing". > > How could I do it right? > > Kind regards > > Georg > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]