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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue May 10 19:40:09 CEST 2022


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.



More information about the R-help mailing list