Dr. Dalgaard, Thank you. But pre-checking with is.null() or using with() doesn't solve the problem of catching spelling mistakes in the name of a variable inside a data frame, when using the df$var notation often in a program.
Is there some way for R to behave, in relation to a variable inside a data frame, the same way it behaves for a variable not in a data frame? For example: ##---------------------------------------- a <- c(1,2,3) ## the variable exists, we get a correct answer a==1 ## the variable does not exist, R rightly points this out aaa==1 ##---------------------------------------- My point is, if we make a spelling mistake in a program when referring to a variable inside a data frame, using the df$var notation, there seems to be no way of getting warned about that. Thank you once again. Paulo Barata --------------------------------------------------------------------- ---------- Original Message ----------- From: peter dalgaard <pda...@gmail.com> To: "Paulo Barata" <paulo.bar...@ensp.fiocruz.br> Sent: Sun, 15 Jul 2012 16:47:35 +0200 Subject: Re: [R] variable (column) in a data frame > On Jul 15, 2012, at 16:30 , Paulo Barata wrote: > > > > > To the R help list, > > > > When using a data frame, there is no warning or error message > > when I refer to a non-existent variable inside the data frame. > > > > Example: > > > > ##---------------------------------------------- > > > > a <- c(1,2,3) > > b <- c(11,22,33) > > df <- data.frame(a,b) > > df > > > > ## correct: there is a column in df named 'a' > > ## the sum is correctly performed > > sum(df$a==2) > > > > ## incorrect: there is no column in df named 'aaa', > > ## but the sum is performed anyway without either warning or error > > sum(df$aaa==2) > > > > ##---------------------------------------------- > > > > Is there some way to make R issue either a warning or an error > > message in such a situation? > > > > You can pre-check for is.null(df$aaa) or use with(df, sum(aaa==2)). > > -- > Peter Dalgaard, Professor, > Center for Statistics, Copenhagen Business School > Solbjerg Plads 3, 2000 Frederiksberg, Denmark > Phone: (+45)38153501 > Email: pd....@cbs.dk Priv: pda...@gmail.com > > -- > This message has been scanned for viruses and > dangerous content by MailScanner, and is > believed to be clean. ------- End of Original Message ------- ______________________________________________ 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.