复制偶数列并从2 data.tables&中删除奇数列。创造第三个

时间:2016-12-29 16:54:53

标签: r data.table subtraction

我需要一个新的DT(DT_final_LW_corr_baseline_sub),它应该具有偶数列DT_resampled_final的偶数列和奇数列的DT_resampled_finalbaseline_final之间的减法。所有DT都具有相同的尺寸。在这里我的尝试有效:

# Subtract baseline
DT_final_LW_corr_baseline_sub <- DT_resampled_final
i==1
for(i in 1:(numLW*2)){
  if (!i %% 2==FALSE) 
    DT_final_LW_corr_baseline_sub[[i]] <- DT_resampled_final[[i]]
  else {
    DT_final_LW_corr_baseline_sub[[i]] <- DT_resampled_final[[i]]-baseline_final[[i]]
  }
}

有没有办法以更短的方式做到这一点?例如,不使用循环。当我第一次学习编写代码时,我希望在开始时收到一些反馈,以避免陷入不良习惯。

> dput(DT_resampled_final)
structure(list(`Raman shift (cm-1)` = c(50, 51, 52, 53, 54, 55, 
56, 57, 58), HKU47_PSG_1_LW_0.txt = c(2.79147734046395, 3.12736782243076, 
3.48956530852053, 3.91369820172483, 4.38198578134454, 4.88017327696127, 
5.39373038518585, 5.90764948375938, 6.41547186798507), `Raman shift (cm-1)` = c(50, 
51, 52, 53, 54, 55, 56, 57, 58), HKU47_PSG_1_LW_1.txt = c(2.80309121136158, 
3.13172873120713, 3.49064185427554, 3.92083378212472, 4.38170006196362, 
4.86333787529566, 5.41898234137124, 5.96816896624743, 6.45359586105501
), `Raman shift (cm-1)` = c(50, 51, 52, 53, 54, 55, 56, 57, 58
), HKU47_PSG_1_LW_2.txt = c(2.81531357970881, 3.13794190850462, 
3.49352178106289, 3.92668050936782, 4.39913358215528, 4.89819532764696, 
5.44873800963596, 5.99763728207632, 6.51413470312863), `Raman shift (cm-1)` = c(50, 
51, 52, 53, 54, 55, 56, 57, 58), HKU47_PSG_1_LW_3.txt = c(2.77300320344437, 
3.11330661799265, 3.4831985430277, 3.92275162880708, 4.39475831745469, 
4.8887413823001, 5.42293580079945, 5.95922687586664, 6.48438977785762
)), .Names = c("Raman shift (cm-1)", "HKU47_PSG_1_LW_0.txt", 
"Raman shift (cm-1)", "HKU47_PSG_1_LW_1.txt", "Raman shift (cm-1)", 
"HKU47_PSG_1_LW_2.txt", "Raman shift (cm-1)", "HKU47_PSG_1_LW_3.txt"
), row.names = c(NA, -9L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x0000000000100788>)

> dput(baseline_final)
structure(list(baseline_final = c(50, 51, 52, 53, 54, 55, 56, 
57, 58), HKU47_PSG_1_LW_0.txt = c(2.57151934485739, 2.98291744761488, 
3.42744260714059, 3.8986361182227, 4.39003927564949, 4.89519337420913, 
5.40763970869006, 5.92091957388055, 6.42857426456877), V2 = c(50, 
51, 52, 53, 54, 55, 56, 57, 58), HKU47_PSG_1_LW_1.txt = c(4.02555110741741, 
3.72997378256628, 3.71798587071498, 3.93228548539923, 4.3155707401479, 
4.81053974849669, 5.35989062397721, 5.90632148012082, 6.39253043046165
), V2 = c(50, 51, 52, 53, 54, 55, 56, 57, 58), HKU47_PSG_1_LW_2.txt = c(3.2465748229348, 
3.32636445475191, 3.56085310009485, 3.91928231791712, 4.37089366717112, 
4.88492870681023, 5.43062899578865, 5.97723609305751, 6.49399155757146
), V2 = c(50, 51, 52, 53, 54, 55, 56, 57, 58), HKU47_PSG_1_LW_3.txt = c(2.77906856332555, 
3.08807780450024, 3.46763774953217, 3.90452356871538, 4.38551043234384, 
4.89737351071187, 5.42688797411364, 5.96082899284301, 6.48597173719406
)), .Names = c("baseline_final", "HKU47_PSG_1_LW_0.txt", "V2", 
"HKU47_PSG_1_LW_1.txt", "V2", "HKU47_PSG_1_LW_2.txt", "V2", "HKU47_PSG_1_LW_3.txt"
), row.names = c(NA, -9L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x0000000000100788>)

1 个答案:

答案 0 :(得分:1)

如果没有可重复的例子,我提供的不仅仅是建议:

连接所需的两个表,然后dcast

set.seed(321048)
DT1 = data.table(V1 = rnorm(10), V2 = rnorm(10))
DT2 = data.table(V3 = rnorm(10), V4 = rnorm(10))

dcast(rbind(DT1, DT1 - DT2, idcol = "id"), 
      rowid(id) ~ id, value.var = c("V1", "V2"))

请注意,列顺序不应该真正意味着什么。在极少数情况下,有setcolorder