Calculating a percentage from a "list", R -
i have 15 deer analysing there used , expected info points extracted slope tif. file. analysing expected info , have problem calculating percentage
packages used raster package, rgdal, , psych packages. info layout: info displayed in "list" format , displayed each individual (i.e.15 rows) each structure: deer01, 2 1 5 4 2 3 2 2 (where numbers represent category of slope incline). however, want seek calculate total number of 5's or 2's (representing slope) across deer , convert percentage.
problem: not know how calculate proportion of category 5's (or 2's etc) "list" info construction described above , need help. guess must sum rows / columns or unsure how. know how? apparent syntax below there 1 column heading refers id (deer) column (see construction typed in paragraph above).
here total syntax used within 50% home ranges:
install.packages("raster") library(raster) install.packages("rgdal") library(rgdal) slope<-raster("c:\\rework\\slope011.tif") plot(slope) slope str(slope) install.packages("psych") library(psych) calculate , categorise 50% expected slope values
m <- matrix (c(0, 5, 1, 5, 10, 2, 10, 15, 3, 15, 20, 4, 20, inf, 5), ncol=3, byrow=true) r2 <- reclassify(slope, m) r2 plot(r2) all50hrs<-shapefile("l:\\gpsdatabackup\\kernels\\newhrs50") plot(all50hrs) all50hrs library(raster) update.packages("raster") attempt extract slope spatial polygons" using vector format , maintaining object_id here info required in "list" format
exp50poly <- extract(r2, all50hrs) names(exp50poly)<-all50hrs$object_id str(exp50poly) this section effort @ trying proportional value info shown in str(exp50poly) doesn't seem work
exp50polycov.table50.sum<-colsums(exp50polycov.table50) exp50polycov.table50.sum<-exp50polycov.table50.sum/sum(exp50polycov.table50.sum) exp50polycov.table50.sum to seek , help people see info construction have copied info console see data. see below (the left no' id slope codes right). repeated 15 times each individual
dr_12_008_11: num [1:2089] 1 1 2 3 2 2 1 1 1 2... dr_14_011_12: num [1:5260] 2 2 2 2 2 2 2 2 2 2... dr_10_012_14: num [1:710] 2 2 2 2 3 3 2 2 3 3 2.. hope can help, thanks
this hard reply without seeing how you've shaped data.
i assume info looks this:
circle habitat circle25 1 circle14 2 if case can add together new column , populate each row 1:
dataframe$proportion <- 1 following can calculate percentage of each type of habitat circle type using aggregate:
percentage <- aggregate(dataframe$proportion, by=list(dataframe$circle,dataframe$habitat),fun="mean") this finish guesswork based on vague question may not correctly answering.
if want percentage of habitats overall utilize same aggregate drop circle column:
percentage <- aggregate(dataframe$proportion, by=list(dataframe$habitat),fun="mean") r
No comments:
Post a Comment