This is the line of code I need to change:
which(!is.na(match(tbl$col1,arr)))
I am a newbie to python, would really appreciate the help
There is a simpler way to achieve the same objective in R:
which(tbl$col1 %in% arr)
which can be translated to Python (I'm considering that tbl is a Pandas dataframe here):
import numpy as np
np.where(tbl[col1].isin(arr))[0]
You should remember that Python is 0-indexed, though.
Related
Is there a way to incorporate Python functions from an external module/library in a function you're writing/defining, without importing them globally or beyond of the function scope?
For example, in R you can do package::function:
myfxn <- function(x){ # R example
dplyr::as_tibble(x)
}
I'd like to do something similar in Python, but as far as I can tell, people usually just import the modules/elements of modules at the top of the file (for speed, succinctness, comprehensiveness, etc.) I know you can technically do something like this:
Example of what I know you can do in Python (but isn't what I'm looking for):
def myfxn(x): # Python
from pandas import DataFrame
return(DataFrame(x))
...But what I'd really like is a way to use a function like the pandas DataFrame one without having to globally import it. I wish there was something like...
def myfxn(x): # Python
return(pandas.DataFrame(x))
Where I wouldn't have to actually import anything. Is there any way of doing what I'm referencing? If not, is there an argument (except for a tiny amount of processing speed) about why there shouldn't be?
My colleague gave me a big R function and I have to use it in the pipeline which will be developed in python3. In theory I have two options:
convert R into python
directly use R function in python script (for example by importing like a module)
What do you suggest? and how can I do each of these options?
Using rpy2.robjects might help ('function.R' is the r code)
import rpy2.robjects as robjects
robjects.r.source("function.R")
result refers to the output of the function
print(robjects.globalenv["result"])
I am trying to print a scipy result with pretty print but it seems output not in latex format.
import sympy
from sympy import *
init_printing()
var('x,y')
y = sympy.expand((x+y)**2)
y
Output:
Make sure you are using the latest version of SymPy (0.7.6.1). There was an issue with printing which was fixed in the 0.7.6.1 bugfix release.
You need set pretty printing up:
from sympy import init_printing
init_printing()
Now it should work.
Also install matplotlib.
I want to use output from python program and barplot it using R.
I tried using rPy2
import rpy2.robjects as robjects
A= 2
B=4
plot = robjects.IntVector([A,D])
now to call barplot(plot) function. Please guide, even help using any other python package is appreciated
I am trying to load a matlab file with the R.matlab package. The problem is that it keeps loading indefinitely (e.g. table <- readMat("~/desktop/hg18_with_miR_20080407.mat"). I have a genome file from the Broad Institute (hg18_with_miR_20080407.mat).
You can find it at:
http://genepattern.broadinstitute.org/ftp/distribution/genepattern/dev_archive/GISTIC/broad.mit.edu:cancer.software.genepattern.module.analysis/00125/1.1/
I was wondering: has anyone tried the package and have similar issues?
(hopefully helpful, but not really an answer, though there was too much formatting for a comment)
I think you may need to get a friend with matlab access to save the file into a more reasonable format or use python for data processing. It "hangs" for me as well (OS X 10.9, R 3.1.1). The following works in python:
import scipy.io
mat = scipy.io.loadmat("hg18_with_miR_20080407.mat")
(you can see and work with the rg and cyto' crufty numpy arrays, but they can't be converted to JSON withjson.dumpsand evenjsonpickle.encodecoughs up a lung-full of errors (i.e. you won't be able to userPython` to get access to the object which was the original workaround I was looking for), so no serialization to a file either (and, I have to believe the resultant JSON would have been ugly to deal with).
Your options are to:
get a friend to convert it (as suggested previous)
make CSV files out of the numpy arrays in python
use matlab