Why does a table function find a variable that has been deleted

Why does a table function find a variable that has been 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 
+6
source share
1 answer

This is simply due to the partial match that occurs with some uses of $ .

Try the following:

 > 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' 
+11
source

Source: https://habr.com/ru/post/947756/


All Articles