Determine which column name is causing 'undefined columns selected' error when using subset()

亡梦爱人 提交于 2019-12-05 12:31:45

Find the elements of your vector that are not %in% the names() of your data frame.

Working example:

dd <- data.frame(a=1,b=2)
subset(dd,select=c("a"))
##   a
## 1 1

Now try something that doesn't work:

v <- c("a","d")
subset(dd,select=v)
## Error in `[.data.frame`(x, r, vars, drop = drop) : 
##    undefined columns selected

v[!v %in% names(dd)]
## [1] "d"

Or

setdiff(v,names(dd))
## [1] "d"

The last few lines of the example code in ?match show a similar case.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!