0

This is my first time asking question in Stack Overflow, I hope I am providing enough information.

Spyder 3.2.3 Python 3.6.2 64bits, Qt 5.6.2, PyQt5 5.6 on Windows 10

I have written code in R to identify and map clusters using Long/Lat

I am now tasked with translating it into Python language for which I am not even novice yet (can harldy spell python, lol.

Here is sample data: Actual number of records = 2350 and other non-essential columns for my immediate question needs

x <- tmp1[,c(4)]
head(x,n=15)

-79.86286 -81.47581 -73.99576 -91.58647 -82.05398 -76.72366 -87.31574 -122.73485 -89.61309 -87.70185 -70.97230 -73.79628 -73.74651 -96.82385 -112.01015

y <- tmp1[,c(5)]
head(y,n=15)

40.43981 31.21329 40.80918 42.07720 26.77963 39.95549 41.51222 42.37320 39.84502 42.07626 42.37230 41.21192 40.70294 32.41051 41.52537

Here is the first step of the process and where I encounter my first error: In Spyder I get this "colon" error

xy = r.SpatialPointsDataFrame(matrix(c(x,y), ncol=2), 
     r.data.frame(ID = r.seq(1:length(x))),
     proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

Error here: SyntaxError: invalid syntax data.frame(ID=seq(1:length(x))),

One of many links I have perused for help
Getting a Colon Syntax Error in Python

IN R it looks like this:

xy <- SpatialPointsDataFrame(matrix(c(x,y), ncol=2), data.frame(ID=seq(1:length(x))),
                         proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

I Tried with & w/o the :, with a , and no 1?

I have reviewed many links trying to get past this first step but have found no solution

I have tried to parse it out piece by piece and naturally get other errors as well.

r.data.frame(ID = r.seq(1:length(x)))

SyntaxError: invalid syntax

xy = r.SpatialPointsDataFrame(matrix(c(x,y), ncol=2), 
r.data.frame(ID = r.seq(1:length(x))),
proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

AttributeError: 'R' object has no attribute 'SpatialPointsDataFrame'''

https://rdrr.io/r/base/seq.html

xy = r.SpatialPointsDataFrame(matrix(c(x,y), ncol=2))

AttributeError: 'R' object has no attribute 'SpatialPointsDataFrame

proj4string=r.CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
proj4string=robjects.CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
proj4string=r.CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")

AttributeError: module 'rpy2.robjects' has no attribute 'CRS

proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")

NameError: name 'CRS' is not defined

I believe I have followed all the preparation steps to work with R in Python using rpy2: Import rpy2 then Import R https://rpy2.github.io/doc/latest/html/robjects_rpackages.html https://rpy2.github.io/doc/v3.1.x/html/_modules/rpy2/robjects/packages.html

Using R inside python

import rpy2
print(rpy2.__version__)

3.3.3

https://rpy2.github.io/doc/latest/html/introduction.html Get necessary tools

import rpy2.robjects as robjects

from rpy2.robjects import DataFrame, Formula

import rpy2.robjects.packages as rpackages

from rpy2.robjects.packages import importr  

from rpy2.robjects import r   

utils = rpackages.importr('utils')

utils.chooseCRANmirror(ind=1)  
base = importr('base')

rpy2 - 'R' object has no attribute 'nls' Alternative Only

base = importr('base', robject_translations={'with': '_with'})

base

rpy2.robjects.packages.Package as a

stats = importr('stats')

Alternative Only

stats = importr('stats', robject_translations={'format_perc': '_format_perc'})

stats

rpy2.robjects.packages.Package as a

packnames = ('ggplot2', 
'usmap','geosphere','sp','rdgal','ggmap','maps','mapdata','ggcorrplot','reshape2','dplyr'
         ,'magrittr''devtools','proj4')

from rpy2.robjects.vectors import StrVector

utils.install_packages(StrVector(packnames))

Alternative Only: I have not used this yet

names_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
if len(names_to_install) > 0:
utils.install_packages(StrVector(names_to_install))

https://rpy2.github.io/doc/latest/html/pandas.html

from rpy2.robjects.conversion import localconverter

from rpy2.robjects import pandas2ri

https://pandas.pydata.org/pandas-docs/version/0.22.0/r_interface.html

pandas2ri.activate()
import rpy2.robjects as ro

I hope someone can provide me with some direction.

Thank you.

WHP

3
  • what is the r object in your python code?
    – at80
    Jun 3, 2020 at 22:07
  • There are two options, the first one is reading about rpy2 and use R functions in python, or you can read about pandas and geopandas and rewrite your functions. I guess rpy2 is the fastest way. But whichever route you choose, take your time and try to follow a tutorial first and understand what you're doing.
    – SergioR
    Jun 4, 2020 at 11:26
  • Thank you Sergio, after several attempts I I have abandoned the rpy2 option and I am teaching myself python using this task. Reviewing pandas and geopandas documents. I expect to have new questions shortly. Please consider this question closed
    – WHP66
    Jun 9, 2020 at 9:34

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.