Matrix Results

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Matrix Results

MSousa
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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Results

PIKAL Petr
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Results

MSousa
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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Results

jholtman
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Results

MSousa
Hello


Error: could not find function sqldf:

Hello, I'm using R Studio, and installed the option of installing the
packages sqldbf function.
    But When I run the code give the next error.


install.packages("sqldf")
library("RSQLite")
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
+ ")


ERROR: lazy loading failed for package ‘sqldf’
* removing ‘/home/ricardosousa/R/x86_64-pc-linux-gnu-library/2.13/sqldf’
Warning in install.packages :
  installation of package 'sqldf' had non-zero exit status

The downloaded packages are in
        ‘/tmp/RtmpS53jrJ/downloaded_packages’



--
View this message in context: http://r.789695.n4.nabble.com/Matrix-Results-tp4468642p4469239.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Results

Uwe Ligges-3


On 13.03.2012 15:40, RMSOPS wrote:

> Hello
>
>
> Error: could not find function sqldf:
>
> Hello, I'm using R Studio, and installed the option of installing the
> packages sqldbf function.
>      But When I run the code give the next error.
>
>
> install.packages("sqldf")
> library("RSQLite")
> 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
> + ")
>
>
> ERROR: lazy loading failed for package ‘sqldf’
> * removing ‘/home/ricardosousa/R/x86_64-pc-linux-gnu-library/2.13/sqldf’
> Warning in install.packages :
>    installation of package 'sqldf' had non-zero exit status


So that error message tells us that the installation of sqldf did not
succeed on your machine.

Uwe Ligges


> The downloaded packages are in
> ‘/tmp/RtmpS53jrJ/downloaded_packages’
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Matrix-Results-tp4468642p4469239.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Filling matrices

dkStevens
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.
Reply | Threaded
Open this post in threaded view
|

Re: Filling matrices

plangfelder
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Filling matrices

Peter Meilstrup
In reply to this post by dkStevens
http://cran.r-project.org/doc/manuals/R-lang.html#Indexing-matrices-and-arrays

matrix[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<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/**
> posting-guide.html <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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Filling matrices

Sarah Goslee
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Filling matrices

Berend Hasselman
In reply to this post by dkStevens

On 19-03-2012, at 22:07, David Stevens 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?

pIndex <- cbind(iRows,iCols)

A <- matrix(0,nrow=20,ncol=20)

A[pIndex] <- 9
A

Berend

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Filling matrices

Bert Gunter
In reply to this post by plangfelder
Please folks!

Why issue protestations of  "clumsiness" when all you have to do is
make some effort to learn R?

See section 5.3 of an Intro to R -- which ships with every copy of R
-- on matrix indexing.

?"["

also documents the behavior, albeit more tersely. ("... a third form
of indexig ...")

(ergo the process is: Create a matrix with 0's; use matrix indexing to
populate the rows and columns of choice).

Of course, the best solution may be just to use the SparseM package to
handle it. But as I have not used it (except indirectly) ...

-- Bert

On Mon, Mar 19, 2012 at 2:16 PM, Peter Langfelder
<[hidden email]> wrote:

> 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-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

______________________________________________
[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.