我有一个脚本返回一个data.table
其中将包含一组列。我想重命名其中一些列,但是setnames
如果不是全部都存在则中断。有没有什么方法可以在不循环+错误捕获或与现有名称相交的情况下重命名?
iris.dt <- data.table(iris)
# First time works fine
setnames(iris.dt, c("Sepal.Length", "Sepal.Width"), c("length", "width"))
# Second time fails because columns no longer exist
setnames(iris.dt, c("Sepal.Length", "Sepal.Width"), c("length", "width"))
# Error in setnames(iris.dt, c("Sepal.Length", "Sepal.Width"), c("length",
# :Items of 'old' not found in column names: Sepal.Length,Sepal.Width
就像是setnames(..., allow=T)
将是理想的。
Edit:将此归档为Github 上的 FR https://github.com/Rdatatable/data.table/issues/1099.
As of data.table
v1.12.0(2019 年 1 月 13 日),这是一个论点setnames
:
setnames(..., skip_absent=TRUE) # FALSE by default.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)