I am using the intervals_ex40()
function from the {SDAResources}
library. I want to extract a single value (Cluster_cover_prob
) from the output the function produces but can't do so as the result is of class NULL
.
library(SDAResources)
x <- intervals_ex40(groupcorr = .5,numintervals = 100,groupsize = 5,
sampgroups = 10)
x
[1] NULL
lx <- as.list(intervals_ex40(groupcorr = .5,numintervals = 100,groupsize = 5,
sampgroups = 10))
lx
list()
The function also produces a bunch of plots. I don't know how to work with the output of this function and ones similar to it. How do I figure out the class of the function's result and how can I save it?
I am using the intervals_ex40()
function from the {SDAResources}
library. I want to extract a single value (Cluster_cover_prob
) from the output the function produces but can't do so as the result is of class NULL
.
library(SDAResources)
x <- intervals_ex40(groupcorr = .5,numintervals = 100,groupsize = 5,
sampgroups = 10)
x
[1] NULL
lx <- as.list(intervals_ex40(groupcorr = .5,numintervals = 100,groupsize = 5,
sampgroups = 10))
lx
list()
The function also produces a bunch of plots. I don't know how to work with the output of this function and ones similar to it. How do I figure out the class of the function's result and how can I save it?
Share Improve this question edited Mar 12 at 2:35 Darren Tsai 36.3k5 gold badges25 silver badges57 bronze badges asked Mar 12 at 2:05 Ahmad Noman AlnoorAhmad Noman Alnoor 1396 bronze badges 1 |2 Answers
Reset to default 3If you check the source code of intervals_ex40()
, you will find that the author only prints the data to the console without returning it. The most straightforward approach is to modify the source code to make the function return the data.
Another approach is to use capture.output()
to store the data displayed in the console as character strings, and then parse them into an R data object.
out <- capture.output(
intervals_ex40(groupcorr = .5,numintervals = 100,groupsize = 5, sampgroups = 10)
)
out_parsed <- read.table(text = out[1:2], header = TRUE)
# Number_of_intervals SRS_cover_prob Cluster_cover_prob SRS_mean_CI_width
# 1 100 0.75 0.97 0.5676814
out_parsed$Cluster_cover_prob
# [1] 0.97
That function doesn't actually return anything. It outputs the results to the screen and produces a plot. You could use capture.output
to grab what is printed, if necessary.
x <- capture.output(
intervals_ex40(groupcorr = .5,numintervals = 100,
groupsize = 5, sampgroups = 10))
x[2] # a bunch of numbers, the third is the sought after one
library(stringr)
as.numeric(
str_match_all(x[2], '\\d+.\\d+')[[1]][3]
)
# [1] 0.97 # Cluster_cover_prob
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744772525a4592846.html
capture.output
to grab what is printed, if necessary. – Edward Commented Mar 12 at 2:23