[R] extracting numeric values returned when using the by function to run the acf function
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Tue May 10 20:11:04 CEST 2022
Just as a slight addendum to Rui's and Sarah's remarks, this is really more
of a use case for tapply() than by(), as you are only working with a
function, acf, that wants a vector as an argument rather than (several
columns of) a data frame. So the following also does the job (using a
slightly more realistic example with two groups rather than one; all the
rest of their comments are still germane, though):
> RecallSmall <- data.frame(ID=rep(c('a','b'), e=4),value=rep(c(6,5,3,4),2))
>
> out <- with(RecallSmall,tapply(value, ID,acf, na.action = na.pass, plot =
FALSE))
>
> str(out)
List of 2
$ a:List of 6
..$ acf : num [1:4, 1, 1] 1 0.15 -0.5 -0.15
..$ type : chr "correlation"
..$ n.used: int 4
..$ lag : num [1:4, 1, 1] 0 1 2 3
..$ series: chr "X[[i]]"
..$ snames: NULL
..- attr(*, "class")= chr "acf"
$ b:List of 6
..$ acf : num [1:4, 1, 1] 1 0.15 -0.5 -0.15
..$ type : chr "correlation"
..$ n.used: int 4
..$ lag : num [1:4, 1, 1] 0 1 2 3
..$ series: chr "X[[i]]"
..$ snames: NULL
..- attr(*, "class")= chr "acf"
- attr(*, "dim")= int 2
- attr(*, "dimnames")=List of 1
..$ : chr [1:2] "a" "b"
>
> sapply(out, \(x)x$acf) ## or as Rui suggested
a b
[1,] 1.00 1.00
[2,] 0.15 0.15
[3,] -0.50 -0.50
[4,] -0.15 -0.15
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, May 10, 2022 at 10:42 AM Rui Barradas <ruipbarradas using sapo.pt> wrote:
> Hello,
>
> by returns an object of class "by" that inherits from class "list". So
> assign its return value and then use sapply to extract the list members
> you want.
>
>
> RecallSmall <- data.frame(ID=c(1,1,1,1),value=c(6,5,3,4))
>
> acf_list <-
>
> by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE)
> sapply(acf_list, `[[`, 1)
> #> 1
> #> [1,] 1.00
> #> [2,] 0.15
> #> [3,] -0.50
> #> [4,] -0.15
>
>
> This can be coerced to vector with c().
>
> Hope this helps,
>
> Rui Barradas
>
> Às 18:29 de 10/05/2022, Sorkin, John escreveu:
> > I am using the by function to run the acf function. Each run of the by
> function returns more information than I want. All I want is the four
> values that acf returns, which using the data below would be
> > 1.00 0.15 -0.50 -0.15
> > How can I isolate these four values from the output returned by the by
> function.
> >
> > Sample code:
> >
> > RecallSmall <- data.frame(ID=c(1,1,1,1),value=c(6,5,3,4))
> > cat("Here are the data\n")
> > RecallSmall
> > cat("All I want is autocorrerlations, e.g. 1.00 0.15 -0.50 -0.15 \n ")
> > # attempting to subset does not work at all
> >
> by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE)$acf
> > # this gives me more than I want, not just the numeric results.
> >
> by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE)
> >
> > # For your reference, this is what the acf function returns
> > acf(RecallSmall[1:4,"value"],na.action=na.pass,plot=FALSE)
> > # For your reference, this is what the acf function returns when the
> output is subset
> > acf(RecallSmall[1:4,"value"],na.action=na.pass,plot=FALSE)Thank you, John
> >
> > Thank you,
> > John
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list