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 
                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'
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With