遇到错误,将data.frame强制转换为数值

时间:2017-12-30 18:09:50

标签: r dataframe vector multiplication

我有一个名为" 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

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

使用tidyverse方法

加载库:

library(tidyverse)

执行乘法并仅选择所需的三个变量:

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

答案 1 :(得分:1)

我们可以简单地进行乘法

from pylab import *
from scipy.optimize import brentq
import numpy as np

l = float(input("Angular momentum l:"))
L = float(input("Width of the potential:"))
Vo = float(input("Value of the potential:"))
N = int(input("Number of steps (~10000):"))
h = float(3*L/N)
psi = np.zeros(N) #wave function
psi[0] = 0
psi[1] = h

def V(x,E):
    """
    Effective potential function.
    """
    if x > L:
        return -2*E+l*(l+1)/x**2
    else:
        return -2*(Vo+E)+l*(l+1)/x**2

def Wavefunction(energy):
    """
    Calculates wave function psi for the given value
    of energy E and returns value at point xmax
    """
    global psi
    global E
    E=energy
    for i in range(2,N):
        psi[i]=(2*(1+5*(h**2)*V(i*h,E)/12)*psi[i-1]-(1-(h**2)*V((i-1)*h,E)/12)*psi[i-2])/(1-(h**2)*V((i+1)*h,E)/12)       
    return psi[-1]

def find_energy_levels(x,y):
    """
    Gives all zeroes in y = psi_max, x=en
    """
    zeroes = []
    s = np.sign(y)
    for i in range(len(y)-1):
        if s[i]+s[i+1] == 0: #sign change
            zero = brentq(Wavefunction, x[i], x[i+1])
            zeroes.append(zero)
    return zeroes

def main():

    energies = np.linspace(-Vo,0,int(10*Vo))   # vector of energies where we look for the stable states
    psi_max = []  # vector of wave function at x = 3L for all of the energies in energies
    for energy in energies:
        psi_max.append(Wavefunction(energy))     # for each energy find the the psi_max at xmax
    E_levels = find_energy_levels(energies,psi_max)   # now find the energies where psi_max = 0  
    print ("Energies for the bound states are: ")
    for E in E_levels:
        print ("%.2f" %E)
    # Plot the wavefunctions for first 4 eigenstates
    x = np.linspace(0, 3*L, N)
    figure()
    for E in E_levels:
        Wavefunction(E)
        plot(x, psi, label="E = %.2f"%E)
    legend(loc="upper right")
    xlabel('r')
    ylabel('$u(r)$', fontsize = 10)
    grid()
    savefig('numerov.pdf', bbox_inches='tight')

if __name__ == "__main__":
    main()

假设列为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

如果不是numericnumeric,则首先将感兴趣的列转换为factor,然后进行乘法

numeric