r - 根据两个 data.frames/data.tables 计算因子级别的新列
问题描述
我正在尝试计算 data.table 的新列的值dt
。计算的一部分来自 data.frame df
(也可以是 data.table,到目前为止我还不需要它)。
sample
如果因子级别(此处:)匹配,如何使用来自两个不同对象的值来计算新列?我曾经合并两个对象并逐行进行,但这会导致大量冗余数据。
这是 data.frame,它只有 10 行:
df
sample scaling_factor
A1 A1 111956565
A2 A2 89869320
A3 A3 120925219
A4 A4 111757559
A5 A5 77319341
A6 A6 89403194
A7 A7 150214981
B8 B8 133885925
B9 B9 86536587
B10 B10 123574939
df <- structure(list(sample = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
9L, 10L, 8L), .Label = c("A1", "A2", "A3", "A4", "A5", "A6",
"A7", "B10", "B8", "B9"), class = "factor"), scaling_factor = c(111956565.427018,
89869319.9348599, 120925219.4453, 111757558.886234, 77319340.5841949,
89403194.1170576, 150214980.784589, 133885925.080984, 86536586.7136393,
123574939.026597)), .Names = c("sample", "scaling_factor"), class = "data.frame", row.names = c("A1",
"A2", "A3", "A4", "A5", "A6", "A7", "B8", "B9", "B10"))
这是data.table,每个样本有数十万行(dput<
在输出中有问题,所以这里不提供):
setDT(dt)
sample contig_id product_reads_rpk
1: A1 contig_10 2000.00000
2: A1 contig_100 24.27184
3: A1 contig_1000 1713.90374
4: A1 contig_10000 2900.66225
5: A1 contig_100003 1713.94231
6: A1 contig_100004 8575.23511
7: A1 contig_100004 11059.32203
8: A2 contig_100009 6923.67400
9: A2 contig_100010 1285.30259
10: A2 contig_100015 84.74576
dt[,product_rpm := product_reads_rpk/(df$scaling_factor/1000000), by = sample]
我正在尝试product_rpm
根据 .dt 中每个样本的相应值在 dt中生成一个新列df
。我怎么做?我得到longer object length is not a multiple of shorter object length
但较短的对象长度是 1 例如A1
在 df 中,对吗?
解决方案
我不知道在没有实际合并两个数据集的情况下执行此操作的方法 - 但如果您使用data.table
合并数据集的方式,则可以避免创建冗余列。
所以,在你的情况下,它只是:
df <- data.table(df)
dt[df, product_rpm := (product_reads_rpk/scaling_factor/1000000), on = "sample"]
一个简单的例子:
library(data.table)
dt1 <- data.table(id = sample(1000:9999, size = 100),
size = sample(10000:99999, size = 100))
dt2 <- data.table(id = rep(dt1$id, 10),
group = rep(LETTERS[1:5], 10),
value = sample(1000:9999, size = 100 * 10, replace = T))
dt3 <- dt2[dt1, metric:= (value / size), on = "id"]
head(dt3)
推荐阅读
- java - 如何使用在应用程序的 main 方法中初始化的 Spring 提取 Hibernate DB 记录
- sql - PostgreSQL 函数进行计算然后填充表
- idris - TDD 书中的堆栈代码:尝试删除重复的代码
- python - 如何通过子进程向不同的 Python 文件发送和接收变量?
- reactjs - 如何通过获取和重定向来解析 url 而无需通过反应刷新
- python - 如何使用 python 将 bash 命令发送到命令行并执行下一个命令而不等待 bash 命令执行?
- ios - 在 TextEditor swift 上显示 UIPickerView 值的问题
- javascript - 单击时向另一个页面发出 JavaScript 警报
- tensorflow - 改变 Dense 中中间神经元的数量
- r - 给定 R 中的 Y 输出预测 X 变量