运行 进入错误强制 data.frame 为数值

Running into error coercing data.frame to numerical values

我这里有一个名为 "matched_SNPs" 的 data.frame:

  SNP               ACB               ASW               BEB          EFF
rs10007883 0.536458333333333 0.549180327868853 0.191860465116279    -0.005748
rs10009522 0.604166666666667 0.475409836065574 0.162790697674419     0.008854
rs10010325 0.458333333333333 0.467213114754098 0.453488372093023    -0.006217
rs10010809             0.375 0.401639344262295 0.290697674418605     0.005879
rs10015151 0.572916666666667 0.442622950819672 0.546511627906977    -0.005789
rs10016978            0.5625 0.565573770491803 0.424418604651163    -0.005444

我想创建一个新的数据框,它基于第 2、3 和 4 列的值乘以第 5 列,格式如下:

ACB   ASW   BEB
value value value
value value value
value value value
value value value

我试过了 new_df=(as.numeric(as.character(matched_SNPs[,2:4]))*as.numeric(as.character(matched_SNPs$EFF)))

but all I get is: Warning messages:
1: NAs introduced by coercion 
2: In as.numeric(as.character(matched_SNPs[, 2:4])) * as.numeric(as.character(matched_SNPs$EFF)) :
  longer object length is not a multiple of shorter object length

我也尝试了更基本的 weighted_freqs=(matched_SNPs[,2:27])*(matched_SNPs$EFF),但我收到一条警告消息,指出 In Ops.factor(left, right) : '*' not meaningful for factors

我该如何解决这个问题?

我们可以简单地做乘法

matched_SNPs[2:4] * matched_SNPs[,5]
#      ACB          ASW          BEB
#1 -0.003083562 -0.003156689 -0.001102814
#2  0.005349292  0.004209279  0.001441349
#3 -0.002849458 -0.002904664 -0.002819337
#4  0.002204625  0.002361238  0.001709012
#5 -0.003316615 -0.002562344 -0.003163756
#6 -0.003062250 -0.003078984 -0.002310535

假设列是 numeric

如果它不是 numeric 而是 factor 那么首先将感兴趣的列转换为 numeric 然后进行乘法

matched_SNPs[2:5] <- lapply(matched_SNPs[2:5], function(x) as.numeric(as.character(x)))

使用tidyverse方法

加载库:

library(tidyverse)

执行乘法和select只需要三个变量:

mydf_molt <- mydf %>% 
    mutate_at(.vars=c("ACB","ASW","BEB"),.funs=funs(.*EFF)) %>% 
    select(ACB,ASW,BEB)

这是输出:

           ACB          ASW          BEB
1 -0.003083562 -0.003156689 -0.001102814
2  0.005349292  0.004209279  0.001441349
3 -0.002849458 -0.002904664 -0.002819337
4  0.002204625  0.002361238  0.001709012
5 -0.003316615 -0.002562344 -0.003163756
6 -0.003062250 -0.003078984 -0.002310535