admin管理员组文章数量:1391754
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
本文标签: quotNULLquot output when trying to save the result of a function in RStack Overflow
版权声明:本文标题:"NULL" output when trying to save the result of a function in R - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744772525a2624426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
capture.output
to grab what is printed, if necessary. – Edward Commented Mar 12 at 2:23