[Tutor] Get a single random sample

Alan Gauld alan.gauld at btinternet.com
Fri Sep 9 20:04:04 CEST 2011


On 09/09/11 11:44, kitty wrote:

> ...I have been an R user for 7 years and and am
> finding it difficult to do even basic things in python,

If you are trying to do the kinds of things that R is good at in Python 
you will find it involves a lot more work. That's because Python is a 
general purpose programming language and R is a special purpose one, 
optimised to its task. You can do most things in Python but it can never 
compete with a specialised language in that language's area.
On the other hand if you try writing networking applications or
GUIs in R you will likely find that's harder than using Python.

Choosing the right tool for the job is always a choice between a 
specialist tool or a general purpose one. If you do a specialised job a 
specialised tool is probably a better choice.

> In R I would just
>
> data<-read.table("FILE PATH\\Road.density.municipio.all.txt", header=T)
> names(data)
> attach(data)
> subset<-change.dens[area<2000&area>700]
> random<-sample(subset,1)
>
>
> My question is how do I get python to do this???
>
> Sorry I know it is very basic but I just cant seem to get going,


And there's the rub, it may be very basic in R but its quite a challenge 
in Python, you need to get into a lot more detail.

But the good news is that there is an interface between Python
and R - called RPy - that you can use to get the best of both worlds. 
Use Python for the general stuff and use RPy for the analytical work...

You can get RPy here:

http://rpy.sourceforge.net/rpy2.html

And there is a simple introduction and comparison of R and
Python benefits here:

http://www.bytemining.com/2010/10/accessing-r-from-python-using-rpy2/

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list