对不等大数据表的操作(+, - ,/,*) [英] operations (+, -, /, *) on unequal-sized data.table

查看:116
本文介绍了对不等大数据表的操作(+, - ,/,*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)可以使用 data.table 在不等大的data.tables之间进行操作(乘法,除法,加法,减法),否则它必须使用 data.frame



执行以下示例是我原始发布的简化版本。在我的实际数据集中,它将是A1:A12,B1:B12,C1:C12,E1:E12,F1:F12等。我在列J和K中添加了接近我的原始数据集,显示我不能在矩阵中执行以下操作。

 #示例数据
library(data.table)
input1a< - data.table(ID = c(37,45,900),
A1 = c(1,2,3),
A2 = c(43,320,390 ),
B1 = c(-0.94,2.2,-1.223),
B2 = c(2.32,4.54,7.21),
C1 = c(1,2,3) b $ b C2 = c(-0.94,2.2,-1.223),
D = c(43,320,390),
J = paste0(measurement_1,1:3) $ b K = paste0(type_1,1:3))
setkey(input1a,ID)
input1a
#ID A1 A2 B1 B2 C1 C2 DJK
# 37 1 43 -0.940 2.32 1 -0.940 43 measurement_11 type_11
#2:45 2 320 2.200 4.54 2 2.200 320 measurement_12 type_12
#3:900 3 390 -1.223 7.21 3 -1.223 390 measurement_13 type_13

input2a < - data.table(ID = c(37,45,900),
E1 = c(23,-0.2,12),
E2 = c 0.33,-0.012,-1.342))
setkey(input2a,ID)
input2a
#ID E1 E2
#1:37 -0.6135756 -0.330
#2 :45 -0.0124872 -0.012
#3:900 -0.4165049 -1.342






  outputa <-0.00066 * input1a [,c(4:5),with = FALSE] * 
input1a [,8,with = FALSE ] * input2a [,c(2:3),with = FALSE]#no keys,but be
#like to keep the keys
#outputa <-0.00066 * B1:B2 * D * A1 :A2返回列名
setnames(outputa,2:3,c(F1,F2))

使用 outputa 结果

  outputa#使用现有代码并给出没有键的结果
#F1 F2
#1:-0.6135756 -0.02172773
#2:-0.0929280 -0.01150618
#3:-3.7776024 -2.49055607

在下面的代码中,我使用 outputa ,但没有保留这些键,并将 outputa 重写为 outputause 。我想回答以下问题,以便我可以对数据集执行所需的操作,同时保持密钥不变。



2)如何以下代码使用为每组列定义的 x 重写?此问题源自按组的变量的加权总和与data.table ,我无法使用我的数据集复制任何答案。



每组栏位定义如下:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆