A data set
set.seed(111)
library(vegan)
library(truncnorm)
df <- data.frame(sp1 = rep(rtruncnorm(10, a=0, b=1, mean = 0.50, sd = 0.2), times = 10),
sp2 = rep(rtruncnorm(10, a=0, b=1, mean = 0.70, sd = 0.1), times = 10),
env1 = rep(rtruncnorm(10, a=0, b=1, mean = 0.45, sd = 0.6), times = 10),
env2 = rep(rtruncnorm(10, a=0, b=1, mean = 0.65, sd = 0.6), times = 10)
)
Using this data, I calculate a test rda
that is stored in test
: which is a list of lists.
spe.rda <- rda(df[,c(1:2)] ~ ., data = df[,c(3:4)])
test <- ordiR2step(rda(df[,c(1:2)]~1, data=df[,c(3:4)]),
scope = formula(spe.rda), direction= "forward", R2scope=TRUE, pstep=1000)
I want to extract R2 from test
to store the following in a dataframe with two columns as such. How can I do this? I can't spot the statistic when I open each of the lists but it does appear on the console.
variables R2.adjusted
+ env1 0.08636035
<All variables> 0.08220187
+ env2 0.00842866
<none> 0.00000000
I'm not super familiar with this package, but from inspecting the source code it looks like the output that's printed is never returned - try looking at ordiR2step
in the console. You could try creating your own version that also returns it?
One generic workaround would be to use capture.output
to save the printed stuff as a variable:
r2_text = capture.output(test <- ordiR2step(rda(df[,c(1:2)]~1, data=df[,c(3:4)]),
scope = formula(spe.rda), direction= "forward",
R2scope=TRUE, pstep=1000))
The r2_text
object looks like this:
[1] "Step: R2.adj= 0 " "Call: df[, c(1:2)] ~ 1 "
[3] " " " R2.adjusted"
[5] "+ env1 0.08636035" "<All variables> 0.08220187"
[7] "+ env2 0.00842866" "<none> 0.00000000"
[9] ""
The saved output would need tidied up to get it into a data frame but it should be doable.