r - Subsetting a list of data frame with multiple criteria -
i have large dateset multiple data forest inventory. data frame contains species found in each plot. , data organized site.
> str(mm,max.level=2) list of 50 $ :'data.frame': 2944 obs. of 18 variables: ..$ plot : int [1:2944] 2 3 3 3 3 4 4 4 5 5 ... ..$ cla : factor w/ 2 levels "a","n": 1 1 1 1 1 1 1 1 1 1 ... ..$ subclase : factor w/ 7 levels "1","3c","3e",..: 5 2 3 2 3 2 3 3 1 1 ... ..$ posesp : int [1:2944] 1 1 1 2 2 1 1 2 1 2 ... ..$ especie : int [1:2944] 28 72 72 41 44 28 43 28 28 43 ... ..$ ocupa : int [1:2944] 10 6 7 3 2 9 5 4 5 5 ... ..$ estado : int [1:2944] 3 4 4 4 4 4 2 2 1 2 ... ..$ fpmasa : int [1:2944] 1 4 4 4 4 2 4 2 1 4 ... ..$ edad : int [1:2944] 14 na na na na 35 na 5 2 na ... ..$ finfor : int [1:2944] 8 na na na na 7 na 5 1 na ... ..$ fiabil : int [1:2944] 4 na na na na 4 na 3 4 na ... ..$ barrena1 : logi [1:2944] na na na na na na ... ..$ barrena2 : logi [1:2944] na na na na na na ... ..$ barrena3 : logi [1:2944] na na na na na na ... ..$ altper : logi [1:2944] na na na na na na ... ..$ orgmasa1 : int [1:2944] 2 1 1 1 1 2 1 1 1 1 ... ..$ orgmasa2 : int [1:2944] 2 4 4 4 4 2 4 1 1 4 ... ..$ tratmasa : int [1:2944] 1 2 2 2 2 1 2 1 1 2 ... $ :'data.frame': 3685 obs. of 18 variables: ..$ plot : int [1:3685] 25 25 26 26 26 27 28 29 29 29 ... ..$ cla : factor w/ 3 levels "a","n","r": 1 1 1 1 1 1 1 1 1 1 ... ..$ subclase : factor w/ 8 levels "1","2","3c","3e",..: 1 1 5 5 5 1 6 3 4 3 ... ..$ posesp : int [1:3685] 1 2 1 2 3 1 1 1 1 2 ... ..$ especie : int [1:3685] 24 237 24 237 68 24 24 24 24 237 ... ..$ ocupa : int [1:3685] 8 1 6 2 2 9 10 9 9 1 ... ..$ estado : int [1:3685] 4 2 3 2 2 4 3 4 4 2 ... ..$ fpmasa : int [1:3685] 4 4 4 4 4 3 2 4 4 4 ... ..$ edad : int [1:3685] na na na na na na 25 na na na ... ..$ finfor : int [1:3685] na na na na na na 5 na na na ... ..$ fiabil : int [1:3685] na na na na na na 3 na na na ... ..$ barrena1 : logi [1:3685] na na na na na na ... ..$ barrena2 : logi [1:3685] na na na na na na ... ..$ barrena3 : logi [1:3685] na na na na na na ... ..$ altper : logi [1:3685] na na na na na na ... ..$ orgmasa1 : int [1:3685] 1 1 1 1 1 1 2 1 1 1 ... ..$ orgmasa2 : int [1:3685] 1 1 1 1 1 1 2 1 1 1 ... ..$ tratmasa : int [1:3685] 1 1 1 1 1 1 1 1 1 1 ... > names(mm[[9]]) [1] "plot" "cla" "subclase" "posesp" "especie" [6] "ocupa" "estado" "fpmasa" "edad" "finfor" [11] "fiabil" "barrena1" "barrena2" "barrena3" "altper" [16] "orgmasa1" "orgmasa2" "tratmasa"
i need subset data set every plot multiple criteria in especies(==24 & ==28). trying
<-llply(mm, subset, especie==24)
buy, worked 1 species
does know how can this? in advance
it difficult work str(mm).
no libraries needed. try like:
# input: df - output: subset plot , other criteria f <- function(x){ x.splitted <- split(x, x$plot) lapply(x.splitted, function(y) y[y$especie == 24 & y$especie == 28,]) } # mm.subset <- lapply(mm, f)
optionally may have data consolidated in single data.frame
# in case prefer data.frame instead of list of data.frames do.call(rbind, mm.subset)
Comments
Post a Comment