简体   繁体   English

从不同的data.table向data.table的列分配值的最“ data.table”方法是什么

[英]What is the most “data.table” way to assign values to a column of a data.table from a different data.table

I was trying to assign the values from column "True_value" in data.table b into the column of the same name in data.table a. 我试图将data.table b中“ True_value”列中的值分配给data.table a中相同名称的列中。 I finally did get something to work, but I'm not sure 1)why it worked and 2)whether there's a better way. 我终于得到了一些可以工作的东西,但是我不确定1)为什么有效,2)是否有更好的方法。 Any insight would be appreciated. 任何见识将不胜感激。 And yes, with respect, I have read up on data.table a little. 是的,相对而言,我已经对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])

Your example is a really amazing way of creating a data.table :) 您的示例是创建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]]

If you want to match any number of columns in any order: 如果要按任意顺序匹配任意数量的列:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用另一个 data.table 的相关值替换 data.table 列中的向量值的最有效方法是什么? - What is the most efficient way to replace a vector's values in a data.table's column with correlating values from another data.table? 在匹配来自第二个表的数据时,在`data.table`中创建向量列的最有效方法是什么? - What is the most efficient way to create a column of vectors in `data.table` when matching data from a second table? 在 data.table 中分配变量列名 - Assign a variable column name in data.table 将data.table列分配给R中的变量 - Assign a data.table column to a variable in R 将列表分配给data.table - Assign a list to data.table 使用来自另一个 data.table 列的值更新 data.table 列中值的子集 - Update a subset of values in data.table column with values from another data.table column 意外列删除 - 创建 data.table 备份和删除 data.table 列的正确方法是什么 - Accidental column deletion - What is the proper way of creating data.table backups and deleting data.table columns 使用列输入从函数分配data.table列 - assign data.table column from function with column inputs R: Add boolean column to a data.table based on return values of a function which evaluates two columns from different data.table - R: Add boolean column to a data.table based on return values of a function which evaluates two columns from different data.table 按列标准化:data.table方式 - Normalizing by column: the data.table way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM