[R] shifting data in matrix by n rows
Hi everyone, I have some data in a matrix, and I want to shift it down by one row. The matrix in question has a date column. Does anyone know of a way to shift the data by one row, whilst preserving the date column in the matrix - i.e. shift the data and leave the date column in the current location? Thanks, rcoder -- View this message in context: http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18368420.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] shifting data in matrix by n rows
Original Table: Date Apples Oranges Pears 1/7 2 35 2/7 1 47 3/7 3 810 4/7 5 72 5/7 6 35 What I want after shift (data shifted, dates left unchanged) Date Apples Oranges Pears 1/7 na na na 2/7 2 35 3/7 1 47 4/7 3 810 5/7 5 72 jholtman wrote: > > Can you provide commented, minimal, self-contained, reproducible code. > If you don't have code, at least provide a before/after version of > the matrix that you would like. It is easy to use indexing to move > stuff around, we just have to know what is it that you want to move. > > On Wed, Jul 9, 2008 at 2:57 PM, rcoder <[EMAIL PROTECTED]> wrote: >> >> Hi everyone, >> >> I have some data in a matrix, and I want to shift it down by one row. The >> matrix in question has a date column. Does anyone know of a way to shift >> the >> data by one row, whilst preserving the date column in the matrix - i.e. >> shift the data and leave the date column in the current location? >> >> Thanks, >> >> rcoder >> -- >> View this message in context: >> http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18368420.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. >> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? > > __ > 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. > > -- View this message in context: http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18372509.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] shifting data in matrix by n rows
Hi everyone, Thanks very much for all your replies. I'm interested in hearing more about the lag function. I remember coming across this in the R intro manual, but I couldn't figure out how to apply it to my case. Does anyone know how it is applied, assuming a time series data frame? Thanks, rcoder Gabor Grothendieck wrote: > > If its a zoo or ts time series you can use the lag function. > > On Wed, Jul 9, 2008 at 2:57 PM, rcoder <[EMAIL PROTECTED]> wrote: >> >> Hi everyone, >> >> I have some data in a matrix, and I want to shift it down by one row. The >> matrix in question has a date column. Does anyone know of a way to shift >> the >> data by one row, whilst preserving the date column in the matrix - i.e. >> shift the data and leave the date column in the current location? >> >> Thanks, >> >> rcoder >> -- >> View this message in context: >> http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18368420.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-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. > > -- View this message in context: http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18389841.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] shifting data in matrix by n rows
This is great! Thank you very much Gabor. rcoder Gabor Grothendieck wrote: > > See ?lag and in zoo ?lag.zoo. Both pages have > examples. Using lag.zoo here it is with your data: > > Lines <- "Date Apples Oranges Pears > 1/7 2 35 > 2/7 1 47 > 3/7 3 810 > 4/7 5 72 > 5/7 6 35" > > library(zoo) > > # z <- read.zoo("myfile.dat", header = TRUE, format = "%d/%m") > z <- read.zoo(textConnection(Lines), header = TRUE, format = "%d/%m") > > lag(z, -1, na.pad = TRUE) > > On Thu, Jul 10, 2008 at 3:12 PM, rcoder <[EMAIL PROTECTED]> wrote: >> >> Hi everyone, >> >> Thanks very much for all your replies. >> >> I'm interested in hearing more about the lag function. I remember coming >> across this in the R intro manual, but I couldn't figure out how to apply >> it >> to my case. Does anyone know how it is applied, assuming a time series >> data >> frame? >> >> Thanks, >> >> rcoder >> >> >> >> Gabor Grothendieck wrote: >>> >>> If its a zoo or ts time series you can use the lag function. >>> >>> On Wed, Jul 9, 2008 at 2:57 PM, rcoder <[EMAIL PROTECTED]> >>> wrote: >>>> >>>> Hi everyone, >>>> >>>> I have some data in a matrix, and I want to shift it down by one row. >>>> The >>>> matrix in question has a date column. Does anyone know of a way to >>>> shift >>>> the >>>> data by one row, whilst preserving the date column in the matrix - i.e. >>>> shift the data and leave the date column in the current location? >>>> >>>> Thanks, >>>> >>>> rcoder >>>> -- >>>> View this message in context: >>>> http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18368420.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-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. >>> >>> >> >> -- >> View this message in context: >> http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18389841.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-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. > > -- View this message in context: http://www.nabble.com/shifting-data-in-matrix-by-n-rows-tp18368420p18393177.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] Mapping data onto 1-10 score
Hi everyone, I want to score a set of data (-ve to +ve) using a 0-10 scale. I have the data in an R matrix, so I need to add another column, containing the scores and resave. I would be grateful for suggestions on how best to do this. Thanks, rcoder -- View this message in context: http://www.nabble.com/Mapping-data-onto-1-10-score-tp18463694p18463694.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] Mapping data onto score
Hi everyone, I want to score a set of data (-ve to +ve) using a 0-10 scale. I have the data in an R matrix, so I need to add another column, containing the scores and resave. I would be grateful for suggestions on how best to do this. Thanks, rcoder -- View this message in context: http://www.nabble.com/Mapping-data-onto-score-tp18463695p18463695.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] Mapping data onto score
Hi Ben, Yes, this is more or less what I want to do. I want to apply this data in columns in a matrix, and insert the results to additional columns. I am not entirely aware of a good way of doing this. e.g. take data from column B, apply normalisation, and feed output into column C Thanks, rcoder Ben Tupper wrote: > > > On Jul 15, 2008, at 8:16 AM, rcoder wrote: > >> >> Hi everyone, >> >> I want to score a set of data (-ve to +ve) using a 0-10 scale. I >> have the >> data in an R matrix, so I need to add another column, containing >> the scores >> and resave. >> > > Hi, > > I am a little fuzzy on what you are asking, but my guess is that you > want to normalize the data into the 0-1 range then multiply by 10. > > values <- rnorm(10) #some numbers > mm <- range(values) #the minmax range > scaled <- (values-mm[1])/(mm[2]-mm[1]) #normalize into 0-1 > scaled10 <- 10 * scaled #scale 0-10 > > Is that what you seek? > Ben > > __ > 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. > > -- View this message in context: http://www.nabble.com/Mapping-data-onto-score-tp18463695p18475083.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] Mapping data onto score
Thank you Ben! This is very clear. rcoder Ben Tupper wrote: > > > On Jul 15, 2008, at 5:16 PM, rcoder wrote: > >> >> Hi Ben, >> Yes, this is more or less what I want to do. I want to apply this >> data in >> columns in a matrix, and insert the results to additional columns. >> I am not >> entirely aware of a good way of doing this. >> >> e.g. take data from column B, apply normalisation, and feed output >> into >> column C >> > > Oh, > > I think you want to use cbind() to add columns to a matrix. Perhaps > like this which works with the second column... > > v <- matrix(data = rnorm(100), nrow = 10, ncol = 10) > mm <- range(v[,2]) > s <- 10 * (v[,2]-mm[1])/(mm[2]-mm[1]) > v2 <- cbind(v, s) > > Cheers, > Ben > > > >> Thanks, >> >> rcoder >> >> >> >> Ben Tupper wrote: >>> >>> >>> On Jul 15, 2008, at 8:16 AM, rcoder wrote: >>> >>>> >>>> Hi everyone, >>>> >>>> I want to score a set of data (-ve to +ve) using a 0-10 scale. I >>>> have the >>>> data in an R matrix, so I need to add another column, containing >>>> the scores >>>> and resave. >>>> >>> >>> Hi, >>> >>> I am a little fuzzy on what you are asking, but my guess is that you >>> want to normalize the data into the 0-1 range then multiply by 10. >>> >>> values <- rnorm(10) #some numbers >>> mm <- range(values) #the minmax range >>> scaled <- (values-mm[1])/(mm[2]-mm[1]) #normalize into 0-1 >>> scaled10 <- 10 * scaled #scale 0-10 >>> >>> Is that what you seek? >>> Ben >>> >>> __ >>> 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. >>> >>> >> >> -- >> View this message in context: http://www.nabble.com/Mapping-data- >> onto-score-tp18463695p18475083.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. > > Ben Tupper > [EMAIL PROTECTED] > > I GoodSearch for Ashwood Waldorf School. > > Raise money for your favorite charity or school just by searching the > Internet with GoodSearch - www.goodsearch.com - powered by Yahoo! > > __ > 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. > > -- View this message in context: http://www.nabble.com/Mapping-data-onto-score-tp18463695p18476293.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] Mapping data onto score
I am trying to apply the solution you mentioned to all columns in a matrix, and output the results to another matrix and append the two using the cbind function. I have written something that works, using a nested For loop to go through all the cells in the target matrix, but the trouble is that the process takes a while to run (3-4 mins). The matrix is large, about 2000 by 1, so this could be a reason for the slow speed. However, I'm convinced there is a faster way of applying this mapping procedure to all columns in a matrix and outoutting into columns in a separate matrix. I would be grateful for any suggestions on this slight modification. Otherwise, I can make do with my version. Thanks, rcoder rcoder wrote: > > Thank you Ben! This is very clear. > rcoder > > > Ben Tupper wrote: >> >> >> On Jul 15, 2008, at 5:16 PM, rcoder wrote: >> >>> >>> Hi Ben, >>> Yes, this is more or less what I want to do. I want to apply this >>> data in >>> columns in a matrix, and insert the results to additional columns. >>> I am not >>> entirely aware of a good way of doing this. >>> >>> e.g. take data from column B, apply normalisation, and feed output >>> into >>> column C >>> >> >> Oh, >> >> I think you want to use cbind() to add columns to a matrix. Perhaps >> like this which works with the second column... >> >> v <- matrix(data = rnorm(100), nrow = 10, ncol = 10) >> mm <- range(v[,2]) >> s <- 10 * (v[,2]-mm[1])/(mm[2]-mm[1]) >> v2 <- cbind(v, s) >> >> Cheers, >> Ben >> >> >> >>> Thanks, >>> >>> rcoder >>> >>> >>> >>> Ben Tupper wrote: >>>> >>>> >>>> On Jul 15, 2008, at 8:16 AM, rcoder wrote: >>>> >>>>> >>>>> Hi everyone, >>>>> >>>>> I want to score a set of data (-ve to +ve) using a 0-10 scale. I >>>>> have the >>>>> data in an R matrix, so I need to add another column, containing >>>>> the scores >>>>> and resave. >>>>> >>>> >>>> Hi, >>>> >>>> I am a little fuzzy on what you are asking, but my guess is that you >>>> want to normalize the data into the 0-1 range then multiply by 10. >>>> >>>> values <- rnorm(10) #some numbers >>>> mm <- range(values) #the minmax range >>>> scaled <- (values-mm[1])/(mm[2]-mm[1]) #normalize into 0-1 >>>> scaled10 <- 10 * scaled #scale 0-10 >>>> >>>> Is that what you seek? >>>> Ben >>>> >>>> __ >>>> 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. >>>> >>>> >>> >>> -- >>> View this message in context: http://www.nabble.com/Mapping-data- >>> onto-score-tp18463695p18475083.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. >> >> Ben Tupper >> [EMAIL PROTECTED] >> >> I GoodSearch for Ashwood Waldorf School. >> >> Raise money for your favorite charity or school just by searching the >> Internet with GoodSearch - www.goodsearch.com - powered by Yahoo! >> >> __ >> 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. >> >> > > -- View this message in context: http://www.nabble.com/Mapping-data-onto-score-tp18463695p18494272.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] Mapping data onto score
Here's my code: nc<-ncol(mat) #setting end point in counter to number of cols in sm nr<-nrow(mat) mm <- array(NA, dim=c(2, nc)) #to hold min/max ranges sc <- array(NA, dim=c(nr, nc)) #to hold percentile scales for (n in 1:nc) { #now calculate respective ranges for data matrix mm[,n]<-range(mat[,n],na.rm=T) #inserts min/max values into sc matrix for (m in 1:nr) { sc[m,n]<-100*(mat[m,n]-mm[1,n])/(mm[2,n]-mm[1,n]) #re-scaling onto percentile ranking } } rcoder rcoder wrote: > > I am trying to apply the solution you mentioned to all columns in a > matrix, and output the results to another matrix and append the two using > the cbind function. > > I have written something that works, using a nested For loop to go through > all the cells in the target matrix, but the trouble is that the process > takes a while to run (3-4 mins). The matrix is large, about 2000 by 1, > so this could be a reason for the slow speed. However, I'm convinced there > is a faster way of applying this mapping procedure to all columns in a > matrix and outoutting into columns in a separate matrix. > > I would be grateful for any suggestions on this slight modification. > Otherwise, I can make do with my version. > > Thanks, > > rcoder > > > rcoder wrote: >> >> Thank you Ben! This is very clear. >> rcoder >> >> >> Ben Tupper wrote: >>> >>> >>> On Jul 15, 2008, at 5:16 PM, rcoder wrote: >>> >>>> >>>> Hi Ben, >>>> Yes, this is more or less what I want to do. I want to apply this >>>> data in >>>> columns in a matrix, and insert the results to additional columns. >>>> I am not >>>> entirely aware of a good way of doing this. >>>> >>>> e.g. take data from column B, apply normalisation, and feed output >>>> into >>>> column C >>>> >>> >>> Oh, >>> >>> I think you want to use cbind() to add columns to a matrix. Perhaps >>> like this which works with the second column... >>> >>> v <- matrix(data = rnorm(100), nrow = 10, ncol = 10) >>> mm <- range(v[,2]) >>> s <- 10 * (v[,2]-mm[1])/(mm[2]-mm[1]) >>> v2 <- cbind(v, s) >>> >>> Cheers, >>> Ben >>> >>> >>> >>>> Thanks, >>>> >>>> rcoder >>>> >>>> >>>> >>>> Ben Tupper wrote: >>>>> >>>>> >>>>> On Jul 15, 2008, at 8:16 AM, rcoder wrote: >>>>> >>>>>> >>>>>> Hi everyone, >>>>>> >>>>>> I want to score a set of data (-ve to +ve) using a 0-10 scale. I >>>>>> have the >>>>>> data in an R matrix, so I need to add another column, containing >>>>>> the scores >>>>>> and resave. >>>>>> >>>>> >>>>> Hi, >>>>> >>>>> I am a little fuzzy on what you are asking, but my guess is that you >>>>> want to normalize the data into the 0-1 range then multiply by 10. >>>>> >>>>> values <- rnorm(10) #some numbers >>>>> mm <- range(values) #the minmax range >>>>> scaled <- (values-mm[1])/(mm[2]-mm[1]) #normalize into 0-1 >>>>> scaled10 <- 10 * scaled #scale 0-10 >>>>> >>>>> Is that what you seek? >>>>> Ben >>>>> >>>>> __ >>>>> 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. >>>>> >>>>> >>>> >>>> -- >>>> View this message in context: http://www.nabble.com/Mapping-data- >>>> onto-score-tp18463695p18475083.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. >>> >>> Ben Tupper >>> [EMAIL PROTECTED] >>> >>> I GoodSearch for Ashwood Waldorf School. >>> >>> Raise money for your favorite charity or school just by searching the >>> Internet with GoodSearch - www.goodsearch.com - powered by Yahoo! >>> >>> __ >>> 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. >>> >>> >> >> > > -- View this message in context: http://www.nabble.com/Mapping-data-onto-score-tp18463695p18494357.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] Rolling range and regression calculations
Hi everyone, I want to calculate a min and max (i.e. range) on a rolling time frame of 50 periods for a matrix with the number of periods extending along the row direction. So for 300 periods, there will be 6 time frame windows per column, and 6 min max pairs. I then want to o/p these pairs to a separate matrix. On a separate matter, I have a matrix containing data on which perform a regresssion over a rolling time period. Is there a convenient way I can do this for each column, and then save the gradient to an o/p matrix? Thanks, rcoder -- View this message in context: http://www.nabble.com/Rolling-range-and-regression-calculations-tp18643465p18643465.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] Rolling range and regression calculations
Hi Achim, Thanks for your reply. rollmean and rollmax functions exist, but is there anything for returning the minima on a rolling basis? I know there is no rollmin in the zoo library. Thanks, rcoder Achim Zeileis wrote: > > On Thu, 24 Jul 2008, rcoder wrote: > >> Hi everyone, >> >> I want to calculate a min and max (i.e. range) on a rolling time frame of >> 50 >> periods for a matrix with the number of periods extending along the row >> direction. So for 300 periods, there will be 6 time frame windows per >> column, and 6 min max pairs. I then want to o/p these pairs to a separate >> matrix. >> >> On a separate matter, I have a matrix containing data on which perform a >> regresssion over a rolling time period. Is there a convenient way I can >> do >> this for each column, and then save the gradient to an o/p matrix? > > Look at the package "zoo", specifically the examples on the manual pages > of ?rollapply and ?aggregate.zoo. These should be helpful in doing what > you want. The package vignettes have further worked examples. > > hth, > Z > >> Thanks, >> >> rcoder >> -- >> View this message in context: >> http://www.nabble.com/Rolling-range-and-regression-calculations-tp18643465p18643465.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-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. > > -- View this message in context: http://www.nabble.com/Rolling-range-and-regression-calculations-tp18643465p18650915.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] Rolling range and regression calculations
You mean embedding something like a range() function in a rollapply function? I'm just not sure how it will handle outputting max and min values in sequence and in the correct columns. On a parallel topic, I would like to find some way of generating a matrix containing rows that are the product of sequential rows in a preceeding matrix: i.e. rows 1*2 -> row 1 in o/p matrix; rows2*3-> row 2; rows 4*5->row3 etc. Thanks, rcoder stephen sefick wrote: > > how about rollapply in the zoo package? > > On Fri, Jul 25, 2008 at 8:37 AM, rcoder <[EMAIL PROTECTED]> wrote: > >> >> Hi Achim, >> >> Thanks for your reply. rollmean and rollmax functions exist, but is there >> anything for returning the minima on a rolling basis? I know there is no >> rollmin in the zoo library. >> >> Thanks, >> >> rcoder >> >> >> >> Achim Zeileis wrote: >> > >> > On Thu, 24 Jul 2008, rcoder wrote: >> > >> >> Hi everyone, >> >> >> >> I want to calculate a min and max (i.e. range) on a rolling time frame >> of >> >> 50 >> >> periods for a matrix with the number of periods extending along the >> row >> >> direction. So for 300 periods, there will be 6 time frame windows per >> >> column, and 6 min max pairs. I then want to o/p these pairs to a >> separate >> >> matrix. >> >> >> >> On a separate matter, I have a matrix containing data on which perform >> a >> >> regresssion over a rolling time period. Is there a convenient way I >> can >> >> do >> >> this for each column, and then save the gradient to an o/p matrix? >> > >> > Look at the package "zoo", specifically the examples on the manual >> pages >> > of ?rollapply and ?aggregate.zoo. These should be helpful in doing what >> > you want. The package vignettes have further worked examples. >> > >> > hth, >> > Z >> > >> >> Thanks, >> >> >> >> rcoder >> >> -- >> >> View this message in context: >> >> >> http://www.nabble.com/Rolling-range-and-regression-calculations-tp18643465p18643465.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-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. >> > >> > >> >> -- >> View this message in context: >> http://www.nabble.com/Rolling-range-and-regression-calculations-tp18643465p18650915.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. >> > > > > -- > Let's not spend our time and resources thinking about things that are so > little or so large that all they really do for us is puff us up and make > us > feel like gods. We are mammals, and have not exhausted the annoying little > problems of being mammals. > > -K. Mullis > > [[alternative HTML version deleted]] > > __ > 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. > > -- View this message in context: http://www.nabble.com/Rolling-range-and-regression-calculations-tp18643465p18655895.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] product of successive rows
Hi everyone, I want to perform an operation on a matrx that outputs the product of successive pairs of rows. For example: calculating the product between rows 1 & 2; 3 & 4; 5 & 6...etc. Does anyone know of any readily available functions that can do this? Thanks, rcoder -- View this message in context: http://www.nabble.com/product-of-successive-rows-tp18681259p18681259.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] Rolling regression - o/p selected coefficients
Hi everyone, Using the rolling regression function - rollingRegression(formula, data, width, ...) - is there a way to output only selected coefficients to a results matrix. For e.g. if I only wanted the slope coefficients to be recorded, how would I go about doing this? Thanks, Michael -- View this message in context: http://www.nabble.com/Rolling-regression---o-p-selected-coefficients-tp18681338p18681338.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] rollapply() opertation on ts matrix
Hi everyone, Is there a way to perform a rollapply operation on a time series data matrix and preserve the time frame? Currently, when I apply rollapply in its standart form, the date column is no longer present in the o/p matrix. Thanks, rcoder -- View this message in context: http://www.nabble.com/rollapply%28%29-opertation-on-ts-matrix-tp18694735p18694735.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] rollapply() opertation on time series
rcoder wrote: > > Hi everyone, > > Is there a way to perform a rollapply operation on a time series data > matrix and preserve the time frame? Currently, when I apply rollapply in > its standart form, the date column is no longer present in the o/p matrix. > > Thanks, > > rcoder > -- View this message in context: http://www.nabble.com/rollapply%28%29-opertation-on-ts-matrix-tp18694735p18699707.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] correlation between matrices - both with some NAs
Hi everyone, I'm having trouble applying the Cor() function to two matrices, both of which contain NAs. I am doing the following: a<-cor(m1, m2, use="complete.obs") ... and I get the following error message: Error in cor(m1, m2, use = "complete.obs") : no complete element pairs Does anyone know how I can apply a correlation, ignoring any NAs? Thanks, rcoder -- View this message in context: http://www.nabble.com/correlation-between-matrices---both-with-some-NAs-tp18721853p18721853.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] rollapply() opertation on time series
Hi Gabor, Thanks for your reply. Assuming I have a time series that is ready made (i.e. not constructed it using a zoo function) will the procedure below still retain the dates in the matrix? Thanks, rcoder quote author="Gabor Grothendieck"> rollapply along an index: library(zoo) z <- zoo(matrix(101:110, 5), 201:205) tt <- time(z) zz <- zoo(seq_along(tt), tt) out <- rollapply(zz, 3, function(ix) list(z[ix,])) str(out) # list of zoo objects On Mon, Jul 28, 2008 at 5:03 PM, rcoder <[EMAIL PROTECTED]> wrote: > > > rcoder wrote: >> >> Hi everyone, >> >> Is there a way to perform a rollapply operation on a time series data >> matrix and preserve the time frame? Currently, when I apply rollapply in >> its standart form, the date column is no longer present in the o/p >> matrix. >> >> Thanks, >> >> rcoder >> > > -- > View this message in context: > http://www.nabble.com/rollapply%28%29-opertation-on-ts-matrix-tp18694735p18699707.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-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. -- View this message in context: http://www.nabble.com/rollapply%28%29-opertation-on-ts-matrix-tp18694735p18721849.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] rolling regression between adjacent columns
Hi everyone, I am trying to apply linear regression to adjacent columns in a matrix (i.e. col1~col2; col3~col4; etc.). The columns in my matrix come with identifiers at the top of each column, but when I try to use these identifiers to reference the columns in the regression function using rollapply(), the columns are not recognised and the regression breaks down. Is there a more robust way to reference the columns I need, so that I can apply the regression across the matrix; 'by.column', but every other column? Thanks, rcoder -- View this message in context: http://www.nabble.com/rolling-regression-between-adjacent-columns-tp18722392p18722392.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] Re gression between columns in matrix - with some/full NAs
Hi Anna, Thanks for your reply and suggestions. I'm trying something different, based on regression this time, albeit giving similar problems. I want to regress a single column in a matrix with each other column in the same matrix, and o/p the intercept and slope coefficients to a results matrix. The loop below works, even when I have columns with partial NAs, but exits with an error when a column with only NAs is encountered. So, the 'na.action=NULL' statement (see last line of code below) doesn't seem to work in this case, or perhaps I am applying it incorrectly. I would be very grateful for any pointers in the right direction: >tt<-time(SourceMat) >ResultMat<-matrix(NA, ncol=colnum, nrow=rownum) #creates an o/p template matrix #loop through each column in the source matrix: >for (i in 1:5000) { sel_col<-[col(SourceMat)==i]#selecting the correct column in the matrix in turn SourceMat[,i]<-coef(lm(tt~sel_col), na.action=NULL) } Thanks, rcoder rcoder wrote: > > Hi everyone, > > I'm having trouble applying the Cor() function to two matrices, both of > which contain NAs. I am doing the following: > > a<-cor(m1, m2, use="complete.obs") > > ... and I get the following error message: > > Error in cor(m1, m2, use = "complete.obs") : > no complete element pairs > > Does anyone know how I can apply a correlation, ignoring any NAs? > > Thanks, > > rcoder > -- View this message in context: http://www.nabble.com/correlation-between-matrices---both-with-some-NAs-tp18721853p18739148.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] rolling regression between adjacent columns
Well, in this case I don't think my original code would have helped much... So, I've rewritten as below. I want to perform regression between one column in a matrix and all other columns in the same matrix. I have a for loop to achieve this, which succeeds in exporting intercept and slope coefficients to a results matrix, except when a column that contains only NAs is reached. Columns partially filled with NAs are handled, but the code exits with errors when a single column is filled with NAs. I inserted the 'na.action=NULL' statement within the lm() construct, but to no avail. I would be very grateful for any advice. >tt<-time(SourceMat) >ResultMat<-matrix(NA, ncol=colnum, nrow=rownum) #creates an o/p template matrix #loop through each column in the source matrix: >for (i in 1:5000) { sel_col<-[col(SourceMat)==i] #selecting the correct column in the matrix in turn SourceMat[,i]<-coef(lm(tt~sel_col), na.action=NULL) } Thanks, rcoder Gabor Grothendieck wrote: > > Read the last line of every message to r-help. > > On Tue, Jul 29, 2008 at 6:15 PM, rcoder <[EMAIL PROTECTED]> wrote: >> >> Hi everyone, >> >> I am trying to apply linear regression to adjacent columns in a matrix >> (i.e. >> col1~col2; col3~col4; etc.). The columns in my matrix come with >> identifiers >> at the top of each column, but when I try to use these identifiers to >> reference the columns in the regression function using rollapply(), the >> columns are not recognised and the regression breaks down. Is there a >> more >> robust way to reference the columns I need, so that I can apply the >> regression across the matrix; 'by.column', but every other column? >> >> Thanks, >> >> rcoder >> -- >> View this message in context: >> http://www.nabble.com/rolling-regression-between-adjacent-columns-tp18722392p18722392.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-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. > > -- View this message in context: http://www.nabble.com/rolling-regression-between-adjacent-columns-tp18722392p18739292.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] Re gression between adjacent columns - error with NAs
Hi Gabor, Thanks for your reply. I've written something that can be copied and pasted into your monitor to reproduce the error I am experiencing. Once the loop experiences a column full of NAs in SourceMat (column 3), it exits with errors, and ResultMat is only partially complete (up to column 2) with o/p intercept and slope results. When I include the 'na.action=NULL' statement, I get the following statement: Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : NA/NaN/Inf in foreign function call (arg 1) When I leave this statement out, I get the following: Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases In either case, ResultMat is only filled up to column 2: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 5.3611056 5.4099400 NA NA NA NA NA NA NANA [2,] -0.8028985 -0.4078084 NA NA NA NA NA NA NANA ##Code start SourceMat<-matrix(data=rnorm(100), ncol=10, nrow=10) SourceMat[,3]<-c(NA) tt<-time(SourceMat) rownum=2 colnum=10 ResultMat<-matrix(NA, ncol=colnum, nrow=rownum) #loop through each column in the source matrix: for (i in 1:10) { sel_col<-SourceMat[col(SourceMat)==i] #selecting the correct column in the matrix in turn ResultMat[,i]<-coef(lm(tt~sel_col, na.action=NULL)) } ##Code end I would be grateful for any suggestions to avoid this problem. Thanks, rcoder rcoder wrote: > > Well, in this case I don't think my original code would have helped > much... > > So, I've rewritten as below. I want to perform regression between one > column in a matrix and all other columns in the same matrix. I have a for > loop to achieve this, which succeeds in exporting intercept and slope > coefficients to a results matrix, except when a column that contains only > NAs is reached. Columns partially filled with NAs are handled, but the > code exits with errors when a single column is filled with NAs. I inserted > the 'na.action=NULL' statement within the lm() construct, but to no avail. > I would be very grateful for any advice. > >>tt<-time(SourceMat) >>ResultMat<-matrix(NA, ncol=colnum, nrow=rownum) #creates an o/p template matrix > > #loop through each column in the source matrix: >>for (i in 1:5000) > { > sel_col<-[col(SourceMat)==i] #selecting the correct column in the > matrix in turn > SourceMat[,i]<-coef(lm(tt~sel_col), na.action=NULL) > } > > Thanks, > > rcoder > > > Gabor Grothendieck wrote: >> >> Read the last line of every message to r-help. >> >> On Tue, Jul 29, 2008 at 6:15 PM, rcoder <[EMAIL PROTECTED]> wrote: >>> >>> Hi everyone, >>> >>> I am trying to apply linear regression to adjacent columns in a matrix >>> (i.e. >>> col1~col2; col3~col4; etc.). The columns in my matrix come with >>> identifiers >>> at the top of each column, but when I try to use these identifiers to >>> reference the columns in the regression function using rollapply(), the >>> columns are not recognised and the regression breaks down. Is there a >>> more >>> robust way to reference the columns I need, so that I can apply the >>> regression across the matrix; 'by.column', but every other column? >>> >>> Thanks, >>> >>> rcoder >>> -- >>> View this message in context: >>> http://www.nabble.com/rolling-regression-between-adjacent-columns-tp18722392p18722392.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-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. >> >> > > -- View this message in context: http://www.nabble.com/rolling-regression-between-adjacent-columns-tp18722392p18743632.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] Re gression between adjacent columns - error with NAs
Thank you both very much for your assistance. Both the suggestions worked out in the end, and I managed to achieve what I wanted. There is something else I want to try, which is a slight deviation on the theme. In the code I posted, I export Intercept and Slope regression coefficients to an o/p matrix. If I only want to o/p one of these, say Slope, is there an argument that I can add to the lm() statement? I know with Summary() addind '-1' can remove the Intercept coefficient from the o/p results, but I couldn't see any way of doing something similar with lm() in the help files and examples. Any suggestions would be grately appreciated. Many thanks, rcoder Gabor Grothendieck wrote: > > That's good. Try this: > > 1. put set.seed(1) at the top of the code to make it reproducible. > > 2. replace body of loop with: >sel_col<-SourceMat[, i] >out <- try(coef(lm(tt~sel_col, na.action=NULL))) >if (!inherits(out, "try-error")) ResultMat[,i] <- out > > 3. email tends to wrap long lines so try not putting > comments at the end of the line. Put them on a separate > line by themselves. > > You will still get the error messages but it won't > stop at them and will run to completion. > > > On Wed, Jul 30, 2008 at 5:54 PM, rcoder <[EMAIL PROTECTED]> wrote: >> >> Hi Gabor, >> >> Thanks for your reply. I've written something that can be copied and >> pasted >> into your monitor to reproduce the error I am experiencing. Once the loop >> experiences a column full of NAs in SourceMat (column 3), it exits with >> errors, and ResultMat is only partially complete (up to column 2) with >> o/p >> intercept and slope results. >> >> When I include the 'na.action=NULL' statement, I get the following >> statement: >> Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : >> NA/NaN/Inf in foreign function call (arg 1) >> >> When I leave this statement out, I get the following: >> Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : >> 0 (non-NA) cases >> >> In either case, ResultMat is only filled up to column 2: >> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] >> [,10] >> [1,] 5.3611056 5.4099400 NA NA NA NA NA NA NANA >> [2,] -0.8028985 -0.4078084 NA NA NA NA NA NA NANA >> >> >> ##Code start >> SourceMat<-matrix(data=rnorm(100), ncol=10, nrow=10) >> SourceMat[,3]<-c(NA) >> tt<-time(SourceMat) >> rownum=2 >> colnum=10 >> ResultMat<-matrix(NA, ncol=colnum, nrow=rownum) >> #loop through each column in the source matrix: >> for (i in 1:10) >>{ >>sel_col<-SourceMat[col(SourceMat)==i] #selecting the correct >> column >> in the matrix in turn >>ResultMat[,i]<-coef(lm(tt~sel_col, na.action=NULL)) >>} >> ##Code end >> >> I would be grateful for any suggestions to avoid this problem. >> >> Thanks, >> >> rcoder >> >> >> rcoder wrote: >>> >>> Well, in this case I don't think my original code would have helped >>> much... >>> >>> So, I've rewritten as below. I want to perform regression between one >>> column in a matrix and all other columns in the same matrix. I have a >>> for >>> loop to achieve this, which succeeds in exporting intercept and slope >>> coefficients to a results matrix, except when a column that contains >>> only >>> NAs is reached. Columns partially filled with NAs are handled, but the >>> code exits with errors when a single column is filled with NAs. I >>> inserted >>> the 'na.action=NULL' statement within the lm() construct, but to no >>> avail. >>> I would be very grateful for any advice. >>> >>>>tt<-time(SourceMat) >>>>ResultMat<-matrix(NA, ncol=colnum, nrow=rownum) #creates an o/p >> template matrix >>> >>> #loop through each column in the source matrix: >>>>for (i in 1:5000) >>> { >>> sel_col<-[col(SourceMat)==i] #selecting the correct column in >>> the >>> matrix in turn >>> SourceMat[,i]<-coef(lm(tt~sel_col), na.action=NULL) >>> } >>> >>> Thanks, >>> >>> rcoder >>> >>> >>> Gabor Grothendieck wrote: >>>> >>>> Read the last line of every message to r-he
[R] rollapply() to portions of a matrix
Hi everyone, I have a rollapply statement that applies a function, in steps, over a data matrix as follows: #Code start testm<-rollapply(mat, 100, by=100, min, na.rm=F) #Code end This moves down matrix 'mat' and calculates the minimum value over a 100 row range, every 100 rows (i.e. no overlaps). NAs are not removed. I want to modify this statement somehow so that the rollapply() stops at a certain row in the matrix: i.e. it applies from the top, as instructed by the statement, but only to a certain row in the matrix. Can anyone suggest a way to do this using the additional arguments in the parentheses? One way would be to introduce an if statement, but is this the only way? Thanks, rcoder -- View this message in context: http://www.nabble.com/rollapply%28%29-to-portions-of-a-matrix-tp18761906p18761906.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] losing row.names in matrix operations
Hi everyone, I have a data frame, with the following format: MatDate-> row.names ID1 ID2 ID3 1 date1 2 date1 3 date3 etc but I cannot perform a rollapply() statement on the matrix without converting the matrix into a time series. i.e. MatTs<-ts(MatDate) Only then will my rollapply statement work: MatMin<-rollapply(MatTs, 2,by=2, min, na.rm=F) If I apply the rollapply() statement to the dataframe, I get the following error: Error: could not find function "rollapply" The problem is that when I convert the data.frame matrix into a time series matrix, I lose the dates in the row.names column. I just want to know if anyone could suggest a way to get around this problem, i.e. keep the row.names column in place, and use the rollapply() statement as above. Thanks, rcoder -- View this message in context: http://www.nabble.com/losing-row.names-in-matrix-operations-tp18788509p18788509.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] rollingRegression() aplied between vector and matrix
Hi everyone, I'm trying to use the rollingRegression() function to apply regression over a rolling 'window' to some tine series data. Currently, I have: #Code start vec<-c(1:100) mat<-matrix(abs(rnorm(1000)), nrow=100,ncol=100) mat[,5]<-c(NA) if(!all(is.na(mat))) {RegData<-rollingRegression(vec~mat,10)} #Code end I get the following error message: Error in eval(predvars, data, env) : not that many frames on the stack I know the arguments in the rollingRegression() function are: (formula, data, window), so I then combined the vector and matrix into a single data frame as follows: #Code start vec<-c(1:100) mat<-matrix(abs(rnorm(1000)), nrow=100,ncol=100) mat[,5]<-c(NA) comb<-data.frame(cbind(vec,mat)) if(!all(is.na(comb))) {RegData<-rollingRegression(vec~mat,comb,10)} #Code end ...I get the following error message: Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases Can anyone see what I am doing incorrectly? I hope the example code helps. Thanks, rcoder -- View this message in context: http://www.nabble.com/rollingRegression%28%29-aplied-between-vector-and-matrix-tp18788824p18788824.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] problem with nested loop for regression
Hi everyone, I'm experiencing difficulty getting the results I want when I use a nested for loop. I have a data set to which I perform some calculations, and then try to apply a regression over a rolling window. The code runs, but the regression results I am getting (intercept and slope) are simply the same, repeated again and again in the results matrix. The regression does not seem to be obeying the instructions of the nested loop, which intend it to calculate regression coefficients over a data one row at a time. I have been struggling with the code for many days now, testing various parts, and I cannot seem to get the nested loop to work as I want it to. I would be very grateful for any suggestions. Below is a brief version of my code: #Code start library(zoo) seed.set(1) Pmat<-matrix(rnorm(1000), nrow=100, ncol=100) maxcol<-ncol(Pmat) maxrow<-nrow(Pmat) startrow<-10 period<-10 wind<-2 #roll window subdiv<-period/wind rollstart<-11 #start roll at period+1 #converting Pmat into ts for rollapply() to work... PmatTS<-ts(Pmat) Preg<-matrix(NA,ncol=maxcol,nrow=2*maxrow) PmatWt<-matrix(NA, nrow=subdiv,ncol=maxcol) mult_col<-matrix(1:5, nrow=5, ncol=1) #rolling calculations... for (i in (rollstart):maxrow) #test loop - works { #extract the relevant timeframe... dslice<-PmatTS[(i-period):i,] #slicing past 100 days dslicets<-ts(dslice) #operating on sliced data... Pmin<-rollapply(dslicets, wind, by=wind, min, na.rm=F) #getting min data at per quintile Pmax<-rollapply(dslicets, wind, by=wind, max, na.rm=F) Pmult<-Pmin*Pmax#calculating product tt<-time(Pmult) for (j in 1:5) #1st nested loop { PmatWt[j,]<-Pmult[j,]*mult_col[j,] } #rolling regression analysis... for (k in 1:maxcol) #2nd nested loop { sel_col<-PmatWt[,k] if(!all(is.na(sel_col))) {Preg[,k]<-coef(lm(tt~sel_col))} } } #Code End Thanks, rcoder -- View this message in context: http://www.nabble.com/problem-with-nested-loop-for-regression-tp18792841p18792841.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] losing row.names in matrix operations
Thanks for your reply Gabor. As I already have a dates column in my dataframe (in column row.names), is it possible to preserve this whilst still making a data set suitable for rollapply()? Thanks, rcoder Gabor Grothendieck wrote: > > If its regular you can convert it to ts or zoo. > If its irregular convert it to zoo. There is no > reason to expect rollapply to work with objects > of other classes. Read ?ts and ?zoo. In > ts note the start and frequency arguments. > > > On Sat, Aug 2, 2008 at 7:50 AM, rcoder <[EMAIL PROTECTED]> wrote: >> >> Hi everyone, >> >> I have a data frame, with the following format: >> >> MatDate-> >>row.names ID1 ID2 ID3 >> 1 date1 >> 2 date1 >> 3 date3 >>etc >> >> but I cannot perform a rollapply() statement on the matrix without >> converting the matrix into a time series. >> i.e. MatTs<-ts(MatDate) > > Use the start and frequency arguments. See ?ts > >> >> Only then will my rollapply statement work: >> MatMin<-rollapply(MatTs, 2,by=2, min, na.rm=F) >> >> If I apply the rollapply() statement to the dataframe, I get the >> following >> error: Error: could not find function "rollapply" >> >> The problem is that when I convert the data.frame matrix into a time >> series >> matrix, I lose the dates in the row.names column. I just want to know if >> anyone could suggest a way to get around this problem, i.e. keep the >> row.names column in place, and use the rollapply() statement as above. >> >> Thanks, >> >> rcoder >> -- >> View this message in context: >> http://www.nabble.com/losing-row.names-in-matrix-operations-tp18788509p18788509.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-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. > > -- View this message in context: http://www.nabble.com/losing-row.names-in-matrix-operations-tp18788509p18789688.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] problem with nested loop for regression
Hi, I guess my question is really more about the nested for loop construct and whether it is doing what I intend it to do in my code in the previous post. I would be grateful if anyone who has used nested loops could let me know if I am doing something wrong. Thanks, rcoder rcoder wrote: > > Hi everyone, > > I'm experiencing difficulty getting the results I want when I use a nested > for loop. I have a data set to which I perform some calculations, and then > try to apply a regression over a rolling window. The code runs, but the > regression results I am getting (intercept and slope) are simply the same, > repeated again and again in the results matrix. The regression does not > seem to be obeying the instructions of the nested loop, which intend it to > calculate regression coefficients over a data one row at a time. I have > been struggling with the code for many days now, testing various parts, > and I cannot seem to get the nested loop to work as I want it to. I would > be very grateful for any suggestions. Below is a brief version of my code: > > #Code start > library(zoo) > seed.set(1) > Pmat<-matrix(rnorm(1000), nrow=100, ncol=100) > maxcol<-ncol(Pmat) > maxrow<-nrow(Pmat) > startrow<-10 > period<-10 > wind<-2 #roll window > subdiv<-period/wind > rollstart<-11 #start roll at period+1 > #converting Pmat into ts for rollapply() to work... > PmatTS<-ts(Pmat) > Preg<-matrix(NA,ncol=maxcol,nrow=2*maxrow) > PmatWt<-matrix(NA, nrow=subdiv,ncol=maxcol) > mult_col<-matrix(1:5, nrow=5, ncol=1) > #rolling calculations... > for (i in (rollstart):maxrow) > { > #extract the relevant timeframe... > dslice<-PmatTS[(i-period):i,] > dslicets<-ts(dslice) > #operating on sliced data... > Pmin<-rollapply(dslicets, wind, by=wind, min, na.rm=F) > Pmax<-rollapply(dslicets, wind, by=wind, max, na.rm=F) > Pmult<-Pmin*Pmax#calculating product > tt<-time(Pmult) > for (j in 1:5)#1st nested loop > { > PmatWt[j,]<-Pmult[j,]*mult_col[j,] > } > #rolling regression analysis... > for (k in 1:maxcol) #2nd nested loop > { > sel_col<-PmatWt[,k] > if(!all(is.na(sel_col))) {Preg[,k]<-coef(lm(tt~sel_col))} > } > } > #Code End > > Thanks, > > rcoder > -- View this message in context: http://www.nabble.com/problem-with-nested-loop-for-regression-tp18792841p18815273.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] problem with nested loop for regression
Hi Jim, Thanks very much for your reply and suggestions. Although the statement Preg[,k]<-coef(lm(tt~sel_col)) is applying lm() between the same vectors, the contents of one of the vectors (sel_col) should be changing with each cycle through the outer loop. My mistake is that I didn't specify information on the rows the data should go into. I have modified the statement to resemble something like this: pEUUreg[j:(j+1),k]<-coef(lm(tt~sel_col)), where j+1 ensures a jump of 2 rows, as slope and intercept are arranged vertically in the o/p matrix. I tried it on a section of one column (to save time) and it seemed to o/p some (varying) data to Preg. I now want to just extract the slope data into a new matrix, so I have added a statement for this. I was just wondering if there is a neater way to tell the lm() function to o/p slope data only? Perhaps one way would be to use [j,k] vs [j:(j+1),k) in the regression statement - forcing successive slope data to overwrite cell locations containing intercept data. Also, the full code takes an awfully long time to run. Would anyone be able to suggest a way for mee to speed it up - perhaps in the regression algorithm? Thanks, rcoder jholtman wrote: > > Actually now that I read it closer, I see what your problem is. what > did you think the statement: > > Preg[,k]<-coef(lm(tt~sel_col)) > > was going to do? Preg is a 200x100 matrix and you are only storing > two values (the coefficients) so they will be repeated 100 times in > the column. So there is nothing wrong with your nested "for" loops; > it is in the algorithm that you are using. You might want to use the > browser and you would see something like this: > > Browse[1]> str(sel_col) > num [1:5] -0.115 -2.666 -0.811 1.440 -0.879 > Browse[1]> str(tt) > Time-Series [1:5] from 2 to 10: 2 4 6 8 10 > Browse[1]> n > debug: if (!all(is.na(sel_col))) { > Preg[, k] <- coef(lm(tt ~ sel_col)) > } > Browse[1]> n > debug: Preg[, k] <- coef(lm(tt ~ sel_col)) > Browse[1]> k > [1] 1 > Browse[1]> n > debug: k > Browse[1]> n > debug: sel_col <- PmatWt[, k] > Browse[1]> k > [1] 2 > Browse[1]> str(sel_col) > num [1:5] -0.115 -2.666 -0.811 1.440 -0.879 > Browse[1]> str(Preg) > num [1:200, 1:100] 6.356 0.587 6.356 0.587 6.356 ... > Browse[1]> str(PmatWt) > num [1:5, 1:100] -0.115 -2.666 -0.811 1.440 -0.879 ... > Browse[1]> coef(lm(tt~sel_col)) > (Intercept) sel_col >6.3557600.586699 > Browse[1]> str(Preg) > num [1:200, 1:100] 6.356 0.587 6.356 0.587 6.356 ... > Browse[1]> > > This would help you to understand what is happening. > > On Mon, Aug 4, 2008 at 8:35 PM, jim holtman <[EMAIL PROTECTED]> wrote: >> Exactly what problem are you having? There is nothing wrong with >> nested for loops, so what is leading you to believe you have a >> problem? I ran your program and it seems to terminate. Most of the >> time seems to have been spent in the following statement: >> >> if(!all(is.na(sel_col))) >> {Preg[,k]<-coef(lm(tt~sel_col))} >> >> Given that you appear to be executing this statement about 9000 times, >> it is not surprising. So can you provide more details? >> >> 0 51.2 root >> 1. 44.5 coef >> 2. . 44.1 lm >> 3. . . 15.7 eval >> 4. . . . 15.6 eval >> 5. . . . | 15.6 model.frame >> 6. . . . | . 15.3 model.frame.default >> 7. . . . | . .6.0 sapply >> 8. . . . | . . .3.8 lapply >> 9. . . . | . . . .3.0 FUN >> 10. . . . | . . . . |1.1 %in% >> 11. . . . | . . . . | .1.0 match >> 12. . . . | . . . . | . .0.8 is.factor >> 13. . . . | . . . . | . . .0.7 inherits >> 10. . . . | . . . . |1.0 .deparseOpts >> 11. . . . | . . . . | .0.3 pmatch >> 9. . . . | . . . .0.5 as.list >> 8. . . . | . . .1.7 unique >> 9. . . . | . . . .0.7 unique.default >> 9. . . . | . . . .0.6 unlist >> 10. . . . | . . . . |0.4 lapply >> 8. . . . | . . .0.3 unlist >> 7. . . . | . .4.0 na.omit >> 8. . . . | . . .3.8 na.omit.data.frame >> 9. . . . | . . . .3.0 [ >> 10. . . . | . . . . |2.9 [.data.frame >> 11. . . . | . . . . | .0.6 duplicated >> 12. . . . | . . . . | . .0.3 duplicated.default >> 11. . . . | . . . . | .0.3 [ >> 12. . . . | . . . . | . . 0.2 [.ts >> 11. . . . | . . . . | .0.3 [[ >> >> On Mon, Aug 4, 2008 at 12:58 PM, rcoder <[EMAIL PROTECTED]> wrote: >>> >>> Hi, >>> >>> I guess my question is really more about the nested for loop construct &g
[R] using acf() for multiple columns
Hi everyone, I'm trying to use the acf() function to calculate the autocorrelation of each column in a matrix. The trouble is that I can only seem to get the function to work if I extract the data in the column into a separate matrix and then apply the acf() function to this column. I have something like this: acf(mat,lag.max=10,na.action=na.pass) ...but I would really like to apply the function to 'mat' where 'mat' is a matrix as opposed to a vector. The function actually doesn't return an acf coefficient, but instead plots the data. So, in addition to handling matrices with multiple columns, would anyone know how to coerce the function to output the underlying data? Finally, when working with a matrix, is there a way I can specify how many plots I can display after the function executes? I managed to generate a multiple plot when I was experimenting, but the titles suggested the acf was calculated between adjacent columns in the matrix, which is something I was puzzled about. Thanks, rcoder -- View this message in context: http://www.nabble.com/using-acf%28%29-for-multiple-columns-tp18858672p18858672.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] using acf() for multiple columns
Hi Gary, Thanks for your reply. This works fine. Is there any way to send the ACF data to a matrix, so I could analyse the data in excel for e.g.? Thanks, rcoder Ling, Gary (Electronic Trading) wrote: > > Hi, here is one possible solution ... > -gary > > ### example ### > > # create a 500x3 multi-ts > A <- matrix(rnorm(1500),nrow=500) > > # misc. graphical setting > par(mfrow=c(ncol(A),1)) > > # then our friend -- lapply(split(...)) > lapply(split(A,col(A)), acf) > #Or > lapply(split(A,col(A)), function(ts) acf(ts, lag.max=10)) > > > > > > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > On Behalf Of rcoder > Sent: Wednesday, August 06, 2008 4:13 PM > To: r-help@r-project.org > Subject: [R] using acf() for multiple columns > > > > Hi everyone, > > I'm trying to use the acf() function to calculate the autocorrelation of > each column in a matrix. The trouble is that I can only seem to get the > function to work if I extract the data in the column into a separate > matrix > and then apply the acf() function to this column. > > I have something like this: acf(mat,lag.max=10,na.action=na.pass) > > ...but I would really like to apply the function to 'mat' where 'mat' is > a > matrix as opposed to a vector. The function actually doesn't return an > acf > coefficient, but instead plots the data. So, in addition to handling > matrices with multiple columns, would anyone know how to coerce the > function > to output the underlying data? > > Finally, when working with a matrix, is there a way I can specify how > many > plots I can display after the function executes? I managed to generate a > multiple plot when I was experimenting, but the titles suggested the acf > was > calculated between adjacent columns in the matrix, which is something I > was > puzzled about. > > Thanks, > > rcoder > -- > View this message in context: > http://www.nabble.com/using-acf%28%29-for-multiple-columns-tp18858672p18 > 858672.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. > > > This message w/attachments (message) may be privileged, confidential or > proprietary, and if you are not an intended recipient, please notify the > sender, do not use or share it and delete it. Unless specifically > indicated, this message is not an offer to sell or a solicitation of any > investment products or other financial product or service, an official > confirmation of any transaction, or an official statement of Merrill > Lynch. Subject to applicable law, Merrill Lynch may monitor, review and > retain e-communications (EC) traveling through its networks/systems. The > laws of the country of each sender/recipient may impact the handling of > EC, and EC may be archived, supervised and produced in countries other > than the country in which you are located. This message cannot be > guaranteed to be secure or error-free. This message is subject to terms > available at the following link: > http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch > you consent to the foregoing. > > __ > 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. > > -- View this message in context: http://www.nabble.com/using-acf%28%29-for-multiple-columns-tp18858672p18865946.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] long run time for loop operation & matrix fill
Hi everyone, I'm running some code containing an outer and inner loop, to fill cells in a 2500x1500 results matrix. I left my program running overnight, and it was still running when I checked 17 hours later. I have tested the operation on a smaller matrix and it executes fine, so I believe there is nothing wrong with the code. I was just wondering if this is normal program execution speed for such an operation on a P4 with 2GB RAM? Thanks, rcoder -- View this message in context: http://www.nabble.com/long-run-time-for-loop-operation---matrix-fill-tp18876372p18876372.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] increment size in for loop
Hi everyone, Is there a way to vary the increment size in a for loop? For e.g. when incrementing in steps greater than unity. Thanks, rcoder -- View this message in context: http://www.nabble.com/increment-size-in-for-loop-tp18893893p18893893.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] increment size in for loop
Don't worry, I got it: for(x in seq(1,100,5)) { print(x) } where the step size is 5. rcoder rcoder wrote: > > Hi everyone, > > Is there a way to vary the increment size in a for loop? For e.g. when > incrementing in steps greater than unity. > > Thanks, > > rcoder > > > -- View this message in context: http://www.nabble.com/increment-size-in-for-loop-tp18893893p18894006.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] ignoring zeros or converting to NA
Hi everyone, I have a matrix that has a combination of zeros and NAs. When I perform certain calculations on the matrix, the zeros generate "Inf" values. Is there a way to either convert the zeros in the matrix to NAs, or only perform the calculations if not zero (i.e. like using something similar to an !all(is.na() construct)? Thanks, rcoder -- View this message in context: http://www.nabble.com/ignoring-zeros-or-converting-to-NA-tp18948979p18948979.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] merging data sets to match data to date
Hi everyone, I want to extract data from a data set according to dates specified in a vector. I have created a blank matrix with row names (dates) that I want to extract from the full data set. I have then performed a merge to try to o/p rows corresponding to common dates to a results matrix, but the operation did not fill the results matrix. Coulc anyone offer any advice to assist with this operation? Thanks, rcoder -- View this message in context: http://www.nabble.com/merging-data-sets-to-match-data-to-date-tp18962197p18962197.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] conditional IF with AND
Hi everyone, I'm trying to create an "if" conditional statement with two conditions, whereby the statement is true when condition 1 AND condition 2 are met: code structure: if ?AND? (a[x,y] , a[x,y] ) I've trawled through the help files, but I cannot find an example of the syntax for incorporating an AND in a conditional IF statement. Thanks, rcoder -- View this message in context: http://www.nabble.com/conditional-IF-with-AND-tp18966890p18966890.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] merging data sets to match data to date
Dear Henrique, This is exactly what I need. Thank you very much for your help! rcoder Henrique Dallazuanna wrote: > > Try this: > > x <- data.frame(Dates = seq(as.Date('2008-01-01'), > as.Date('2008-01-31'), by = > 'days'), >Values = sample(31)) > > subset(x, Dates %in% as.Date(c('2008-01-05', '2008-01-20'))) > > On 8/13/08, rcoder <[EMAIL PROTECTED]> wrote: >> >> Hi everyone, >> >> I want to extract data from a data set according to dates specified in a >> vector. I have created a blank matrix with row names (dates) that I want >> to >> extract from the full data set. I have then performed a merge to try to >> o/p >> rows corresponding to common dates to a results matrix, but the operation >> did not fill the results matrix. Coulc anyone offer any advice to assist >> with this operation? >> >> Thanks, >> >> rcoder >> -- >> View this message in context: >> http://www.nabble.com/merging-data-sets-to-match-data-to-date-tp18962197p18962197.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. >> > > > -- > Henrique Dallazuanna > Curitiba-Paraná-Brasil > 25° 25' 40" S 49° 16' 22" O > > __ > 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. > > -- View this message in context: http://www.nabble.com/merging-data-sets-to-match-data-to-date-tp18962197p18969953.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] conditional IF with AND
Thank you all for your replies. This is all very useful information for me! Ted, thank you very much for the extra explanation and example. Many thanks, rcoder Ted.Harding-2 wrote: > > On 13-Aug-08 16:45:27, rcoder wrote: >> Hi everyone, >> I'm trying to create an "if" conditional statement with two conditions, >> whereby the statement is true when condition 1 AND condition 2 are met: >> >> code structure: >> if ?AND? (a[x,y] , a[x,y] ) >> >> I've trawled through the help files, but I cannot find an example of >> the syntax for incorporating an AND in a conditional IF statement. >> >> Thanks, >> rcoder > > The basic structure of an 'if' statement (from ?"if" -- don't > forget the ".." for certain keywords such as "if") is: > > if(cond) expr > > What is not explained in the ?"if" help is that 'cond' may > be any expression that evaluates to a logical TRUE or FALSE. > > Hence you can build 'cond' to suit your purpose. Therefore: > > if( ()&() ) { > > } > > Example: > > if( (a[x,y]>1.0)&(a[x,y]<2.0) ){ > print("Between 1 and 2") > } > > Hoping this helps, > Ted. > > > E-Mail: (Ted Harding) <[EMAIL PROTECTED]> > Fax-to-email: +44 (0)870 094 0861 > Date: 13-Aug-08 Time: 19:33:53 > -- XFMail -- > > __ > 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. > > -- View this message in context: http://www.nabble.com/conditional-IF-with-AND-tp18966890p18970101.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] ignoring zeros or converting to NA
Thank you for all your replies. Borrowing an alternative solution kindly provided by Mark Leeds, I am using a conditional statement to pass non-zero values to a holding matrix that has cells initially set to NA. The code is as follows: ##Code Start mat_zeroless<-matrix(NA,5000,2000) #generating holding matrix for (j in 1:5000) { for (k in 1:2000) { if(mat[j,k]!=0) {mat_zeroless[j,k]<-mat[j,k]} } } ##Code End Problems arise when the algorithm encounters NAs. Numbers are passed to the holding matrix, and zeros not, but when an NA is encountered, the following error is generated: Error in if mat[j,k] !=0 { :missing value where TRUE/FALSE needed I'm not sure how to resolve this. Thanks, rcoder Henrik Bengtsson (max 7Mb) wrote: > > FYI, > > there is an isZero() in the R.utils package that allows you to specify > the precision. It looks like this: > > isZero <- function (x, neps=1, eps=.Machine$double.eps, ...) { > (abs(x) < neps*eps); > } > > /Henrik > > On Wed, Aug 13, 2008 at 8:23 AM, Roland Rau <[EMAIL PROTECTED]> > wrote: >> Hi, >> >> since many suggestions are following the form of >> x[x==0] (or similar) >> I would like to ask if this is really recommended? >> What I have learned (the hard way) is that one should not test for >> equality >> of floating point numbers (which is the default for R's numeric values, >> right?) since the binary representation of these (decimal) floating point >> numbers is not necessarily exact (with the classic example of decimal >> 0.1). >> Is it okay in this case for the value zero where all binary elements are >> zero? Or does R somehow recognize that it is an integer? >> >> Just some questions out of curiosity. >> >> Thank you, >> Roland >> >> >> rcoder wrote: >>> >>> Hi everyone, >>> >>> I have a matrix that has a combination of zeros and NAs. When I perform >>> certain calculations on the matrix, the zeros generate "Inf" values. Is >>> there a way to either convert the zeros in the matrix to NAs, or only >>> perform the calculations if not zero (i.e. like using something similar >>> to >>> an !all(is.na() construct)? >>> >>> Thanks, >>> >>> rcoder >> >> __ >> 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-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. > > -- View this message in context: http://www.nabble.com/ignoring-zeros-or-converting-to-NA-tp18948979p18970797.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] missing TRUE/FALSE error in conditional construct
Hi everyone, I posted something similar to this in reply to another post, but there seems to be a problem getting it onto the board, so I'm starting a new post. I am trying to use conditional formatting to select non-zero and non-NaN values from a matrix and pass them into another matrix. The problem is that I keep encountering an error message indicating the ":missing value where TRUE/FALSE needed " My code is as follows: ##Code Start mat_zeroless<-matrix(NA,5000,2000) #generating holding matrix ##Assume source matrix containing combination of values, NaNs and zeros## for (j in 1:5000) { for (k in 1:2000) { if(mat[j,k]!=0 & !is.NaN(mat[j,k])) {mat_zeroless[j,k]<-mat[j,k]} } } ##Code End Error in if (mat[j,k] !=0 & !is.NaN(mat[j,k])) { :missing value where TRUE/FALSE needed I'm not sure how to resolve this. rcoder -- View this message in context: http://www.nabble.com/missing-TRUE-FALSE-error-in-conditional-construct-tp18972244p18972244.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] cor() btwn columns in two matrices - no complete element pairs
Is there any way to increment the loop when the error occurs, returning an NA in the results matrix. I have included an if starement to only calculate cor() when columns are !all(is.na()), but I still exit out with this error, albeit after some loops through the matrix - i.e. when the condition is satisfied, but the data rows do not overlap at all. rcoder wrote: > > Hi everyone, > > I'm trying to calculate correlation coefficients between corresponding > columns in two matrices with identical dimensions but different data. The > problem is that the matrices contain NAs in different locations. I am > using the following code to try to calculate correlations between complete > sets of data: > > #Code start > maxcol<-ncol(mat1) > for (i in 1:maxcol) > { > corr_results[1,i]<-cor(mat1[,i],mat2[,i], use="complete.obs") > } > #Code end > > ...but I get the following error message: > > Error in cor(mat1[,i], mat2[,i], use="complete.obs") : > no complete element pairs > > Is there something I'm not including in the 'cor' parentheses? I apologise > for not including the true original data frames. > > Thanks, > > rcoder > -- View this message in context: http://www.nabble.com/cor%28%29-btwn-columns-in-two-matrices---no-complete-element-pairs-tp18998875p18999356.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] cor() btwn columns in two matrices - no complete element pairs
Hi everyone, I'm trying to calculate correlation coefficients between corresponding columns in two matrices with identical dimensions but different data. The problem is that the matrices contain NAs in different locations. I am using the following code to try to calculate correlations between complete sets of data: #Code start maxcol<-ncol(mat1) for (i in 1:maxcol) { corr_results[1,i]<-cor(mat1[,i],mat2[,i], use="complete.obs") } #Code end ...but I get the following error message: Error in cor(mat1[,i], mat2[,i], use="complete.obs") : no complete element pairs Is there something I'm not including in the 'cor' parentheses? I apologise for not including the true original data frames. Thanks, rcoder -- View this message in context: http://www.nabble.com/cor%28%29-btwn-columns-in-two-matrices---no-complete-element-pairs-tp18998875p18998875.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] cor() btwn columns in two matrices - no complete element pairs
Hi Daniel, Thanks for your reply. I experimented with various options since my poet and eventually tried use="pairwise.complete.obs" which seemed to do the trick. Thanks, rcoder Daniel Malter wrote: > > Hi, > > the argument use should be: use="p" (for pairwise complete obs.) > > Daniel > > > > - > cuncta stricte discussurus > - > > -Ursprüngliche Nachricht- > Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im > Auftrag von rcoder > Gesendet: Friday, August 15, 2008 9:45 AM > An: r-help@r-project.org > Betreff: [R] cor() btwn columns in two matrices - no complete element > pairs > > > Hi everyone, > > I'm trying to calculate correlation coefficients between corresponding > columns in two matrices with identical dimensions but different data. The > problem is that the matrices contain NAs in different locations. I am > using > the following code to try to calculate correlations between complete sets > of > data: > > #Code start > maxcol<-ncol(mat1) > for (i in 1:maxcol) > { > corr_results[1,i]<-cor(mat1[,i],mat2[,i], use="complete.obs") > } > #Code end > > ...but I get the following error message: > > Error in cor(mat1[,i], mat2[,i], use="complete.obs") : > no complete element pairs > > Is there something I'm not including in the 'cor' parentheses? I apologise > for not including the true original data frames. > > Thanks, > > rcoder > -- > View this message in context: > http://www.nabble.com/cor%28%29-btwn-columns-in-two-matrices---no-complete-e > lement-pairs-tp18998875p18998875.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-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. > > -- View this message in context: http://www.nabble.com/cor%28%29-btwn-columns-in-two-matrices---no-complete-element-pairs-tp18998875p19014384.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] how to call a C-library in R
Hi Cindy, Take a look at the following. This should help get you started. http://www.stat.umn.edu/~charlie/rc/ rcoder cindy Guo wrote: > > Hi, everyone, > > I need to use a C library. But since I have little experience in C, I want > to call this C library in R and program the rest in R. Does anyone know > how > to do this? > > Thanks, > > Cindy > > [[alternative HTML version deleted]] > > __ > 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. > > -- View this message in context: http://www.nabble.com/how-to-call-a-C-library-in-R-tp19125202p19126668.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.