[R] extracting numeric values returned when using the by function to run the acf function

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Tue May 10 20:12:15 CEST 2022

What is returned is a specialized list structure called a 'by" and getting the part you want out can be done sort of carefully and sometimes with a bit of trial and error as the contents are nested. You can use str(0 and other tools but this seems to work with care as you need [[]] not just [] to get the internal contents, not another list:
> byeby <- by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE)

> result <- byeby[[1]]$acf
> print(result)
, , 1
      [,1][1,]  1.00[2,]  0.15[3,] -0.50[4,] -0.15
To make it a more normal vector rather than an array you might want to do this.
> result <- as.vector(result)

> result

[1]  1.00  0.15 -0.50 -0.15

Again, we discourage use of packages here for some reason but there are many alternatives to using by() that get around this issue by not producing a specialized class as output. 
And a review ofthe help page for by() shows you could have added simplify=TRUE to make a normal list as in:

by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE, simplify=TRUE)

But it remains a nested list and getting the part you want in one long command line that works for me is:

by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE, simplify=TRUE)[[1]]$acf[1:4]

-----Original Message-----
From: Sorkin, John <jsorkin using som.umaryland.edu>
To: r-help using r-project.org (r-help using r-project.org) <r-help using r-project.org>
Sent: Tue, May 10, 2022 1:29 pm
Subject: [R] extracting numeric values returned when using the by function to run the acf function

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")
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
# this gives me more than I want, not just the numeric results.

# For your reference, this is what the acf function returns
# 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,

    [[alternative HTML version deleted]]

R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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