[R] Summing using a boolean index vector
Hello all: I have a dataframe f of weekdays and value, and a Boolean vector with Fridays set to true, and other days set to false, created by fridays-(diff(f$weekdays) -1). I would like to create a vector of sums, for each week. That is, start summing on the first false value in the vector, and when I get to true, produce the sum, and start summing again. Is there a vector operation which can do this, without writing an explicit loop? Thank you, Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 [[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.
[R] Summing using a boolean index vector (repost in plain text).
Hello all: I have a dataframe f of weekdays and value, and a Boolean vector with Fridays set to true, and other days set to false, created by fridays-(diff(f$weekdays) -1). I would like to create a vector of sums, for each week. That is, start summing on the first false value in the vector, and when I get to true, produce the sum, and start summing again. Is there a vector operation which can do this, without writing an explicit loop? Thank you, Dan Stanger P.S. Sorry about the repost. Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 __ 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] Summing using a boolean index vector (repost in plain text).
Hello Jim, Thank you for getting back to me. Cumsum does exactly what I needed as the following example shows. [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE cumsum(x) [1] 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 -Original Message- From: jim holtman [mailto:jholt...@gmail.com] Sent: Friday, June 18, 2010 12:24 PM To: Dan Stanger Cc: R-help@r-project.org Subject: Re: [R] Summing using a boolean index vector (repost in plain text). ?cumsum ?ave But without data (follow the posting guide) specific solution can not be specified On Fri, Jun 18, 2010 at 11:39 AM, Dan Stanger dstan...@eatonvance.com wrote: Hello all: I have a dataframe f of weekdays and value, and a Boolean vector with Fridays set to true, and other days set to false, created by fridays-(diff(f$weekdays) -1). I would like to create a vector of sums, for each week. That is, start summing on the first false value in the vector, and when I get to true, produce the sum, and start summing again. Is there a vector operation which can do this, without writing an explicit loop? Thank you, Dan Stanger P.S. Sorry about the repost. Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 __ 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 that 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.
[R] Cluster analysis question
Hello All, I have data where each feature data point is a vector, and my distance measurement is a weighted dot product between vectors. I would like to use R to perform a cluster analysis on this data. Does one of the R cluster analysis routines provide for a user provided distance function? Dan Stanger Eaton Vance Management 255 State Street Boston, MA 02109 617 598 8261 [[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.
[R] Newbie question about vector matrix multiplication
Hello All, I have a covariance matrix, generated by read.table, and cov: co-cov(read.table(c:/r.x)) XYZ X 0.0012517684 0.0002765438 0.0007887114 Y 0.0002765438 0.0002570286 0.0002117336 Z 0.0007887114 0.0002117336 0.0009168750 And a weight vector generated by w- read.table(c:/r.weights) X Y Z 1 0.5818416 0.2158531 0.2023053 I want to compute the product of the matrix and vectors termwise to generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j]. 0.000423773 7.47216E-08 4.41255E-08 7.47216E-08 1.96566E-11 4.29229E-11 4.41255E-08 4.29229E-11 4.11045E-11 Is this possible without writing explicit loops? Thank you, Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 [[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.
Re: [R] Newbie question about vector matrix multiplication
Hello Abhijit, When I try that, I get: diag(w)%*%co%*%diag(w) Error in diag(w) %*% co : requires numeric matrix/vector arguments Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 -Original Message- From: Abhijit Dasgupta [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 14, 2008 3:22 PM To: Dan Stanger Cc: r-help@r-project.org Subject: Re: [R] Newbie question about vector matrix multiplication Won't diag(w)%*%co%*%diag(w) do it? Dan Stanger wrote: Hello All, I have a covariance matrix, generated by read.table, and cov: co-cov(read.table(c:/r.x)) XYZ X 0.0012517684 0.0002765438 0.0007887114 Y 0.0002765438 0.0002570286 0.0002117336 Z 0.0007887114 0.0002117336 0.0009168750 And a weight vector generated by w- read.table(c:/r.weights) X Y Z 1 0.5818416 0.2158531 0.2023053 I want to compute the product of the matrix and vectors termwise to generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j]. 0.000423773 7.47216E-08 4.41255E-08 7.47216E-08 1.96566E-11 4.29229E-11 4.41255E-08 4.29229E-11 4.11045E-11 Is this possible without writing explicit loops? Thank you, Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 [[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. __ 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] Newbie question about vector matrix multiplication
Hi Phil, That solved the problem. Thanks, Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 -Original Message- From: Phil Spector [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 14, 2008 3:42 PM To: Dan Stanger Subject: RE: [R] Newbie question about vector matrix multiplication Dan - Most likely it's due to the fact that you used read.table for your weight vector, and it gave you a one row, three column data frame (as it should). Your weights are in the first row of that, so ww = as.numeric(w[1,]) will extract them as a vector, and outer(ww,ww,'*) * X should get what you want. - Phil On Wed, 14 May 2008, Dan Stanger wrote: Hi Phil, outer(w,w,*) Error in as.vector(X) %*% t(as.vector(Y)) : requires numeric matrix/vector arguments There must be something about the object I am creating when I read my weight vector in, which causes it to be treated improperly. My file is: \tX\tY\tZ 1\t0.581841567\t0.215853099\t0.202305334 where tabs have been replaced by \t for readability. Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 -Original Message- From: Phil Spector [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 14, 2008 3:27 PM To: Dan Stanger Subject: Re: [R] Newbie question about vector matrix multiplication Dan - One possible way to do what you describe is outer(w,w,*) * co - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley [EMAIL PROTECTED] On Wed, 14 May 2008, Dan Stanger wrote: Hello All, I have a covariance matrix, generated by read.table, and cov: co-cov(read.table(c:/r.x)) XYZ X 0.0012517684 0.0002765438 0.0007887114 Y 0.0002765438 0.0002570286 0.0002117336 Z 0.0007887114 0.0002117336 0.0009168750 And a weight vector generated by w- read.table(c:/r.weights) X Y Z 1 0.5818416 0.2158531 0.2023053 I want to compute the product of the matrix and vectors termwise to generate a 3x3 matrix, where m[i,j]=w[i]*co[i,j]*w[j]. 0.000423773 7.47216E-08 4.41255E-08 7.47216E-08 1.96566E-11 4.29229E-11 4.41255E-08 4.29229E-11 4.11045E-11 Is this possible without writing explicit loops? Thank you, Dan Stanger Eaton Vance Management 200 State Street Boston, MA 02109 617 598 8261 [[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. __ 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.