[R] using lapply to get function values
Hi R users, I need to apply a function on a list of vectors. This is simple when I use functions that returns only one numerical value such as 'mean' or 'variance'. Things get complex when I use functions returning a list of value, such as 'acf'. In the following example I first create a list of vectors with equal length 10. Then I try to get the first order autocorrelation coefficient of each vector using lapply: # library(stats) X=rnorm(1:100) Y=split(X,as.numeric(gl(length(X),10,length(X# to divide X into 10 sub-vectors app=lapply(Y,FUN=acf,lag.max=1,plot=F) app # I need to know how to display other values of the function e.g. 'type', 'lag' not just 'acf'. Because using app=lapply(Y,FUN=acf$type,lag.max=1,plot=F) prompts the error object of type 'closure' is not subsettable Note that this is only an illustrative example. The function I am studying is not 'acf', but it also return a list of values and I need to call out specific values. Helps and comments are welcome. Long -- View this message in context: http://r.789695.n4.nabble.com/using-lapply-to-get-function-values-tp4683373.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.
Re: [R] Splitting a vector
This does what I needed. However, as the output is a list object, is there any way to apply a function to such object? For example if I want to compute the mean for the 4th subvectors, I can't simply use: # Y=split(X,as.numeric(gl(length(X),3,length(X mean(Y[4]) # as the error message shows argument is not numeric or logical -- View this message in context: http://r.789695.n4.nabble.com/Splitting-a-vector-tp4681930p4681987.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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] Splitting a vector
Hi, I am quite new to R so I know that this probably is very basic , but how can I split a sequence of number into multiple parts with equal length? For example I have a vector X=c(1:12) I simply need to split it into sub-vectors with the same length N . Say N=3 then I need the output to be like 1 2 3 4 5 6 7 8 9 10 11 12 And better if the sub-vectors can be named so that I can use them later for individual study, probably a do-loop in which a function can be applied to them. I just want them to be in consecutive order so really no fancy conditions here. Any helps to this amateur is greatly appreciated, Long -- View this message in context: http://r.789695.n4.nabble.com/Splitting-a-vector-tp4681930.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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] installing package from source
Hi R users, Currently I want to fit a FIGARCH model to a dataset. The only package that allow for it that I could find is fGarch. However it seems that the FIGARCH model class fitting of this package has been moved to Oxmetrics. I tried to install the old versions of it using 'tar.gz' files from CRAN archive http://cran.r-project.org/src/contrib/Archive/fGarch/ http://cran.r-project.org/src/contrib/Archive/fGarch/ but not sure how it works. I tried install.packages(myfilepath\fGarch_260.71.tar.gz, repos = NULL, type=source) And received this error: Warning: invalid package './I:_R filesGarch_260.71.tar.gz' Error: ERROR: no packages specified Warning messages: 1: running command 'I:/01_RFI~1/INSTAL~1/R-30~1.1/bin/i386/R CMD INSTALL -l I:\01_R files\installment\R-3.0.1\library ./I:_R files Garch_260.71.tar.gz' had status 1 2: In install.packages(I:\001_R files\fGarch_260.71.tar.gz, repos = NULL, : installation of package ‘./I:_R filesGarch_260.71.tar.gz’ had non-zero exit status Any helps on this? Regards, Long -- View this message in context: http://r.789695.n4.nabble.com/installing-package-from-source-tp4678928.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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.