Sorkin, John
2022-May-10 17:29 UTC
[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") 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]]
Rui Barradas
2022-May-10 17:40 UTC
[R] extracting numeric values returned when using the by function to run the acf function
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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Eric Berger
2022-May-10 17:40 UTC
[R] extracting numeric values returned when using the by function to run the acf function
as.numeric(by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"],acf,na.action=na.pass,plot=FALSE)[[1]]$acf) On Tue, May 10, 2022 at 8:29 PM Sorkin, John <jsorkin at som.umaryland.edu> wrote:> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Sarah Goslee
2022-May-10 17:44 UTC
[R] extracting numeric values returned when using the by function to run the acf function
Hi, You can either write a small function that returns just the values you want, or use a convenience function within by, as in:> by(RecallSmall[1:4,"value"],RecallSmall[1:4,"ID"], FUN=function(x){acf(x, na.action=na.pass,plot=FALSE)$acf})RecallSmall[1:4, "ID"]: 1 , , 1 [,1] [1,] 1.00 [2,] 0.15 [3,] -0.50 [4,] -0.15 But I suspect that you would get something more like what you want by splitting the data on ID, and then using sapply:> RecallSmall <- data.frame(ID=c(1,1,1,1,2,2,2,2), value=c(6,5,3,4,6,5,3,4)) > result <- split(RecallSmall[["value"]], RecallSmall[["ID"]]) > result <- sapply(result, FUN=function(x){acf(x, na.action=na.pass,plot=FALSE)$acf}) > result1 2 [1,] 1.00 1.00 [2,] 0.15 0.15 [3,] -0.50 -0.50 [4,] -0.15 -0.15 Sarah On Tue, May 10, 2022 at 1:29 PM Sorkin, John <jsorkin at som.umaryland.edu> wrote:> > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Sarah Goslee (she/her) numberwright.com
Avi Gross
2022-May-10 18:12 UTC
[R] extracting numeric values returned when using the by function to run the acf function
John, 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 at som.umaryland.edu> To: r-help at r-project.org (r-help at r-project.org) <r-help at 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") 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]