【问题标题】:What is the most "data.table" way to assign values to a column of a data.table from a different data.table从不同的data.table为data.table的列分配值的最“data.table”方式是什么
【发布时间】:2016-04-07 22:18:13
【问题描述】:

我试图将 data.table b 中“True_value”列中的值分配到 data.table a 中的同名列中。我终于得到了一些工作,但我不确定 1)它为什么工作以及 2)是否有更好的方法。任何见解将不胜感激。是的,恕我直言,我已经阅读了一些 data.table。

require(data.table)
a=as.matrix(c("a","b","c"))
a=cbind(a,c("yes", "no", "maybe"))
a=cbind(a,c("NA","NA","NA"))
rownames(a)=c("one", "two","three")
colnames(a)=c("letter", "status", "True_value")

a=as.data.table(a)

b=as.data.table(c(3,13,42))
colnames(b)=c("True_value")

a[,True_value]
b[,True_value]

##this doesn't work
a[,True_value] = b[,True_value]

##this doesn't assign the values, but rather assigns the string, "True_value"
a[,"True_value"] = b[,"True_value"]

##this doesn't work
a[,.(True_value)] = b[,.(True_value)]

##none of these work

a[,.(True_value)] = unlist(b[,True_value])
a[,True_value] = unlist(b[,True_value])

##and yet this one works. Why does this work, and is there a better way to do this?
a[,"True_value"] = unlist(b[,True_value])

【问题讨论】:

    标签: r data.table


    【解决方案1】:

    您的示例是创建 data.table 的一种非常了不起的方式 :)

    require(data.table)
    
    a <- data.table(letter = c("a","b","c"),
                    status = c("yes", "no", "maybe"),
                    True_value = NA_real_)
    
    b <- data.table(True_value = c(3, 13, 42))
    
    # I am not sure if this is the most "data.table" way of doing this,
    # but it is readable at least:
    a[, True_value := b[, True_value]]
    

    如果你想以任意顺序匹配任意数量的列:

    require(data.table)
    require(dplyr)
    
    a <- data.table(V1 = c("a","b","c"),
                    V2 = c("yes", "no", "maybe"),
                    V3 = NA,
                    V4 = NA)
    
    b <- data.table(V4 = c(1, 2, 3),
                    ignore = c(99),
                    V3 = c(3, 13, 42))
    
    # Get positions of matching columns in a,
    # thanks to match can be in any order
    fromA <- match(names(b), names(a)) %>% na.omit()
    
    # Get positions of matching columns in b in the original order
    fromB <- which(names(b) %in% names(a))
    
    a[, fromA, with = FALSE]
    
    b[, fromB, with = FALSE]
    
    a[, fromA := 
        b[, fromB, with = FALSE],
      with = FALSE]
    
    print(a)
    

    【讨论】:

    • 而且,如果b 也有一个letter 列,它可以用于加入,并且只更新特定的行:a[ b, on=c("letter"), True_value := i.True_value]
    • @Atticus29 - 这个问题是给我的吗?在next version of data.table 中,on=.( letter ) 语法将可用(因此您不需要引号)。
    • @Atticus29,请通过datatable.r-forge.r-project.org/datatable-faq.pdf,在这种情况下会很有帮助,尤其是。第 1 节。
    • @M.D.该链接已过期(2014 年 10 月)。项目移至 github,常见问题在这里:github.com/Rdatatable/data.table/wiki/FAQ
    • 有人必须通知谷歌(我刚刚搜索了“data.table faq”并粘贴了第一个链接,抱歉)。顺便说一句,@Frank,您发布的链接下没有太多内容......
    猜你喜欢
    • 2014-11-15
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多