Tuesday, September 18, 2012

R tips must know by Geospatial Analyst

1) Merge ESRI shape file with external CSV or data frame to plot the map with CSV/data frame variables

library(RColorBrewer)    # creates nice color schemes
library(classInt)               # finds class intervals for continuous variables

#Read files
csvvalues=read.csv(“c:/csv_path ")
shapefile= readShapePoly("c:/shape.shp")

#Merge data by unique ID
shapefile@data <-data.frame(shapefile@data, csvvalues, by="ID")

# Define the number of classes to be mapped
nclass <- 5

# Set the color ramp that will be used to plot these classes
cols <- brewer.pal(nclass,"YlGnBu")

# Set the class breakpoints using equal intervals
# Can also use quantiles or natural breaks - see help(classIntervals)
breaks <- classIntervals(Column_name_to_be_mapped, nclass, style="quantile")

# Based on the breakpoints and color ramp, specify a color to plot for each polygon
plotcols <- findColours(breaks, cols)

# Generate the map
plot (shapefile, col=plotcols)

2) Combine multiple data frames with similar names into a single data frame using matched column names.
Ex. Use gtools library and smart bind function.
library (“gtools”)
df_result <- smartbind(df1,df2)

3) List files recursively inside directory tree.
Ex. List all *.txt files inside directory tree
fileList=list.files(path= “file Location”,pattern="txt$",all.files=TRUE,full.names=TRUE,recursive=TRUE)
Try to play with grep() if you need more specific filter based on file names not by files types only.

4) List all files in home directory

5) Replace NA by 0
Ex. X[is.na(X[])]<-0

6) Exclude/Remove  columns from data frame
Ex: Select only matched columns
X[, colnames(X) != c('col1','col2')]
X[, colnames(X) != ‘col3’


1 comment :

  1. *****************************************

    #Merge data by unique ID
    shapefile@data <-data.frame(shapefile@data, csvvalues, by="ID")


    That does not work for this purpose at all. That is generating a new variable with name "by" and value all equal to "ID".



© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me