none
Migration of Python to R RRS feed

  • Question

  • Hello Team,

    I am a beginner in R language.

    Currently we are in process of moving away from Python to R.

    I wanted help to covert the below codes to R script :

    1)

    #calculate the heat of dissociation and standard free energy of iodine
    %matplotlib inline
    import numpy
    import math
    import matplotlib
    from matplotlib import pyplot
    import warnings
    #initialization of variables
    R=1.987
    T=1000 #K
    T=numpy.array([973.,1073.,1173.,1274.])
    kp=numpy.array([.175e-2,1.108e-2,4.87e-2,17.05e-2])
    #calculations
    Tx=1000./T
    logkp=numpy.log10(kp)
    slope, intercept = numpy.polyfit(Tx,logkp,1)
    dH=-slope*2.303*R*1000.
    dH0=dH-1.5*R*T
    dG1=-R*T*logkp[1]*2.303
    dGt=28720 #cal
    dGI=(dGt/1000. + 4.63)/2
    #results
    print '%s %d %s' %('Heat of dissociation = ',dH,'cal')
    print '%s %.2f %s' %('standard free energy of iodine atom = ',dGI,'kcal/mol')
    pyplot.plot(Tx,logkp)
    pyplot.xlabel('1000/T')
    pyplot.ylabel('log Kp')
    pyplot.title('Logarithm of Kp for dissociation of Iodine as a function of reciprocal temperature')
    pyplot.show()
    warnings.filterwarnings("ignore")

    2) 

    import math
    %matplotlib inline
    import matplotlib
    import numpy as np
    import matplotlib.pyplot as plt
    
    #Initialisation
    L=13200                                       #L parameter in m
    H=10240                                       #H parameter 
    Re=6370000                                    #actual redius of earth
    ht=30                                         #height in m
    hr=20                                         #height in m
    re1=8453000                                    # in metre
    h1=30000                                     # in metre                        
    h2=20000                                    # in metre
    dt1=22.5
    f=10*10**9                                  #frequency in Hz
    c=3*10**8                                   #speed of light
    d=30000                                     #distance in m
    pt=30                                       #transmitter antenna power
    gt=40                                       #transmitter antenna gain
    gr=40                                       #receiver antenna gain                                    
    F3=-3
    H=-34
    D=0.75
    
    #Calculation
    dt=math.sqrt(2*re1*ht)
    X=3*dt*L**-1
    Z1=h1*H**-1
    Z2=h2*H**-1
    vx=10**-3.5                              #from fig 3.26
    z1=10**0.95                              #from fig 3.27
    z2=10**0.65                              #from fig 3.27
    
    #for d=3dt
    lv=20*math.log10(vx)
    lz1=20*math.log10(z1)
    lz2=20*math.log10(z2)
    F=(lv+lz1+lz2)*20**-1
    F1=10**(F)
    F11=20*math.log10(F1)
    X1=2*dt*L**-1
    d3=3
    f3=-F11
    
    vx1=10**-2.35                            
    lv1=20*math.log10(vx1)
    
    #for d=2dt
    F4=1+D
    F5=20*math.log10(F4)
    d2=2
    f2=-F5
    
    
    #for d=1.1dt
    F6=math.sqrt(1+D**2)
    F7=20*math.log10(F6)
    d11=1.1
    f11=-F7
    
    #for d=dt
    d1=1
    f1=0.2
    
    #for plotting graph in terms of points
    
    for N in range(0,4,1):
        a=plot(1,0.2,'-o')
        a1=plot(1.1,-1.9,'-o')
        a2=plot(2,-4.8,'-o')
        a3=plot(3,-38,'-o')
        
    plt.annotate('d/dt',xy=(1,2),xytext=(1, 2))
    plt.annotate('1.1d/dt',xy=(1.2,0.7),xytext=(1.2, 0.7))
    plt.annotate('2d/dt',xy=(2,-0.7),xytext=(2, -0.7))
    plt.annotate('3d/dt',xy=(3,-35),xytext=(3, -35))
    xlabel("$d/dt$")
    ylabel("$20 log(F)$ (dB)")
    title("Path gain F")
    grid()
    show(a)
    show(a1)
    show(a2)
    show(a3)
       
    h=c*f**-1                                                                       #wavelength
    Pr=pt+gt+gr+H+F3-10*math.log10(4*math.pi*d**2)                                  #Received signal power

    So can someone please help me with the R script to achieve the same result.

    This is the initial learning phase.

    Thanks

    Wednesday, February 19, 2020 4:45 AM

All replies

  • You have all the necessary skills for migrating from Python to R here:

    https://rc2e.com/

    I recommend using the Anaconda's R Distribution for Package Management. 

    Monday, February 24, 2020 7:48 AM