Fixing a multiple warning “unknown column”

前端 未结 8 2045
你的背包
你的背包 2020-12-12 12:12

I have a persistent multiple warning of \"unknown column\" for all types of commands (e.g., str(x) to installing updates on packages), and not sure how to debug this or fix

8条回答
  •  猫巷女王i
    2020-12-12 13:02

    I have been encountering the same problem, and although I don't know why it occurs, I have been able to pin down when it occurs, and thus prevent it from happening.

    The issue seems to be with adding in a new column, derived from indexing, in a base R data frame vs. in a tibble data frame. Take this example, where you add a new column (age) to a base R data frame:

    base_df <- data.frame(id = c(1:3), name = c("mary", "jill","steve"))
    
    base_df$age[base_df$name == "mary"] <- 47
    

    That works without returning a warning. But when the same is done with a tibble, it throws a warning (and consequently, I think causing the weird, seemingly unprovoked, multiple warning issue):

    library(tibble)
    
    tibble_df <- tibble(id = c(1:3), name = c("mary", "jill","steve"))
    
    tibble_df$age[tibble_df$name == "mary"] <- 47
    
    Warning message:
    Unknown column 'age' 
    

    There are surely better ways of avoiding this, but I have found that first creating a vector of NAs does the job:

    tibble_df$age <- NA
    
    tibble_df$age[tibble_df$name == "mary"] <- 47
    

提交回复
热议问题