diff --git a/00_Mesobromion_DataPreparation.R b/00_Mesobromion_DataPreparation.R index a12a4be11997fad9e105747bb73868a8cf3c2e47..cd991f39b9fff4b458be114247b40ff75378634a 100644 --- a/00_Mesobromion_DataPreparation.R +++ b/00_Mesobromion_DataPreparation.R @@ -100,42 +100,55 @@ env.all <- env ### 3. Import species data #### # columns in species correspond to those in env # there is no PlotObservationID (yet) -species0 <- read.table("_data/Mesobromion/GVRD_Mes2_veg1.csv", sep = ",", header=T) -dim(species0) #6868 obs. of 907 variables: -rownames(species0) <- env0$RELEVE_NR +#species0 <- read.table("_data/Mesobromion/GVRD_Mes2_veg1.csv", sep = ",", header=T) +species.proz <- read_csv("_data/Mesobromion/GVRD_Mes2_proz.csv", locale = locale(encoding = 'latin1')) +species.proz$RELEVE_NR <- env0$RELEVE_NR +#dim(species0) #6868 obs. of 907 variables: +#rownames(species0) <- env0$RELEVE_NR +dim(species.proz) #6868 obs. of 903 variables: + ## select only plots already selected in env species <- env %>% dplyr::select(RELEVE_NR) %>% - left_join(species0 %>% - mutate(RELEVE_NR=env0$RELEVE_NR), + left_join(species.proz %>% + #rownames_to_column("RELEVE_NR") %>% + mutate(RELEVE_NR=as.numeric(RELEVE_NR)), by="RELEVE_NR") %>% - column_to_rownames("RELEVE_NR") %>% + filter_all(any_vars(!is.na(.))) %>% ## delete species not appearing in any plot - dplyr::select(colnames(.)[which(colSums(.)!=0)]) + dplyr::select(colnames(.)[which(colSums(., na.rm=T)!=0)]) #dplyr::select(traits$species0) +dim(species) # [1] 5810 878 -dim(species) # [1] 5810 881 releve08trait <- species %>% - rownames_to_column("RELEVE_NR") %>% - reshape2::melt(.id="RELEVE_NR") %>% - rename(species0=variable, pres=value) %>% - as.tbl() %>% - filter(pres>0) %>% - arrange(RELEVE_NR) %>% + #transform percentage cover to relative.cover + mutate(sumVar = rowSums(.[-1])) %>% + mutate_at(.vars=vars(-RELEVE_NR), + .funs=~./sumVar) %>% + dplyr::select(-sumVar) %>% + # transform to long format and attach traits + #rownames_to_column("RELEVE_NR") %>% + #reshape2::melt(.id="RELEVE_NR") %>% + #rename(species0=variable, pres=value) %>% + #as.tbl() %>% + #arrange(RELEVE_NR) %>% + pivot_longer(names_to = "species0", values_to = "abund", -RELEVE_NR) %>% + filter(abund>0) %>% ## attach traits left_join(traits %>% dplyr::select(-species), by="species0") %>% mutate_at(.vars = vars(LEB_F_Makrophanerophyt:Disp.unit.leng), - .funs = list(~if_else(is.na(.),0,1) * pres)) %>% + .funs = list(~if_else(is.na(.),0,1) * abund)) %>% group_by(RELEVE_NR) %>% summarize_at(.vars= vars(LEB_F_Makrophanerophyt:Disp.unit.leng), - .funs = list(~mean(.))) %>% + .funs = list(~sum(.))) %>% dplyr::select(RELEVE_NR, order(colnames(.))) %>% - reshape2::melt(id.vars="RELEVE_NR", value.name="trait.coverage") %>% + pivot_longer(values_to="trait.coverage", names_to="trait", -RELEVE_NR) %>% + #reshape2::melt(id.vars="RELEVE_NR", value.name="trait.coverage") %>% group_by(RELEVE_NR) %>% - summarize(ntraits08=mean(trait.coverage>=0.8)) %>% + summarize(ntraits08=mean(trait.coverage>=.8)) %>% #select only those releves where we have a coverage of >0.8 for all traits filter(ntraits08==1) %>% pull(RELEVE_NR) @@ -143,11 +156,12 @@ releve08trait <- species %>% set.seed(1984) releve08trait.samp <- sample(releve08trait, round(length(releve08trait)/10), replace=F) species <- species %>% - rownames_to_column("RELEVE_NR") %>% + # rownames_to_column("RELEVE_NR") %>% filter(RELEVE_NR %in% releve08trait.samp) %>% #column_to_rownames("RELEVE_NR") %>% #as.tbl() %>% - dplyr::select(RELEVE_NR, one_of(traits$species0)) + dplyr::select(RELEVE_NR, one_of(traits$species0)) %>% + dplyr::select(colnames(.)[which(colSums(.)!=0)]) env <- env %>% @@ -228,9 +242,9 @@ traits <- traits %>% ### ordered factors -dim(species) #558 783 -dim(traits) #783 53 -dim(env) #558 8 +dim(species) #558 783 -> 531 461 +dim(traits) #783 53 -> 460 53 +dim(env) #558 8 -> 531 8 @@ -278,22 +292,13 @@ env <- env %>% ##export for Valerio -write_delim(species, path="_data/Mesobromion/species.out.10perc.txt", delim="\t") -write_delim(traits, path="_data/Mesobromion/traits.out.10perc.txt", delim="\t") -write_delim(env, path="_data/Mesobromion/env.10perc.txt", delim="\t") - -## version without missing species -empty <- which(colSums(species[,-1])==0) -traits_nozero <- traits[-empty,] -species_nozero <- species[,-(empty+1)] - -write_delim(species_nozero , path="_data/Mesobromion/species.out.10perc_nozero.txt", delim="\t") -write_delim(traits_nozero, path="_data/Mesobromion/traits.out.10perc_nozero.txt", delim="\t") - +write_delim(species, path="_data/Mesobromion/species.out.10perc.cov.txt", delim="\t") +write_delim(traits, path="_data/Mesobromion/traits.out.10perc.cov.txt", delim="\t") +write_delim(env, path="_data/Mesobromion/env.10perc.cov.txt", delim="\t") write_delim(species %>% dplyr::select(RELEVE_NR), - path="_derived/Mesobromion/ReleveList.txt", delim="\t") + path="_derived/Mesobromion/ReleveList.cov.txt", delim="\t")