Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the table function find a variable that was deleted

Tags:

r

r-table

Why does the table function find a variable that was deleted?

Dog <- c("Rover", "Spot")
Cat <- c("Scratch", "Fluffy")

Pets <- data.frame(Dog, Cat)  #create a data frame with two variables
names(Pets)
# [1] "Dog" "Cat"

#rename Dog to a longer name

names(Pets)[names(Pets)=="Dog"] <- "Dog_as_very_long_name"
Pets$Dog <- NULL # delete Dog  
names(Pets) 
#[1] "Dog_as_very_long_name" "Cat"  #the variable dog is not in the data set anymore

table(Pets$Dog)  #Why does the table function on a variable that was deleted


#  Rover  Spot 
#  1     1 
like image 223
user2502904 Avatar asked Jun 20 '13 15:06

user2502904


1 Answers

This is simply because of the partial matching that occurs in certain uses of $.

Try this:

> table(Pets$Ca)

 Fluffy Scratch 
      1       1 

Using the [[ notation instead will give you more control.

> table(Pets[["Ca"]])
< table of extent 0 >
> table(Pets[["Ca", exact = FALSE]])

 Fluffy Scratch 
      1       1 

You can use the options settings to give a warning when partial matches are used. Consider:

> options(warnPartialMatchDollar = TRUE)
> table(Pets$Ca)

 Fluffy Scratch 
      1       1 
Warning message:
In Pets$Ca : partial match of 'Ca' to 'Cat'
> table(Pets$Dog)

Rover  Spot 
    1     1 
Warning message:
In Pets$Dog : partial match of 'Dog' to 'Dog_as_very_long_name'
like image 94
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 15 '22 01:11

A5C1D2H2I1M1N2O1R2T1