The long way:
Read the instructions:
sudo purge all the time was a pain and didn’t really help too much. In the end I bought 8 Gig of RAM and put that in to my machine. It has helped. I now have a functioning machine again.
Many people I respect and like use R in their work and like the way it works. It is backed up by "R Studio" as well as help files and a large, user community. After an enormous amount of frustration caused by Pandas for Python I thought I might try R instead.
Let's start with the good points. R Studio is a great way to use R. It is for R what "Spyder" would like (but fails) to be for Python. It is considerably more stable and smooth than Spyder and looks good too. R Studio provides some pretty useful tricks for coding, such as running only the marked text in a script file or running step by step, line by line.
R itself comes with many packages and tools, especially for statistics. This is the main focus of R: statistics for ecologists and biologists. This explains and is explained by the strong connexion with these research communities. Plotting (at least with the standard "plot" function and not the hideous "ggplot") is simple and elegant, much easier than "matplotlib" in Python.
The bad points. If you are new to scripting then R is probably fine, you will learn its "psychology" and work with it. If you have learnt any other language first then R will feel unreasonably and pointlessly quirky and counter-intuitive. Indexing is confusing, lists have names for their elements, which might set them apart from vectors, except that so do vectors. Many hours have been spent by many people developing the packages for R, which provides all that functionality, but why R? It doesn't provide any data structure that is superior to other scripting languages. It's not as if "factors" provide a quick and easy mechanism for sub-setting and analysis, certainly no easier than in other languages. The help files are many and mostly useless for the newbie. They read and look like poorly written, first draft man pages.
I suppose my biggest complaint about R is "what's the point?" All that time and effort could have been put into another language and the statistics would work just as well. As I have stated, R provides no intrinsic advantage for statistical calculations, it is simply that engaged enthusiasts have written the libraries in R. Learning another language is fine but in this case, the language sits at some uncomfortable angle to most others and provides no clear advantage, making learning frustrating but without any real incentive. I suppose I shall just have to learn to accept Pandas and its idiotic time stamp behaviour and Python's general love of bloating with evermore object types for no good reason whatsoever.
Or just stop bitching about this sort of thing.import sys
import os
import numpy as np
import numpy.ma as ma
import scipy.stats as stats
from scipy.stats.stats import nanmean
import textwrap
.mean method the mean of the masked values can be calculated.a = [1,2,3,4,5,6]
b = np.array(a)
c = [0,0,0,1,0,0]
d = np.array(c)
a1 = ma.masked_array(a, mask=c)
b1 = ma.masked_array(b, mask=d)
a1mean = a1.mean()
b1mean = b1.mean()
a2 = ma.masked_values(a,4)
#
# PRINT TO TERMINAL
print textwrap.dedent("""\
a = [1,2,3,4,5,6]
b = np.array(a)
c = [0,0,0,1,0,0]
d = np.array(c)
a1 = ma.masked_array(a, mask=c)
b1 = ma.masked_array(b, mask=d)
a1mean = a1.mean()
b1mean = b1.mean()
a2 = ma.masked_values(a,4)
""")
print "a: ", a
print "b: ", b
print "a1: ", a1
print "mean of a1: ", a1mean
print "a2: ", a2
print "b1: ", b1
print "mean of b1: ", b1mean
print "\n"*3
.mean() which returns the mean of all unmasked values or we can specify an axis thus: .mean(0). This would return the mean of each column. e = np.array([[1,2,3],[4,5,6]])
f = np.array([[0,0,0],[1,0,0]])
e1 = ma.masked_array(e, mask=f)
e2 = ma.masked_values(e,4)
e1mean = e1.mean()
e1mean0 = e1.mean(0)
e1mean1 = e1.mean(1)
#
# PRINT TO TERMINAL
print textwrap.dedent("""\
e = np.array([[1,2,3],[4,5,6]])
f = np.array([[0,0,0],[1,0,0]])
e1 = ma.masked_array(e, mask=f)
e2 = ma.masked_values(e,4)
e1mean = e1.mean()
e1mean0 = e1.mean(0)
e1mean1 = e1.mean(1)
""")
print "e: ", e
print "e1: ", e1
print "e2: ", e2
print "mean of e1: ", e1mean
print "mean of each column: ", e1mean0
print "mean of each row: ", e1mean1
print "\n"*3
np.nan (note that the “np” is short for numpy and was defined at the import). Unfortunately the standard mean function can’t handle NaN’s so you have to use nanmean which is imported from scipy.stats.stats (I know, dumb as hell, but there you go). If we try to use .mean on an array containing np.nan it returns “nan”. nanmean gives us the mean of each column though and we can’t just take the mean of the means.g = np.array([[1,2,3],[np.nan,5,6]])
gmean = g.mean()
g_nanmean = nanmean(g)
g_nanmean_all = nanmean(g_nanmean)
#
# PRINT TO TERMINAL
print textwrap.dedent("""\
g = np.array([[1,2,3],[np.nan,5,6]])
gmean = g.mean()
g_nanmean = nanmean(g)
g_nanmean_all = nanmean(g_nanmean)
""")
print "g: ", g
print "gmean: ", gmean
print "nanmean: ", g_nanmean
print "g_nanmean_all: ", g_nanmean_all
print "\n"*3
.meandoes what we want. np.ravel() will flatten it for us. print "The np.ravel() function: "
print "np.ravel(g) to flatten e: "
print g, "\nwhich becomes ", np.ravel(g), "\n"
gravelnmean = nanmean(np.ravel(g))
#
# PRINT TO TERMINAL
print textwrap.dedent("""\
gravelnmean = nanmean(np.ravel(g))
""")
print "gives: ", gravelnmean
np.nan when the masked_array seems more flexible? Well, consistency in implementation in numpy is a bit of a problem. Some packages within numpy contain functions that work in different ways, which can be very frustrating to a newcomer. h = np.array([[1,2,3],[np.nan,5,6]])
h1 = ma.masked_where(np.isnan(h),h)
h2 = ma.masked_where(np.isnan(g1),h)
h3 = ma.masked_where(ma.getmask(g1), h)
#
# PRINT TO TERMINAL
print textwrap.dedent("""\
h = np.array([[1,2,3],[np.nan,5,6]])
h1 = ma.masked_where(np.isnan(h),h)
h2 = ma.masked_where(np.isnan(g1),h)
h3 = ma.masked_where(ma.getmask(g1), h)
""")
print "h: ", h
print "h1: ", h1
print "h2: ", h2
print "h3: ", h3
#! /usr/bin/python
import sys
import os
#
# Create our class.
class Pixel:
# Set a counter for how many objects of this class we have created.
pixelCount = 0
# This is the initialisation bit. We pass the new object 3 values.
# If we only pass 2 values the 3 is assigned a default value.
def __init__(self, east,north,elev = 0):
# Now we give the values passed to the initialing thingy to internal variables.
self.easting = east
self.northing = north
self.elevation = elev
# Then we increase the class counter by 1.
Pixel.pixelCount += 1
#! /usr/bin/python
import sys
import os
# Create our class.
class Pixel:
# Set a counter for how many objects of this class we have created. This is shared by
# all "Pixels"
pixelCount = 0
# This is the initialisation bit. We pass the new object 3 values.
# If we only pass 2 values the 3 is assigned a default value.
def __init__(self, east,north,elev = 0):
# Now we give the values passed to the initialing thingy to internal variables.
self.easting = east
self.northing = north
self.elevation = elev
# Then we increase the class counter by 1.
Pixel.pixelCount += 1
#
# This function doesn't do a great deal other than tell you how many pixels you have.
def counter(self):
print 'Total number of pixels: %s' % Pixel.pixelCount
#
__init__ bit, that is shared by all objects created from this class.#! /usr/bin/python
import sys
import os
# Create our class.
class Pixel:
# Set a counter for how many objects of this class we have created.
pixelCount = 0
# This is the initialisation bit. We pass the new object 3 values.
# If we only pass 2 values the 3 is assigned a default value.
def __init__(self, east,north,elev = 0):
# Now we give the values passed to the initialing thingy to internal variables.
self.easting = east
self.northing = north
self.elevation = elev
# Then we increase the class counter by 1.
Pixel.pixelCount += 1
#
# This function doesn't do a great deal other than tell you how many pixels you have.
def counter(self):
print 'Total number of pixels: %s' % Pixel.pixelCount
#
# This is a function (method if you want to get all technical) that uses the object's
# own variables plus some other thing.
def dist2d(self,other):
# That other thing is another Pixel object, so check that that is what it is.
if not isinstance(other, Pixel):
sys.exit(1)
# A little unnecessary this but lets just pass variable values on.
e1, n1 = self.easting, self.northing
e2, n2 = other.easting, other.northing
# Some calculation (remember Pythagoras?)
edif = e2 - e1
ndif = n2 - n1
dist2 = (edif**2 + ndif**2)**0.5
# Talk to the user.
print 'Eastings: %s - %s = %s' %(e2,e1,edif)
print 'Northings: %s - %s = %s' %(n2,n1,ndif)
print 'Distance in 2D: %s\n' %(dist2)
# Return the calculated value to whatever called the function (method)
return dist2
#
# As before but this time in glorious 3D.
def dist3d(self,other):
if not isinstance(other, Pixel):
sys.exit(1)
e1, n1, z1 = self.easting, self.northing, self.elevation
e2, n2, z2 = other.easting, other.northing, other.elevation
edif = e2 - e1
ndif = n2 - n1
# We don't really need to create too many new variables,
# so lets just use what we have. Get the vertical distance directly from the
# object's self storage.
zdif = other.elevation - self.elevation
dist3 = (edif**2 + ndif**2 + zdif**2)**0.5
print 'Eastings: %s - %s = %s' %(e2,e1,edif)
print 'Northings: %s - %s = %s' %(n2,n1,ndif)
print 'Elevation: %s - %s = %s' %(other.elevation,self.elevation,zdif)
print 'Distance in 3D: %s\n' %(dist3)
return dist3
##! /usr/bin/python
import sys
import os
# Create our class.
class Pixel:
# Set a counter for how many objects of this class we have created.
pixelCount = 0
# This is the initialisation bit. We pass the new object 3 values.
# If we only pass 2 values the 3 is assigned a default value.
def __init__(self, east,north,elev = 0):
# Now we give the values passed to the initialing thingy to internal variables.
self.easting = east
self.northing = north
self.elevation = elev
# Then we increase the class counter by 1.
Pixel.pixelCount += 1
#
# This function doesn't do a great deal other than tell you how many pixels you have.
def counter(self):
print 'Total number of pixels: %s' % Pixel.pixelCount
#
# This is a function (method if you want to get all technical) that uses the object's
# own variables plus some other thing.
def dist2d(self,other):
# That other thing is another Pixel object, so check that that is what it is.
if not isinstance(other, Pixel):
sys.exit(1)
# A little unnecessary this but lets just pass variable values on.
e1, n1 = self.easting, self.northing
e2, n2 = other.easting, other.northing
# Some calculation (remember Pythagoras?)
edif = e2 - e1
ndif = n2 - n1
dist2 = (edif**2 + ndif**2)**0.5
# Talk to the user.
print 'Eastings: %s - %s = %s' %(e2,e1,edif)
print 'Northings: %s - %s = %s' %(n2,n1,ndif)
print 'Distance in 2D: %s\n' %(dist2)
# Return the calculated value to whatever called the function (method)
return dist2
#
# As before but this time in glorious 3D.
def dist3d(self,other):
if not isinstance(other, Pixel):
sys.exit(1)
e1, n1, z1 = self.easting, self.northing, self.elevation
e2, n2, z2 = other.easting, other.northing, other.elevation
edif = e2 - e1
ndif = n2 - n1
# We don't really need to create too many new variables,
# so lets just use what we have. Get the vertical distance directly from the
# object's self storage.
zdif = other.elevation - self.elevation
dist3 = (edif**2 + ndif**2 + zdif**2)**0.5
print 'Eastings: %s - %s = %s' %(e2,e1,edif)
print 'Northings: %s - %s = %s' %(n2,n1,ndif)
print 'Elevation: %s - %s = %s' %(other.elevation,self.elevation,zdif)
print 'Distance in 3D: %s\n' %(dist3)
return dist3
#
# Time to create some objects.
# Create object "a" with Easting = 10, Northing = 10, Elevation = 10.
a = Pixel(10,10,10)
# Create object "b" etc.
b = Pixel(50,45,20)
# Now get to work by assign the variable "ans1" the value returned
# by sending Pixel object "b" to the dist2D function (method) of Pixel object "a"
ans1 = a.dist2d(b)
# Same again but in 3D
ans2 = a.dist3d(b)
print 'ans1 and ans2: %s %s\n' %(ans1,ans2)
print 'Call Pixel.pixelCount Number of pixels is %s' % Pixel.pixelCount
print 'Then call a.counter()'
a.counter()
print 'Then call b.counter()'
b.counter()
#
# What about only sending 2 coordinates?
c = Pixel(20,30)
ans3 = a.dist3d(c)
print '\nans3',ans3
print 'Call Pixel.pixelCount Number of pixels is %s' % Pixel.pixelCount
print 'Then call c.counter()'
c.counter()
#
# We can call an objects variables whenever.
print '\nEasting for a: ', a.easting
# We can even assign it some new ones
a.distTo_b = ans2
print 'Distance a to b: ', a.distTo_b
# It doesn't even have to make sense.
a.stupidName = 'Wibble'
print 'New attribute value: ', a.stupidName