A Few Days of Python: Using R in Python

[This article was first published on Mathew Analytics » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Using R Functions in Python

import pandas as pd
import pyper as pr

def zone_func(zone_file):

    # IMPORT DATA:
    dat = pd.read_csv(zone_file, parse_dates=["Row Labels"])

    # CREATE A R INSTANCE WITH PYPER:    
    r = pr.R()
    
    # PASS DATA FROM PYTHON TO R:
    r.assign("rsfores", dat["forest"]) 
    
    forecast = r("""
            library(forecast)
            forecast_func <- function(vehicle){
                a_fit <- auto.arima(vehicle)
                a_forecast <- forecast(a_fit, h = 8)
                a_accuracy <- accuracy(a_forecast)
            }

            fores_output = forecast_func("rsfores")
    """)

    pydata = pd.DataFrame(r.get("fores_output"))
    print pydata

zone_func("File_Atlanta.csv")

To leave a comment for the author, please follow the link and comment on their blog: Mathew Analytics » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)