Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
HIDDEN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sPlot
HIDDEN
Commits
47699ace
Commit
47699ace
authored
4 years ago
by
Francesco Sabatini
Browse files
Options
Downloads
Patches
Plain Diff
Data Preparation
parent
fb550be0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
00_Mesobromion_DataPreparation.R
+41
-36
41 additions, 36 deletions
00_Mesobromion_DataPreparation.R
with
41 additions
and
36 deletions
00_Mesobromion_DataPreparation.R
+
41
−
36
View file @
47699ace
...
@@ -100,42 +100,55 @@ env.all <- env
...
@@ -100,42 +100,55 @@ env.all <- env
### 3. Import species data ####
### 3. Import species data ####
# columns in species correspond to those in env
# columns in species correspond to those in env
# there is no PlotObservationID (yet)
# there is no PlotObservationID (yet)
species0
<-
read.table
(
"_data/Mesobromion/GVRD_Mes2_veg1.csv"
,
sep
=
","
,
header
=
T
)
#species0 <- read.table("_data/Mesobromion/GVRD_Mes2_veg1.csv", sep = ",", header=T)
dim
(
species0
)
#6868 obs. of 907 variables:
species.proz
<-
read_csv
(
"_data/Mesobromion/GVRD_Mes2_proz.csv"
,
locale
=
locale
(
encoding
=
'latin1'
))
rownames
(
species0
)
<-
env0
$
RELEVE_NR
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
## select only plots already selected in env
species
<-
env
%>%
species
<-
env
%>%
dplyr
::
select
(
RELEVE_NR
)
%>%
dplyr
::
select
(
RELEVE_NR
)
%>%
left_join
(
species0
%>%
left_join
(
species.proz
%>%
mutate
(
RELEVE_NR
=
env0
$
RELEVE_NR
),
#rownames_to_column("RELEVE_NR") %>%
mutate
(
RELEVE_NR
=
as.numeric
(
RELEVE_NR
)),
by
=
"RELEVE_NR"
)
%>%
by
=
"RELEVE_NR"
)
%>%
column_to_rownames
(
"RELEVE_NR"
)
%>%
filter_all
(
any_vars
(
!
is.na
(
.
))
)
%>%
## delete species not appearing in any plot
## 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)
#dplyr::select(traits$species0)
dim
(
species
)
# [1] 5810 878
dim
(
species
)
# [1] 5810 881
releve08trait
<-
species
%>%
releve08trait
<-
species
%>%
rownames_to_column
(
"RELEVE_NR"
)
%>%
#transform percentage cover to relative.cover
reshape2
::
melt
(
.id
=
"RELEVE_NR"
)
%>%
mutate
(
sumVar
=
rowSums
(
.
[
-1
]))
%>%
rename
(
species0
=
variable
,
pres
=
value
)
%>%
mutate_at
(
.vars
=
vars
(
-
RELEVE_NR
),
as.tbl
()
%>%
.funs
=~
.
/
sumVar
)
%>%
filter
(
pres
>
0
)
%>%
dplyr
::
select
(
-
sumVar
)
%>%
arrange
(
RELEVE_NR
)
%>%
# 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
## attach traits
left_join
(
traits
%>%
left_join
(
traits
%>%
dplyr
::
select
(
-
species
),
by
=
"species0"
)
%>%
dplyr
::
select
(
-
species
),
by
=
"species0"
)
%>%
mutate_at
(
.vars
=
vars
(
LEB_F_Makrophanerophyt
:
Disp.unit.leng
),
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
)
%>%
group_by
(
RELEVE_NR
)
%>%
summarize_at
(
.vars
=
vars
(
LEB_F_Makrophanerophyt
:
Disp.unit.leng
),
summarize_at
(
.vars
=
vars
(
LEB_F_Makrophanerophyt
:
Disp.unit.leng
),
.funs
=
list
(
~
mean
(
.
)))
%>%
.funs
=
list
(
~
sum
(
.
)))
%>%
dplyr
::
select
(
RELEVE_NR
,
order
(
colnames
(
.
)))
%>%
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
)
%>%
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
#select only those releves where we have a coverage of >0.8 for all traits
filter
(
ntraits08
==
1
)
%>%
filter
(
ntraits08
==
1
)
%>%
pull
(
RELEVE_NR
)
pull
(
RELEVE_NR
)
...
@@ -143,11 +156,12 @@ releve08trait <- species %>%
...
@@ -143,11 +156,12 @@ releve08trait <- species %>%
set.seed
(
1984
)
set.seed
(
1984
)
releve08trait.samp
<-
sample
(
releve08trait
,
round
(
length
(
releve08trait
)
/
10
),
replace
=
F
)
releve08trait.samp
<-
sample
(
releve08trait
,
round
(
length
(
releve08trait
)
/
10
),
replace
=
F
)
species
<-
species
%>%
species
<-
species
%>%
rownames_to_column
(
"RELEVE_NR"
)
%>%
#
rownames_to_column("RELEVE_NR") %>%
filter
(
RELEVE_NR
%in%
releve08trait.samp
)
%>%
filter
(
RELEVE_NR
%in%
releve08trait.samp
)
%>%
#column_to_rownames("RELEVE_NR") %>%
#column_to_rownames("RELEVE_NR") %>%
#as.tbl() %>%
#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
%>%
env
<-
env
%>%
...
@@ -228,9 +242,9 @@ traits <- traits %>%
...
@@ -228,9 +242,9 @@ traits <- traits %>%
### ordered factors
### ordered factors
dim
(
species
)
#558 783
dim
(
species
)
#558 783
-> 531 461
dim
(
traits
)
#783 53
dim
(
traits
)
#783 53
-> 460 53
dim
(
env
)
#558 8
dim
(
env
)
#558 8
-> 531 8
...
@@ -278,22 +292,13 @@ env <- env %>%
...
@@ -278,22 +292,13 @@ env <- env %>%
##export for Valerio
##export for Valerio
write_delim
(
species
,
path
=
"_data/Mesobromion/species.out.10perc.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.txt"
,
delim
=
"\t"
)
write_delim
(
traits
,
path
=
"_data/Mesobromion/traits.out.10perc.cov.txt"
,
delim
=
"\t"
)
write_delim
(
env
,
path
=
"_data/Mesobromion/env.10perc.txt"
,
delim
=
"\t"
)
write_delim
(
env
,
path
=
"_data/Mesobromion/env.10perc.cov.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
%>%
write_delim
(
species
%>%
dplyr
::
select
(
RELEVE_NR
),
dplyr
::
select
(
RELEVE_NR
),
path
=
"_derived/Mesobromion/ReleveList.txt"
,
delim
=
"\t"
)
path
=
"_derived/Mesobromion/ReleveList.
cov.
txt"
,
delim
=
"\t"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment