Strange behaviour dropping column from data.frame in R

后端 未结 4 1357
逝去的感伤
逝去的感伤 2020-12-18 15:52

I\'ve encountered a strange behavior when dropping columns from data.frame. Initially I have:

> a <- data.frame(\"a\" = c(1,2,3), \"abc\" = c(3,2,1));          


        
4条回答
  •  一整个雨季
    2020-12-18 16:36

    While your exact question has already been answered in the comments, an alternative to avoid this behaviour is to convert your data.frame to a tibble, which is a stripped downed version of a data.frame, without column name munging, among other things:

    library(tibble)
    df_t <- as_data_frame(a)
    df_t
    # A tibble: 3 × 1
        abc
      
    1     3
    2     2
    3     1
    > df_t$a
    NULL
    Warning message:
    Unknown column 'a' 
    

提交回复
热议问题