Thank you once again.
Paulo Barata
---------------------------------------------------------------------
---------- Original Message -----------
From: peter dalgaard <pdalgd at gmail.com>
To: "Paulo Barata" <paulo.barata at 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.mes at cbs.dk Priv: PDalgd at gmail.com
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
------- End of Original Message -------