[R] variable (column) in a data frame

arun smartpink111 at yahoo.com
Sun Jul 15 18:56:10 CEST 2012


Hi,

I guess you can try this:

#You will get the same result here:

 df$aaa==2
logical(0)
!df$aaa==2
logical(0)
#But it is different for the variable present in the dataframe

 df$a==4
[1] FALSE FALSE FALSE
 !df$a==4
[1] TRUE TRUE TRUE
 identical(df$aaa==2,!df$aaa==2)
[1] TRUE
 identical(df$a==4,!df$a==4)
[1] FALSE


A.K.






----- Original Message -----
From: Paulo Barata <paulo.barata at ensp.fiocruz.br>
To: r-help at r-project.org
Cc: 
Sent: Sunday, July 15, 2012 10:30 AM
Subject: [R] variable (column) in a data frame


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?

I am using R version 2.15.1 64-bit on Windows 7 Professional.

Thank you very much.

Paulo Barata

---------------------------------------------------------------------
Paulo Barata

ENSP - Fundação Oswaldo Cruz
Rua Leopoldo Bulhões 1480 - 8A
21041-210  Rio de Janeiro - RJ
Brazil
E-mail: paulo.barata at ensp.fiocruz.br

______________________________________________
R-help at 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.




More information about the R-help mailing list