GIS in R cran

library(maptools)
library(Cairo)
walesCoast<-readShapeSpatial("Z:/MAPPING DATA/Meridian 2 Shape/data/coast_ln_polyline.shp", proj4string=CRS("+init=epsg:27700"))
walesUA<-readShapeSpatial("Z:/MAPPING DATA/Meridian 2 Shape/data/district_region.shp", proj4string=CRS("+init=epsg:27700"))
x1x2<-c(221000,346594)
y1y2<-c(269406,395000)

plot(walesUA,xaxs="i",yaxs="i",xlim=x1x2,ylim=y1y2,lwd=1)
plot(walesCoast,xaxs="i",yaxs="i",xlim=x1x2,ylim=y1y2,lwd=3,col="red", add=TRUE)

mtext("upvar",side=2,line=2,col=1)
mtext("Bottom",side=1,line=2,col=2)
mtext("Top",side=3,line=2,col=3)
mtext("Right",side=4,line=1,col=4)

Remove lots of values from a large Excel without the undo memory overhead

in this case get rid of the null, this was a sparse query output!

Sub RemoveNulls_memoryCheap()
'
' RemoveNulls_memoryCheap Macro
'
For cci = 1 To 255
If Not Cells(1, cci) = "" Then
    columnCOunt = cci
End If
Next cci
    
For t = 1 To columnCOunt
Range(Columns(t), Columns(t)).EntireColumn.Select
    Selection.Replace What:="null", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Next t

R cran Stacked Histograms

Stacking histograms using the lattice library:

library(lattice)
histogram(~X1|V1, data = df0, layout=c(1,2))

Which produces something like this (you’ll need to adjust the layout parameter if there are more factor levels):

Some more options:

histogram(~X1|V1
		, data = df0
		, layout=c(1,2) ##stack them
		, type="count" ##also available are percentage and density 
		, nint=50 ##number of bins
)

Which produces this:

 

R-Cran Very Basics

Read the clipboard into R:

DataFrame<-read.table("clipboard",headers=TRUE)

Write out the data in a Dataframe into the clipboard:

write.table(DataFrame,"clipboard-2048",sep="\t")

You can use this to paste data into other applications.

Create a DataFrame filled with random data (there may be a shorter way of doing this)

x1<-rnorm(2000,750,55)
x2 <-rnorm(2000,16,100)
df1 <-data.frame(cbind("D",x1,x2))

Here we've used "cbind" which means bind some columns
the data.frame bit seems to convert something into a collection of columns into a DataFrame.

x1<-rnorm(2000,500,155)
x2<-rnorm(2000,250,100)
df2<-data.frame(cbind("A",x1,x2))

Now join them together...

df0<-data.frame(rbind(df1,df2))

Now for some reason we have to convert from factors to numerics...

df0$X1<-as.numeric(as.character(df0$x1))
df0$X2<-as.numeric(as.character(df0$x2))

Notice that we've added two extra columns x1 has been converted into X1 and x2 to X2

str(df0)

Finally let's plot them:

scatterplot(X1 ~X2 | V1 ,data=df0,smooth=FALSE,ellipse=FALSE,lty=0)

Which will look something like this:

We can also generate a boxplot using:

bwplot(X2~V1,data = df0)
boxplot(X2~V1,data = df0)

bwplot requires the lattice library.

Back to the scatter plot graph:

scatterplot(X1 ~X2 | V1 
		,data=df0
		,smooth=FALSE
		,ellipse=FALSE
		,lty=0
		,grid=TRUE
		,boxplots="xy")

Which gives something like this:

Here the boxplots are describing all of X1 and X2  i.e. X1 of  A  and D combined.

 

Correlation Matrix in R CRAN

Here it should be assumed that you have the values as columns on a spreadsheet and everywhere you don’t have a value you have replaced the gap with the letters NA.

Month A B C D E
Apr 63.91419 21.13823 75.13719 80.97858 11.86916
May 92.51104 126.4206 129.9751 56.79248 36.77565
Jun 65.44783 39.14822 6.558068 19.42456 21.63901
Jul 131.4266 70.55551 63.00389 102.5545 116
Aug 129.3926 31.60157 2.435477 NA 73.03078
Sep 35.71819 6.667624 89.68176 98.86681 26.59825
Oct 53.36333 97.32301 63.80283 2.39816 41.05988
Nov 38.88958 88.80514 118.4024 3.027732 64.32666
Dec NA 123.3156 123.5542 79.57653 62.26653
Jan 6.570292 11.66491 72.28813 31.43667 39.0891
Feb 15.1047 9.964003 144.4861 48.67408 146.2865
Mar 78.85201 10.53091 147.5341 67.90013 40.08176

Copy The Data into the clipboard:

cr<-read.table("clipboard",header=TRUE, na.strings="NA" )
x <-cr[2:5]
 cm<-cor(x,x,use = "pairwise.complete.obs")

Now output the Matrix back to the clipboard

write.table(cm,"clipboard",sep="\t")

and paste it back into Excel &c.

If you get a buffer full error then use :

write.table(cm,"clipboard-2048",sep="\t")

instead

A B C D
A 1 0.3 -0 0.4
B 0.3 1 0.2 -0
C -0 0.2 1 0.2
D 0.4 -0 0.2 1

Handy Packages for R

Packages:
lattice
envelope
doBy
RODBC
fArma
BayesX
car
maptools
RColorBrewer

install.packages("lattice", dependencies = TRUE)
install.packages("envelope", dependencies = TRUE)
install.packages("doBy", dependencies = TRUE)
install.packages("RODBC", dependencies = TRUE)
install.packages("fArma", dependencies = TRUE)
install.packages("BayesX", dependencies = TRUE)
install.packages("car", dependencies = TRUE)
install.packages("maptools", dependencies = TRUE)
install.packages("KernSmooth", dependencies = TRUE)
install.packages("RColorBrewer", dependencies = TRUE)

or in one command…

install.packages(c("rJava","lattice","Cairo","xlsx","treemap","RODBC","maptools","doBy","car","RColorBrewer","envelope","BayesX", "KernSmooth"), dependencies = T, repos = "http://cran.fhcrc.org") 

update the main table and several sub tables using the autonumber primary key from the first table…

You’ve got a table (here bed_beds with pk = bed_refno) and you want to insert a new ‘bed’ and update the bed_beds_specialties and bed_sex_status tables using the newly created auto-generated id key.

Create a Stored Procedure (SP) and use the the SCOPE_IDENTITY() function to keep a record of the identity key generated within the scope. Then pass that to the INSERT queries used further down in the SP.


CREATE PROCEDURE spInsert_Bed
@wardRefno numeric(10,0),
@typeRefno numeric(10,0),
@dStart DATETIME,
@dEnd DATETIME,
@dCreate DATETIME,
@vUser VARCHAR(15)
AS
SET NOCOUNT ON
declare @lastin numeric(10,0)
INSERT INTO bed_beds (ward_refno, type_refno, start_Dttm, end_Dttm,create_Dttm,user_create)
VALUES (@wardRefno, @typeRefno, @dStart, @dEnd,@dCreate,@vUser)
set @lastin=  SCOPE_IDENTITY()

INSERT INTO bed_beds_specialties (bed_refno, spect_refno,comments, start_Dttm, end_Dttm,create_Dttm,user_create)
VALUES (@lastin, 3,'my sp test', @dStart, @dEnd,getdate(),@vUser)

INSERT INTO bed_sex_status (bed_refno, start_Dttm, end_Dttm,sex_rfval_refno,create_Dttm,user_create)
VALUES (@lastin,  @dStart,@dEnd,6,getdate(),@vUser)

GO