# Matrix Results

12 messages
Open this post in threaded view
|

## Matrix Results

 Hello I am developing a small program that to calculate the maximum, minimum and average. The data.frame v is POS DIF 4 - 4 56 4 - 3 61 3 - 3 300 3 - 3 27 3 - 3 33 3 - 3 87 3 - 4 49 4 - 4 71 4 - 3 121 3 - 4 138 4 - 3 15 When execute res<-table(df\$v) gives this   Var1 Freq 4 - 4 2 4 - 3 3 3 - 3 3 3 - 4 2 4 - 4 1 If possible, my idea is that the result often present in addition to the minimum, maximum and average, all in a single array, for example. what is the quickest way to do this Var1 Freq Min Max Med 4 - 4 2 56 71 .... 4 - 3 3 15 121 3 - 3 3 .... ..... 3 - 4 2 ... ... 4 - 4 1 ... .... Thanks
Open this post in threaded view
|

## Re: Matrix Results

 Hi > > Hello > >    I am developing a small program that to calculate the maximum, minimum > and average. > >     The data.frame v is > > POS       DIF > 4 - 4       56 > 4 - 3       61 > 3 - 3       300 > 3 - 3        27 > 3 - 3       33 > 3 - 3       87 > 3 - 4      49 > 4 - 4      71 > 4 - 3     121 > 3 - 4     138 > 4 - 3      15 > > > When execute  res<-table(df\$v) gives this > Â > Var1    Freq > 4 - 4      2 > 4 - 3      3 > 3 - 3     3 > 3 - 4     2 > 4 - 4    1 > > If possible, my idea is that the result often present in addition to the > minimum, maximum and average, all in a single array, for example. >    what is the quickest way to do this > > Var1    Freq  Min    Max Med > 4 - 4      2      56         71  .... > 4 - 3      3      15       121 > 3 - 3     3      ....        ..... > 3 - 4     2     ...            ... > 4 - 4    1    ...             .... ?aggregate Regards Petr > > Thanks > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Matrix-> Results-tp4468642p4468642.html > Sent from the R help mailing list archive at Nabble.com. >    [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Matrix Results

 Hello is the dataset that was sent to help, has over two columns the source and destination, is the separation of position pos POS       DIF       Source    Dest 4 - 4       56             4              4 4 - 3       61             4             3   3 - 3       300           3             3 3 - 3        27            3             3 3 - 3       33             3             3 3 - 3       87             3             3 3 - 4      49              3             3   4 - 4      71              4             3 4 - 3     121            4             3 3 - 4     138            3             3 4 - 3      15             4             3
Open this post in threaded view
|

## Re: Matrix Results

 In reply to this post by MSousa Try this: > require(sqldf) > x <- read.fwf(textConnection("4 - 4       56 + 4 - 3       61 + 3 - 3       300 + 3 - 3        27 + 3 - 3       33 + 3 - 3       87 + 3 - 4      49 + 4 - 4      71 + 4 - 3     121 + 3 - 4     138 + 4 - 3      15"), width = c(7,8) , header = FALSE, as.is = TRUE) > closeAllConnections() > sqldf(" +     select V1 +             , count(*) as Freq +             , min(V2) as Min +             , max(V2) as Max +             , median(V2) as Median +         from x +         group by V1 + ")        V1 Freq Min Max Median 1 3 - 3      4  27 300   60.0 2 3 - 4      2  49 138   93.5 3 4 - 3      3  15 121   61.0 4 4 - 4      2  56  71   63.5 > On Tue, Mar 13, 2012 at 5:45 AM, RMSOPS <[hidden email]> wrote: > Hello > >   I am developing a small program that to calculate the maximum, minimum > and average. > >    The data.frame v is > > POS       DIF > 4 - 4       56 > 4 - 3       61 > 3 - 3       300 > 3 - 3        27 > 3 - 3       33 > 3 - 3       87 > 3 - 4      49 > 4 - 4      71 > 4 - 3     121 > 3 - 4     138 > 4 - 3      15 > > > When execute  res<-table(df\$v) gives this > > Var1    Freq > 4 - 4      2 > 4 - 3      3 > 3 - 3     3 > 3 - 4     2 > 4 - 4    1 > > If possible, my idea is that the result often present in addition to the > minimum, maximum and average, all in a single array, for example. >   what is the quickest way to do this > > Var1    Freq  Min    Max Med > 4 - 4      2      56         71  .... > 4 - 3      3      15       121 > 3 - 3     3      ....        ..... > 3 - 4     2     ...            ... > 4 - 4    1    ...             .... > > Thanks > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Matrix-Results-tp4468642p4468642.html> Sent from the R help mailing list archive at Nabble.com. >        [[alternative HTML version deleted]] > > > ______________________________________________ > [hidden email] 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 Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Matrix Results

Open this post in threaded view
|

## Re: Matrix Results

Open this post in threaded view
|

## Filling matrices

 I'm a bit clumsy about many things in R. Here's my problem. I'm trying to build a square sparse matrix and populate it without looping (bad practice, right). I have vectors of matched row/column pairs for which the matrix entries have common characteristics and am look for a way to fill the entries. So, if the matrix is A[20 by 20], and I might have rows   iRows <- c(2,3,4,6,7,8,10,11,12,14,15,16,18,19) and columns   iCols <- c(1,2,3,5,6,7,9,10,11,13,14,15,17,18) and you see these are most of the subdiagonal terms in A from rows 2-19. They are all calculated in a similar way using values from a data frame in which the terms are generally in iRows and iCols. I could loop through each pair and all's well, but my question is whether there's an R-certified alternative, that will speed things up when the matrix is much larger (it will be - this is a prototype). Any thoughts? David S -- David K Stevens, P.E., Ph.D., Professor Civil and Environmental Engineering Utah Water Research Laboratory 8200 Old Main Hill Logan, UT  84322-8200 435 797 3229 - voice 435 797 1363 - fax [hidden email] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Filling matrices

 On Mon, Mar 19, 2012 at 2:07 PM, David Stevens <[hidden email]> wrote: > I'm a bit clumsy about many things in R. Here's my problem. I'm trying to > build a square sparse matrix and populate it without looping (bad practice, > right). I have vectors of matched row/column pairs for which the matrix > entries have common characteristics and am look for a way to fill the > entries. So, if the matrix is A[20 by 20], and I might have rows > >  iRows <- c(2,3,4,6,7,8,10,11,12,14,15,16,18,19) > > and columns > >  iCols <- c(1,2,3,5,6,7,9,10,11,13,14,15,17,18) > > and you see these are most of the subdiagonal terms in A from rows 2-19. > They are all calculated in a similar way using values from a data frame in > which the terms are generally in iRows and iCols. > > I could loop through each pair and all's well, but my question is whether > there's an R-certified alternative, that will speed things up when the > matrix is much larger (it will be - this is a prototype). > > Any thoughts? This isn't very elegant, but it's a solution, and probably quite fast on large matrices: n = 20 # matrix dimension # Calculate the indices of the elements when the matrix is turned into a single linear vector indices = (iCols-1) * n + iRows # Fill the vector A.vector = rep(0, n^2); A.vector[indices] = values; # Pick one: Assign the values from the vector to the matrix A[, ] = A.vector # Or re-dimension the vector to a matrix # if copying is to be avoided) dim(A.vector) = c(n,n); A=A.vector; HTH, Peter > > David S > > -- > David K Stevens, P.E., Ph.D., Professor > Civil and Environmental Engineering > Utah Water Research Laboratory > 8200 Old Main Hill > Logan, UT  84322-8200 > 435 797 3229 - voice > 435 797 1363 - fax > [hidden email] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Filling matrices

 In reply to this post by dkStevens http://cran.r-project.org/doc/manuals/R-lang.html#Indexing-matrices-and-arraysmatrix[cbind(iRows, iCols)] <- values Peter On Mon, Mar 19, 2012 at 2:07 PM, David Stevens <[hidden email]>wrote: > I'm a bit clumsy about many things in R. Here's my problem. I'm trying to > build a square sparse matrix and populate it without looping (bad practice, > right). I have vectors of matched row/column pairs for which the matrix > entries have common characteristics and am look for a way to fill the > entries. So, if the matrix is A[20 by 20], and I might have rows > >  iRows <- c(2,3,4,6,7,8,10,11,12,14,15,**16,18,19) > > and columns > >  iCols <- c(1,2,3,5,6,7,9,10,11,13,14,**15,17,18) > > and you see these are most of the subdiagonal terms in A from rows 2-19. > They are all calculated in a similar way using values from a data frame in > which the terms are generally in iRows and iCols. > > I could loop through each pair and all's well, but my question is whether > there's an R-certified alternative, that will speed things up when the > matrix is much larger (it will be - this is a prototype). > > Any thoughts? > > David S > > -- > David K Stevens, P.E., Ph.D., Professor > Civil and Environmental Engineering > Utah Water Research Laboratory > 8200 Old Main Hill > Logan, UT  84322-8200 > 435 797 3229 - voice > 435 797 1363 - fax > [hidden email] > > ______________________________**________________ > [hidden email] 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. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Filling matrices

 In reply to this post by dkStevens I suspect I'm misinterpreting what you are trying to do, because it seems unlikely that the solution is as simple as: A <- matrix(0, 20, 20) iRows <- c(2,3,4,6,7,8,10,11,12,14,15,16,18,19) iCols <- c(1,2,3,5,6,7,9,10,11,13,14,15,17,18) A[iRows, iCols] <- 1 # or a vector of the same length in the same order So if that's not it, can you expand a bit on what your desired result looks like? Sarah On Mon, Mar 19, 2012 at 5:07 PM, David Stevens <[hidden email]> wrote: > I'm a bit clumsy about many things in R. Here's my problem. I'm trying to > build a square sparse matrix and populate it without looping (bad practice, > right). I have vectors of matched row/column pairs for which the matrix > entries have common characteristics and am look for a way to fill the > entries. So, if the matrix is A[20 by 20], and I might have rows > >  iRows <- c(2,3,4,6,7,8,10,11,12,14,15,16,18,19) > > and columns > >  iCols <- c(1,2,3,5,6,7,9,10,11,13,14,15,17,18) > > and you see these are most of the subdiagonal terms in A from rows 2-19. > They are all calculated in a similar way using values from a data frame in > which the terms are generally in iRows and iCols. > > I could loop through each pair and all's well, but my question is whether > there's an R-certified alternative, that will speed things up when the > matrix is much larger (it will be - this is a prototype). > > Any thoughts? > > David S > -- Sarah Goslee http://www.functionaldiversity.org______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.