深层复制data.frame并强制转换为R

时间:2018-02-13 17:59:29

标签: r data.table

我有一个使用data.table的R函数,但接受data.frame作为输入,我不想通过引用修改输入data.frame。

为此,我首先对数据框进行“深层复制”,例如x2 <- data.frame(x)然后data.frame::setDT(x2)

使用data.table函数有更优雅或直接的方法吗?

最小例子:

x <- data.frame(a=1:5,b=1:5)

fun1 <- function(x) {
  x2 <- data.frame(x) #'deep' copy
  data.table::setDT(x2)
  return(x2)
}

x2 <- fun1(x)

class(x)
  

[1]“data.frame”

class(x2)
  

[1]“data.table”“data.frame”

请注意x2 <- data.table::copy(x)会将x强制转换为data.table。

1 个答案:

答案 0 :(得分:2)

这曾经是标准的强制方法(连同"<-"),但后来发明了setDT;

 x3 <- as.data.table(x)

> class(x)
[1] "data.frame"
> class(x2)
[1] "data.table" "data.frame"

如果您想查看内部信息,请点击此处查看以下代码:

> as.data.table
function (x, keep.rownames = FALSE, ...) 
{
    if (is.null(x)) 
        return(null.data.table())
    UseMethod("as.data.table")
}
<bytecode: 0x7fd9597d7d10>
<environment: namespace:data.table>
> methods(as.data.table)
 [1] as.data.table.character*  as.data.table.data.frame* as.data.table.data.table*
 [4] as.data.table.Date*       as.data.table.default*    as.data.table.factor*    
 [7] as.data.table.integer*    as.data.table.ITime*      as.data.table.list*      
[10] as.data.table.logical*    as.data.table.matrix*     as.data.table.numeric*   
[13] as.data.table.ordered*    as.data.table.table*      as.data.table.xts*       
see '?methods' for accessing help and source code
> getAnywhere(as.data.table.data.frame)
A single object matching ‘as.data.table.data.frame’ was found
It was found in the following places
  registered S3 method for as.data.table from namespace data.table
  namespace:data.table
with value

function (x, keep.rownames = FALSE, ...) 
{
    if (!identical(keep.rownames, FALSE)) {
        ans = data.table(rn = rownames(x), x, keep.rownames = FALSE)
        if (is.character(keep.rownames)) 
            setnames(ans, "rn", keep.rownames[1L])
        return(ans)
    }
    ans = copy(x)
    setattr(ans, "row.names", .set_row_names(nrow(x)))
    setattr(ans, "class", .resetclass(x, "data.frame"))
    alloc.col(ans)
}
<bytecode: 0x7fd956a0ed10>
<environment: namespace:data.table>