I have a data.frame titled "matched_SNPs" here:
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
I wanted to make a new dataframe that is based off the values of columns 2, 3, and 4, multiplied by column 5, that is formatted like this:
ACB ASW BEB
value value value
value value value
value value value
value value value
I have tried
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
I have also tried the more basic weighted_freqs=(matched_SNPs[,2:27])*(matched_SNPs$EFF)
, but I get a warning message that says that In Ops.factor(left, right) : '*' not meaningful for factors
.
How can I fix this?
We can simply do the multiplication
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
assuming that the columns are numeric
If it is not numeric
and is factor
then convert the columns of interest to numeric
first and then do the multiplication
matched_SNPs[2:5] <- lapply(matched_SNPs[2:5], function(x) as.numeric(as.character(x)))