Avoiding error when using rename in dplyr and column doesn't exist

前端 未结 7 1974
暖寄归人
暖寄归人 2021-02-07 03:15

Is there a clever way to use the rename function in dplyr when in some instances the column to be renamed doesn\'t exist?

For example, I would like the following not to

7条回答
  •  一整个雨季
    2021-02-07 03:37

    The plyr package has a rename() function with a warn_missing parameter.

    plyr::rename(
      mtcars, 
      replace      = c(mpg="miles_per_gallon", foo="missing_varible"),
      warn_missing = FALSE
    )
    

    If you use it, consider using requireNamespace() instead of library(), so its function names don't collide with dplyr's.

提交回复
热议问题