Re: [R] Matrix Multiplication, Floating-Point, etc.
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Talbot Katz Sent: Monday, July 30, 2007 10:55 AM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED]; r-help@stat.math.ethz.ch Subject: Re: [R] Matrix Multiplication, Floating-Point, etc. Thank you for responding! I realize that floating point operations are often inexact, and indeed, the difference between the two answers is within the all.equal tolerance, as mentioned in FAQ 7.31 (cited by Charles): (as.numeric(ev1%*%ev2))==(sum(ev1*ev2)) [1] FALSE all.equal((as.numeric(ev1%*%ev2)),(sum(ev1*ev2))) [1] TRUE I suppose that's good enough for numerical computation. But I was still surprised to see that matrix multiplication (ev1%*%ev2) doesn't give the exact right answer, whereas sum(ev1*ev2) does give the exact answer. I would've expected them to perform the same two multiplications and one addition. But I guess that's not the case. However, I did find that if I multiplied the two vectors by 10, making the entries integers (although the class was still numeric rather than integer), both computations gave equal answers of 0: xf1-10*ev1 xf2-10*ev2 (as.numeric(xf1%*%xf2))==(sum(xf1*xf2)) [1] TRUE Perhaps the moral of the story is that one should exercise caution and keep track of significant digits. -- TMK -- 212-460-5430 home 917-656-5351 cell There may other issues involved here besides R version, floating point precision, and OS version. On my WinXP system running R-2.5.1 binary from CRAN, I get what you expected: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,]0 There could be differences in OS release, service packs installed, cpu, etc. But the moral you draw is probably a reasonable one. Dan Daniel Nordlund Bothell, WA __ R-help@stat.math.ethz.ch 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] Matrix Multiplication, Floating-Point, etc.
On Mon, 30 Jul 2007, Moshe Olshansky wrote: After multiplication by 10 you get 6*8 = 48 - the result is an exact machine number so there is no roundoff, while 0.6*0.8 = 0.48, where neither of the 3 numbers (0.6, 0.8, 0.48) is an exact machine mumber. However, (-0.6)*0.8 should be equal EXACTLY to -(0.6*0.8), and in fact you get that sum(ev1*ev2) is exactly 0. What is strange is that you are not getting this result from ev1 %*% ev2. This means that either %^% uses some non-straightforward algorithm or it somehow sets the rounding control to something different from round to nearest. In the later case (-0.6) does not necessarily equal to -(0.6) and the rounding after multiplication is not necessarily symetric. Mr Olshansky seems unaware of the effects of extended-precision intermediate arithmetic on ix86 CPUs. sum() does use a higher-precision accumulator (where available, including on Windows), but ev1*ev2 is done in R and so stored to basic precision. OTOH, %*% (sic) calls the BLAS routine dgemm and hence may accumulate in 80-bit floating-point registers. What result you get will depend on what compiler, compiler flags and BLAS is in use, but with the default reference BLAS it is very likely that some of the intermediate results are stored in FP registers to extended precision. It is a simple experiment to confirm this: recompile the BLAS with -fforce-store and you do get 0 (at least on my Windows build system). Let's see less speculation and more homework in future. Regards, Moshe. --- Talbot Katz [EMAIL PROTECTED] wrote: Thank you for responding! I realize that floating point operations are often inexact, and indeed, the difference between the two answers is within the all.equal tolerance, as mentioned in FAQ 7.31 (cited by Charles): (as.numeric(ev1%*%ev2))==(sum(ev1*ev2)) [1] FALSE all.equal((as.numeric(ev1%*%ev2)),(sum(ev1*ev2))) [1] TRUE I suppose that's good enough for numerical computation. But I was still surprised to see that matrix multiplication (ev1%*%ev2) doesn't give the exact right answer, whereas sum(ev1*ev2) does give the exact answer. I would've expected them to perform the same two multiplications and one addition. But I guess that's not the case. However, I did find that if I multiplied the two vectors by 10, making the entries integers (although the class was still numeric rather than integer), both computations gave equal answers of 0: xf1-10*ev1 xf2-10*ev2 (as.numeric(xf1%*%xf2))==(sum(xf1*xf2)) [1] TRUE Perhaps the moral of the story is that one should exercise caution and keep track of significant digits. -- TMK -- 212-460-5430 home 917-656-5351 cell From: Charles C. Berry [EMAIL PROTECTED] To: Talbot Katz [EMAIL PROTECTED] CC: r-help@stat.math.ethz.ch Subject: Re: [R] Matrix Multiplication, Floating-Point, etc. Date: Mon, 30 Jul 2007 09:27:42 -0700 7.31 Why doesn't R think these numbers are equal? On Fri, 27 Jul 2007, Talbot Katz wrote: Hi. I recently tried the following in R 2.5.1 on Windows XP: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,] -2.664427e-17 sum(ev1*ev2) [1] 0 (I got the same result with R 2.4.1 on a different Windows XP machine.) I expect this issue is very familiar and probably has been discussed in this forum before. Can someone please point me to some documentation or discussion about this? Is there some standard way to get the correct answer from %*%? Thanks! -- TMK -- 212-460-5430 home 917-656-5351 cell -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch 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] Matrix nesting (was Re: Obtaining summary of frequencies of value occurrences for a variable in a multivariate dataset.)
Hi !-- @page { size: 21cm 29.7cm; margin: 2cm } P { margin-bottom: 0.21cm } -- I would like to nest matrices, is there a way of doing so, I am getting “number of items to replace is not a multiple of replacement length” errors (probably R is trying to flatten the matrix into a vector and complains if the vector is larger than 1 element during the insert) I have a matrix (see below) in which I would like to place one other matrices in to each k[2,i] position (where i is value between 1 to 4) Why – each value in k[1,i] may represent several (1or more) key-value results which I would like to capture in the corresponding k[2,i] element. k [,1] [,2] [,3] [,4] myVariableNames PR10 PR11 PR12 PR13 x2 00 00 Allan. - Original Message From: Allan Kamau [EMAIL PROTECTED] To: jim holtman [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Sent: Saturday, July 28, 2007 2:48:47 PM Subject: Re: [R] Obtaining summary of frequencies of value occurrences for a variable in a multivariate dataset. Hi Jim, The problem description. I am trying to identify mutations in a given gene from a particular genome (biological genome sequence). I have two CSV files consisting of sequences. One file consists of reference (documented,curated accepted as standard) sequences. The other consists of sample sequences I am trying to identify mutations within. In both files the an individual sequence is contained in a single record, it’s amino acid residues ( the actual sequence of alphabets each representing a given amino acid for example “A” stands for “Alanine”, “C” for Cysteine and so on) are each allocated a single field in the CSV file. The sequences in both files have been well aligned, each contain 115 residues with the first residue is contained in the field 5. The fields 1 to 4 are allocated for metadata (name of sequence and so on). My task is to compile a residue occurrence count for each residue present in a given field in the reference sequence dataset and use this information when reading each sequence in the sample dataset to identify a mutation. For example for position 9 of the sample sequence “bb” a “P” is found and according to our reference sequence dataset of summaries, at position 9 “P” may not even exist or may have an occurrence of 10% or so will be classified as mutation, (I could employ a cut of parameter for mutation classification). Allan. --- jim holtman [EMAIL PROTECTED] wrote: results=()#character() myVariableNames=names(x.val) results[length(myVariableNames)]-NA for (i in myVariableNames){ results[i]-names(x.val[[i]])# this does not work it returns a NULL (how can i convert this to x.val$somevalue ? ) } On 7/27/07, Allan Kamau [EMAIL PROTECTED] wrote: Hi All, I am having difficulties finding a way to find a substitute to the command names(v.val$PR14) so that I could generate the command on the fly for all PR14 to PR200 (please see the previous discussion below to understand what the object x.val contains) . I have tried the following results=()#character() myVariableNames=names(x.val) results[length(myVariableNames)]-NA for as.vector(unlist(strsplit(str,,)),mode=list) +results[i]-names(x.val$i)# this does not work it returns a NULL (how can i convert this to x.val$somevalue ? ) } Allan. - Original Message From: Allan Kamau [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Thursday, July 26, 2007 10:03:17 AM Subject: Re: [R] Obtaining summary of frequencies of value occurrences for a variable in a multivariate dataset. Thanks so much Jim, Andaikalavan, Gabor and others for the help and suggestions. The solution will result in a matrix containing nested matrices to enable each variable name, each variables distinct value and the count of the distinct value to be accessible individually. The main matrix will contain the variable names, the first level nested matrices will consist of the variables unique values, and each such variable entry will contain a one element vector to contain the count or occurrence frequency. This matrix can now be used in comparing other similar datasets for variable values and their frequencies. Building on the input received so far, a probable solution in building the matrix will include the following. 1)I reading the csv file (containing column headers) my_data=read.table(path/to/my/data.csv,header=TRUE,sep=,,dec=.,fill=TRUE) 2)I group the values in each variable producing an occurrence count(frequency) x.val-apply(my_data,2,table) 3)I obtain a vector of the names of the variables in the table names(x.val) 4)Now I make use of the names (obtained in step 3) to obtain a vector of distinct values in a given variable (in the example below the variable name
[R] Matrix Multiplication, Floating-Point, etc.
Hi. I recently tried the following in R 2.5.1 on Windows XP: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,] -2.664427e-17 sum(ev1*ev2) [1] 0 (I got the same result with R 2.4.1 on a different Windows XP machine.) I expect this issue is very familiar and probably has been discussed in this forum before. Can someone please point me to some documentation or discussion about this? Is there some standard way to get the correct answer from %*%? Thanks! -- TMK -- 212-460-5430home 917-656-5351cell __ R-help@stat.math.ethz.ch 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] matrix output in R, and file name creating
Hi, I want to save a matrix (say, matrix[6,7,8]) write a cvs file. How can I do that??? can I write in one file? if I could not write in one file, i want to use a loop to save in different files (in the matrix[6,7,8], should be 8 csv files), such as the filename structure should be: file =filename +str(i) +. +csv Many thanks. Dong [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] Matrix Multiplication, Floating-Point, etc.
This is giving you exactly what you are asking for. The operator * does element by element multiplication. So, .48 + -.48 =0, right? Is there another mathematical possibility you were expecting? -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Talbot Katz Sent: Friday, July 27, 2007 6:31 PM To: r-help@stat.math.ethz.ch Subject: [R] Matrix Multiplication, Floating-Point, etc. Hi. I recently tried the following in R 2.5.1 on Windows XP: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,] -2.664427e-17 sum(ev1*ev2) [1] 0 (I got the same result with R 2.4.1 on a different Windows XP machine.) I expect this issue is very familiar and probably has been discussed in this forum before. Can someone please point me to some documentation or discussion about this? Is there some standard way to get the correct answer from %*%? Thanks! -- TMK -- 212-460-5430 home 917-656-5351 cell __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Matrix nesting (was Re: Obtaining summary of frequencies of value occurrences for a variable in a multivariate dataset.)
Success, thanks Patrick. Below is the final matrix construction code. x=list() x[length(myVariableNames)]-NA names(x)-names(x.val) for (i in myVariableNames){ residues=names(x.val[[i]]) residuesFrequencies=as.vector(x.val[[i]]) someList=list() names(residuesFrequencies)=residues someList-list(frequency=residuesFrequencies) x[i]-someList } #The output x[16:18] $PR12 I 10 $PR13 K R 8 2 $PR14 I V 2 8 - Original Message From: Patrick Burns [EMAIL PROTECTED] To: Allan Kamau [EMAIL PROTECTED] Sent: Monday, July 30, 2007 12:01:32 PM Subject: Re: [R] Matrix nesting (was Re: Obtaining summary of frequencies of value occurrences for a variable in a multivariate dataset.) I think you want your main matrix to be of mode list. S Poetry talks about this some. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and A Guide for the Unwilling S User) Allan Kamau wrote: Hi !-- @page { size: 21cm 29.7cm; margin: 2cm } P { margin-bottom: 0.21cm } -- I would like to nest matrices, is there a way of doing so, I am getting “number of items to replace is not a multiple of replacement length” errors (probably R is trying to flatten the matrix into a vector and complains if the vector is larger than 1 element during the insert) I have a matrix (see below) in which I would like to place one other matrices in to each k[2,i] position (where i is value between 1 to 4) Why – each value in k[1,i] may represent several (1or more) key-value results which I would like to capture in the corresponding k[2,i] element. k [,1] [,2] [,3] [,4] myVariableNames PR10 PR11 PR12 PR13 x2 00 00 Allan. - Original Message From: Allan Kamau [EMAIL PROTECTED] To: jim holtman [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Sent: Saturday, July 28, 2007 2:48:47 PM Subject: Re: [R] Obtaining summary of frequencies of value occurrences for a variable in a multivariate dataset. Hi Jim, The problem description. I am trying to identify mutations in a given gene from a particular genome (biological genome sequence). I have two CSV files consisting of sequences. One file consists of reference (documented,curated accepted as standard) sequences. The other consists of sample sequences I am trying to identify mutations within. In both files the an individual sequence is contained in a single record, it’s amino acid residues ( the actual sequence of alphabets each representing a given amino acid for example “A” stands for “Alanine”, “C” for Cysteine and so on) are each allocated a single field in the CSV file. The sequences in both files have been well aligned, each contain 115 residues with the first residue is contained in the field 5. The fields 1 to 4 are allocated for metadata (name of sequence and so on). My task is to compile a residue occurrence count for each residue present in a given field in the reference sequence dataset and use this information when reading each sequence in the sample dataset to identify a mutation. For example for position 9 of the sample sequence “bb” a “P” is found and according to our reference sequence dataset of summaries, at position 9 “P” may not even exist or may have an occurrence of 10% or so will be classified as mutation, (I could employ a cut of parameter for mutation classification). Allan. --- jim holtman [EMAIL PROTECTED] wrote: results=()#character() myVariableNames=names(x.val) results[length(myVariableNames)]-NA for (i in myVariableNames){ results[i]-names(x.val[[i]])# this does not work it returns a NULL (how can i convert this to x.val$somevalue ? ) } On 7/27/07, Allan Kamau [EMAIL PROTECTED] wrote: Hi All, I am having difficulties finding a way to find a substitute to the command names(v.val$PR14) so that I could generate the command on the fly for all PR14 to PR200 (please see the previous discussion below to understand what the object x.val contains) . I have tried the following results=()#character() myVariableNames=names(x.val) results[length(myVariableNames)]-NA for as.vector(unlist(strsplit(str,,)),mode=list) +results[i]-names(x.val$i)# this does not work it returns a NULL (how can i convert this to x.val$somevalue ? ) } Allan. - Original Message From: Allan Kamau [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Thursday, July 26, 2007 10:03:17 AM Subject: Re: [R] Obtaining summary of frequencies of value occurrences for a variable in a multivariate dataset. Thanks so much Jim, Andaikalavan, Gabor and others for the help and suggestions. The solution will result in a matrix containing nested matrices to enable each variable name, each variables distinct value and the count of the distinct value to be accessible
Re: [R] Matrix Multiplication, Floating-Point, etc.
7.31 Why doesn't R think these numbers are equal? On Fri, 27 Jul 2007, Talbot Katz wrote: Hi. I recently tried the following in R 2.5.1 on Windows XP: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,] -2.664427e-17 sum(ev1*ev2) [1] 0 (I got the same result with R 2.4.1 on a different Windows XP machine.) I expect this issue is very familiar and probably has been discussed in this forum before. Can someone please point me to some documentation or discussion about this? Is there some standard way to get the correct answer from %*%? Thanks! -- TMK -- 212-460-5430 home 917-656-5351 cell __ R-help@stat.math.ethz.ch 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@stat.math.ethz.ch 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] Matrix Multiplication, Floating-Point, etc.
Thank you for responding! I realize that floating point operations are often inexact, and indeed, the difference between the two answers is within the all.equal tolerance, as mentioned in FAQ 7.31 (cited by Charles): (as.numeric(ev1%*%ev2))==(sum(ev1*ev2)) [1] FALSE all.equal((as.numeric(ev1%*%ev2)),(sum(ev1*ev2))) [1] TRUE I suppose that's good enough for numerical computation. But I was still surprised to see that matrix multiplication (ev1%*%ev2) doesn't give the exact right answer, whereas sum(ev1*ev2) does give the exact answer. I would've expected them to perform the same two multiplications and one addition. But I guess that's not the case. However, I did find that if I multiplied the two vectors by 10, making the entries integers (although the class was still numeric rather than integer), both computations gave equal answers of 0: xf1-10*ev1 xf2-10*ev2 (as.numeric(xf1%*%xf2))==(sum(xf1*xf2)) [1] TRUE Perhaps the moral of the story is that one should exercise caution and keep track of significant digits. -- TMK -- 212-460-5430home 917-656-5351cell From: Charles C. Berry [EMAIL PROTECTED] To: Talbot Katz [EMAIL PROTECTED] CC: r-help@stat.math.ethz.ch Subject: Re: [R] Matrix Multiplication, Floating-Point, etc. Date: Mon, 30 Jul 2007 09:27:42 -0700 7.31 Why doesn't R think these numbers are equal? On Fri, 27 Jul 2007, Talbot Katz wrote: Hi. I recently tried the following in R 2.5.1 on Windows XP: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,] -2.664427e-17 sum(ev1*ev2) [1] 0 (I got the same result with R 2.4.1 on a different Windows XP machine.) I expect this issue is very familiar and probably has been discussed in this forum before. Can someone please point me to some documentation or discussion about this? Is there some standard way to get the correct answer from %*%? Thanks! -- TMK -- 212-460-5430 home 917-656-5351 cell __ R-help@stat.math.ethz.ch 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@stat.math.ethz.ch 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] Matrix Multiplication, Floating-Point, etc.
Talbot The general advice on this list is to read the following http://docs.sun.com/source/806-3568/ncg_goldberg.html -Original Message- From: Talbot Katz [mailto:[EMAIL PROTECTED] Sent: Monday, July 30, 2007 1:55 PM To: [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch; Doran, Harold Subject: Re: [R] Matrix Multiplication, Floating-Point, etc. Thank you for responding! I realize that floating point operations are often inexact, and indeed, the difference between the two answers is within the all.equal tolerance, as mentioned in FAQ 7.31 (cited by Charles): (as.numeric(ev1%*%ev2))==(sum(ev1*ev2)) [1] FALSE all.equal((as.numeric(ev1%*%ev2)),(sum(ev1*ev2))) [1] TRUE I suppose that's good enough for numerical computation. But I was still surprised to see that matrix multiplication (ev1%*%ev2) doesn't give the exact right answer, whereas sum(ev1*ev2) does give the exact answer. I would've expected them to perform the same two multiplications and one addition. But I guess that's not the case. However, I did find that if I multiplied the two vectors by 10, making the entries integers (although the class was still numeric rather than integer), both computations gave equal answers of 0: xf1-10*ev1 xf2-10*ev2 (as.numeric(xf1%*%xf2))==(sum(xf1*xf2)) [1] TRUE Perhaps the moral of the story is that one should exercise caution and keep track of significant digits. -- TMK -- 212-460-5430 home 917-656-5351 cell From: Charles C. Berry [EMAIL PROTECTED] To: Talbot Katz [EMAIL PROTECTED] CC: r-help@stat.math.ethz.ch Subject: Re: [R] Matrix Multiplication, Floating-Point, etc. Date: Mon, 30 Jul 2007 09:27:42 -0700 7.31 Why doesn't R think these numbers are equal? On Fri, 27 Jul 2007, Talbot Katz wrote: Hi. I recently tried the following in R 2.5.1 on Windows XP: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,] -2.664427e-17 sum(ev1*ev2) [1] 0 (I got the same result with R 2.4.1 on a different Windows XP machine.) I expect this issue is very familiar and probably has been discussed in this forum before. Can someone please point me to some documentation or discussion about this? Is there some standard way to get the correct answer from %*%? Thanks! -- TMK -- 212-460-5430home 917-656-5351cell __ R-help@stat.math.ethz.ch 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@stat.math.ethz.ch 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] Matrix Multiplication, Floating-Point, etc.
One thing to realize is that although it appears that the operations are the same, the code that is being executed is different in the two cases. Due to the different sequence of instructions, there may be round-off errors that are then introduced On 7/30/07, Talbot Katz [EMAIL PROTECTED] wrote: Thank you for responding! I realize that floating point operations are often inexact, and indeed, the difference between the two answers is within the all.equal tolerance, as mentioned in FAQ 7.31 (cited by Charles): (as.numeric(ev1%*%ev2))==(sum(ev1*ev2)) [1] FALSE all.equal((as.numeric(ev1%*%ev2)),(sum(ev1*ev2))) [1] TRUE I suppose that's good enough for numerical computation. But I was still surprised to see that matrix multiplication (ev1%*%ev2) doesn't give the exact right answer, whereas sum(ev1*ev2) does give the exact answer. I would've expected them to perform the same two multiplications and one addition. But I guess that's not the case. However, I did find that if I multiplied the two vectors by 10, making the entries integers (although the class was still numeric rather than integer), both computations gave equal answers of 0: xf1-10*ev1 xf2-10*ev2 (as.numeric(xf1%*%xf2))==(sum(xf1*xf2)) [1] TRUE Perhaps the moral of the story is that one should exercise caution and keep track of significant digits. -- TMK -- 212-460-5430home 917-656-5351cell From: Charles C. Berry [EMAIL PROTECTED] To: Talbot Katz [EMAIL PROTECTED] CC: r-help@stat.math.ethz.ch Subject: Re: [R] Matrix Multiplication, Floating-Point, etc. Date: Mon, 30 Jul 2007 09:27:42 -0700 7.31 Why doesn't R think these numbers are equal? On Fri, 27 Jul 2007, Talbot Katz wrote: Hi. I recently tried the following in R 2.5.1 on Windows XP: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,] -2.664427e-17 sum(ev1*ev2) [1] 0 (I got the same result with R 2.4.1 on a different Windows XP machine.) I expect this issue is very familiar and probably has been discussed in this forum before. Can someone please point me to some documentation or discussion about this? Is there some standard way to get the correct answer from %*%? Thanks! -- TMK -- 212-460-5430 home 917-656-5351 cell __ R-help@stat.math.ethz.ch 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Matrix Multiplication, Floating-Point, etc.
After multiplication by 10 you get 6*8 = 48 - the result is an exact machine number so there is no roundoff, while 0.6*0.8 = 0.48, where neither of the 3 numbers (0.6, 0.8, 0.48) is an exact machine mumber. However, (-0.6)*0.8 should be equal EXACTLY to -(0.6*0.8), and in fact you get that sum(ev1*ev2) is exactly 0. What is strange is that you are not getting this result from ev1 %*% ev2. This means that either %^% uses some non-straightforward algorithm or it somehow sets the rounding control to something different from round to nearest. In the later case (-0.6) does not necessarily equal to -(0.6) and the rounding after multiplication is not necessarily symetric. Regards, Moshe. --- Talbot Katz [EMAIL PROTECTED] wrote: Thank you for responding! I realize that floating point operations are often inexact, and indeed, the difference between the two answers is within the all.equal tolerance, as mentioned in FAQ 7.31 (cited by Charles): (as.numeric(ev1%*%ev2))==(sum(ev1*ev2)) [1] FALSE all.equal((as.numeric(ev1%*%ev2)),(sum(ev1*ev2))) [1] TRUE I suppose that's good enough for numerical computation. But I was still surprised to see that matrix multiplication (ev1%*%ev2) doesn't give the exact right answer, whereas sum(ev1*ev2) does give the exact answer. I would've expected them to perform the same two multiplications and one addition. But I guess that's not the case. However, I did find that if I multiplied the two vectors by 10, making the entries integers (although the class was still numeric rather than integer), both computations gave equal answers of 0: xf1-10*ev1 xf2-10*ev2 (as.numeric(xf1%*%xf2))==(sum(xf1*xf2)) [1] TRUE Perhaps the moral of the story is that one should exercise caution and keep track of significant digits. -- TMK -- 212-460-5430 home 917-656-5351 cell From: Charles C. Berry [EMAIL PROTECTED] To: Talbot Katz [EMAIL PROTECTED] CC: r-help@stat.math.ethz.ch Subject: Re: [R] Matrix Multiplication, Floating-Point, etc. Date: Mon, 30 Jul 2007 09:27:42 -0700 7.31 Why doesn't R think these numbers are equal? On Fri, 27 Jul 2007, Talbot Katz wrote: Hi. I recently tried the following in R 2.5.1 on Windows XP: ev2-c(0.8,-0.6) ev1-c(0.6,0.8) ev1%*%ev2 [,1] [1,] -2.664427e-17 sum(ev1*ev2) [1] 0 (I got the same result with R 2.4.1 on a different Windows XP machine.) I expect this issue is very familiar and probably has been discussed in this forum before. Can someone please point me to some documentation or discussion about this? Is there some standard way to get the correct answer from %*%? Thanks! -- TMK -- 212-460-5430home 917-656-5351cell __ R-help@stat.math.ethz.ch 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix of scatterplots
Hi, I would like to use the function pairs() to plot a matrix of scatterplots. For each scatterplot, the data are plotted in circles, can I add some argument to change the circles into dots? Could anyone give me some advice?Many thanks -- View this message in context: http://www.nabble.com/matrix-of-scatterplots-tf4067527.html#a11558049 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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] matrix of scatterplots
m - matrix( rnorm(300), nc=3 ) pairs(m, pch=20) or pairs(m, pch=.) See help(par) for more details. livia wrote: Hi, I would like to use the function pairs() to plot a matrix of scatterplots. For each scatterplot, the data are plotted in circles, can I add some argument to change the circles into dots? Could anyone give me some advice?Many thanks __ R-help@stat.math.ethz.ch 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] matrix of scatterplots
Thank you very much for your help. Adaikalavan Ramasamy wrote: m - matrix( rnorm(300), nc=3 ) pairs(m, pch=20) or pairs(m, pch=.) See help(par) for more details. livia wrote: Hi, I would like to use the function pairs() to plot a matrix of scatterplots. For each scatterplot, the data are plotted in circles, can I add some argument to change the circles into dots? Could anyone give me some advice?Many thanks __ R-help@stat.math.ethz.ch 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/matrix-of-scatterplots-tf4067527.html#a11558687 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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] matrix of bins with different length
Dear users, please help to define the following data structure: I would like to have a matrix, where every element is a container of different size , containing real numbers. The containers (bins) are addressed by an index pair [i,j] (i is number of corresponding row of the matrix, j is the coloumn of the matrix). The containers are initially empty, I would like to fill them dynamically (put certain numbers into different bins in each iteration). I can not define a 3 dimensional array, because I don't know the length of the third dimension in advance, and because the vectors (containers) in the matrix are usually of different length. Any help greatly appreciated, Balazs Torma __ R-help@stat.math.ethz.ch 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] matrix of bins with different length
Try this: m - matrix(list(1, 1:2, 1:3, 1:4), 2) m[[1,1]] [1] 1 m[[2,1]] [1] 1 2 m [,1] [,2] [1,] 1 Integer,3 [2,] Integer,2 Integer,4 On 7/10/07, Balazs Torma [EMAIL PROTECTED] wrote: Dear users, please help to define the following data structure: I would like to have a matrix, where every element is a container of different size , containing real numbers. The containers (bins) are addressed by an index pair [i,j] (i is number of corresponding row of the matrix, j is the coloumn of the matrix). The containers are initially empty, I would like to fill them dynamically (put certain numbers into different bins in each iteration). I can not define a 3 dimensional array, because I don't know the length of the third dimension in advance, and because the vectors (containers) in the matrix are usually of different length. Any help greatly appreciated, Balazs Torma __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Matrix multiplication (with multidimensional array)
Hi All, I am wondering if there is an efficient way to do the following matrix multiplication, a[1,,] 1, 2 3, 4 a[2,,] 4, 3 2, 1 b[1,,]5,6 7,8 b[2,,]8,7 6,5 I need the result c, with c[1,,] = a[1,,] %*% b[1,,] c[2,,] = a[2,,] %*% b[2,,] Thanks in advance for any help. __ R-help@stat.math.ethz.ch 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] Matrix library, CHOLMOD error: problem too large
I have a pretty large sparse matrix of integers: dim(tasa) [1] 91650 37651 I need to add one to it in order to take logs, but I'm getting the following error: tasa = log(tasa + 1) CHOLMOD error: problem too large Error in asMethod(object) : Cholmod error `problem too large' I have 2 Gb of RAM, and the current workspace is barely 300mb. Is there any workaround to this? Anyone has any experience with this error? Thanks, -Jose -- Jose Quesada, PhD. http://www.andrew.cmu.edu/~jquesada __ R-help@stat.math.ethz.ch 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] Matrix library, CHOLMOD error: problem too large
On 6/22/2007 1:26 PM, Jose Quesada wrote: I have a pretty large sparse matrix of integers: dim(tasa) [1] 91650 37651 I need to add one to it in order to take logs, but I'm getting the following error: tasa = log(tasa + 1) CHOLMOD error: problem too large Error in asMethod(object) : Cholmod error `problem too large' I have 2 Gb of RAM, and the current workspace is barely 300mb. Is there any workaround to this? Anyone has any experience with this error? If tasa is sparse, then tasa+1 will not be sparse, so that's likely your problem. You might have better luck with log1p(tasa) if the authors of the Matrix package have written a method for log1p(); if not, you'll probably have to do it yourself. Duncan Murdoch __ R-help@stat.math.ethz.ch 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] Matrix *package*, CHOLMOD error: problem too large
[Jose, if you call the Matrix *package* library once more, ... GR! ..] DM == Duncan Murdoch [EMAIL PROTECTED] on Fri, 22 Jun 2007 14:04:03 -0400 writes: DM On 6/22/2007 1:26 PM, Jose Quesada wrote: I have a pretty large sparse matrix of integers: dim(tasa) [1] 91650 37651 I need to add one to it in order to take logs, but I'm getting the following error: tasa = log(tasa + 1) CHOLMOD error: problem too large Error in asMethod(object) : Cholmod error `problem too large' I have 2 Gb of RAM, and the current workspace is barely 300mb. Is there any workaround to this? Anyone has any experience with this error? DM If tasa is sparse, then tasa+1 will not be sparse, so DM that's likely your problem. [of course] DM You might have better luck with DM log1p(tasa) {very good point, thank you, Duncan!} DM if the authors of the Matrix package have written a DM method for log1p(); if not, you'll probably have to do DM it yourself. They have not yet. Note however that this - and expm1() - would automagically work for sparse matrices if these two functions were part of the Math S4 group generic. I'd say that there's only historical reason for them *not* to be part of Math, and I am likely going to propose to change this Martin Maechler DM Duncan Murdoch __ R-help@stat.math.ethz.ch 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] Matrix library error: should never happen; please report
Hi Jose, JQ == Jose Quesada [EMAIL PROTECTED] on Tue, 19 Jun 2007 21:12:53 +0200 writes: JQ Hi, I got the following error. Sorry but this time I JQ couldn't reproduce it with a simple chunk of code: .TM.repl.i.2col(): drop 'matrix' case ... Error in .nextMethod(x = x, i = i, j = j) : 'i' has no integer column number should never happen; please report In addition: Warning messages: 1: Ambiguous method selection for %*%, target ddiMatrix#dgCMatrix (the first of the signatures shown will be used) diagonalMatrix#CsparseMatrix ddenseMatrix#CsparseMatrix in: .findInheritedMethods(classes, fdef, mtable) JQ I got 4 other copies of the same warning. Will play JQ around a bit more... This is really strange. Yes, but - the Matrix library is the file Matrix.so or Matrix.dll which is part of the installed (aka binary) Matrix *package* Maybe you really need to read the result of fortune(package.*Maechler) # after installing package 'fortunes' - please report was not meant to say to report to R-help, but to the package maintainers, - since you cannot reproduce it yet, we cannot do much about it. It may be a bug in the Matrix package (and Jose has told me that he's using the latest released version 0.99875-2), but in theory it could even be your own mistake, namely by wrongly manipulating the slots of a Matrix object. Please try to produce an R script - even if not small -- with a reproducible example; [and then do report to [EMAIL PROTECTED] JQ Thanks -- Jose Quesada, PhD. Best regards, Martin Maechler, ETH Zurich __ R-help@stat.math.ethz.ch 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] Matrix library error: should never happen; please report
Hi, I got the following error. Sorry but this time I couldn't reproduce it with a simple chunk of code: .TM.repl.i.2col(): drop 'matrix' case ... Error in .nextMethod(x = x, i = i, j = j) : 'i' has no integer column number should never happen; please report In addition: Warning messages: 1: Ambiguous method selection for %*%, target ddiMatrix#dgCMatrix (the first of the signatures shown will be used) diagonalMatrix#CsparseMatrix ddenseMatrix#CsparseMatrix in: .findInheritedMethods(classes, fdef, mtable) I got 4 other copies of the same warning. Will play around a bit more... This is really strange. Thanks -- Jose Quesada, PhD. http://www.andrew.cmu.edu/~jquesada __ R-help@stat.math.ethz.ch 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] matrix and data frame
hello, I have just a question before the week end it's that I don't know how to do to paste matrixs and these matrix they have one same column and I'd like to paste its by this column and I wanna paste its not below but just at right side hand thanks good week end _ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] matrix and data frame
I'm not at all certain I understand your question, but try ?cbind Sarah On 6/8/07, elyakhlifi mustapha [EMAIL PROTECTED] wrote: hello, I have just a question before the week end it's that I don't know how to do to paste matrixs and these matrix they have one same column and I'd like to paste its by this column and I wanna paste its not below but just at right side hand thanks good week end -- Sarah Goslee http://www.functionaldiversity.org __ R-help@stat.math.ethz.ch 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] matrix in data.frame
Dear list! I have run into a problem that seems very simple but I can't find any solution to it (have searched the internet, help-files and An introduction to R etc without any luck). The problem is the following: I would like to create a data.frame with two components (columns), the first component being a matrix and the second component a vector. Whatever I have tried so far, I end up with a data.frame containing all the columns from the matrix plus the vector which is not what I am after. I have seen this kind of data.frame among R example datasets (oliveoil and yarn). I would greatly appreciate some help with this problem! Kind regards, Lina Hultin Rosenberg Karolinska Biomics Center Karolinska Institute Sweden __ R-help@stat.math.ethz.ch 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] matrix in data.frame
Have you thought of using a list? a - matrix(1:10, nrow=2) b - 1:5 x - list(a=a, b=b) x $a [,1] [,2] [,3] [,4] [,5] [1,]13579 [2,]2468 10 $b [1] 1 2 3 4 5 x$a [,1] [,2] [,3] [,4] [,5] [1,]13579 [2,]2468 10 x$b [1] 1 2 3 4 5 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Lina Hultin-Rosenberg Sent: 30 May 2007 10:26 To: r-help@stat.math.ethz.ch Subject: [R] matrix in data.frame Dear list! I have run into a problem that seems very simple but I can't find any solution to it (have searched the internet, help-files and An introduction to R etc without any luck). The problem is the following: I would like to create a data.frame with two components (columns), the first component being a matrix and the second component a vector. Whatever I have tried so far, I end up with a data.frame containing all the columns from the matrix plus the vector which is not what I am after. I have seen this kind of data.frame among R example datasets (oliveoil and yarn). I would greatly appreciate some help with this problem! Kind regards, Lina Hultin Rosenberg Karolinska Biomics Center Karolinska Institute Sweden __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix in data.frame
You need to use I() or something similar. E.g. A - matrix(1:6, 2,3) data.frame(x=1:2, I(A)) X - data.frame(x=1:2) X$A - A both insert A as a single column. On Wed, 30 May 2007, Lina Hultin-Rosenberg wrote: Dear list! I have run into a problem that seems very simple but I can't find any solution to it (have searched the internet, help-files and An introduction to R etc without any luck). The problem is the following: I would like to create a data.frame with two components (columns), the first component being a matrix and the second component a vector. Whatever I have tried so far, I end up with a data.frame containing all the columns from the matrix plus the vector which is not what I am after. I have seen this kind of data.frame among R example datasets (oliveoil and yarn). I would greatly appreciate some help with this problem! Kind regards, Lina Hultin Rosenberg Karolinska Biomics Center Karolinska Institute Sweden -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch 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] matrix in data.frame
Thank you so much for your help, it worked of course! Best regards, Lina Hultin-Rosenberg -Ursprungligt meddelande- Från: Prof Brian Ripley [mailto:[EMAIL PROTECTED] Skickat: den 30 maj 2007 12:44 Till: Lina Hultin-Rosenberg Kopia: r-help@stat.math.ethz.ch Ämne: Re: [R] matrix in data.frame You need to use I() or something similar. E.g. A - matrix(1:6, 2,3) data.frame(x=1:2, I(A)) X - data.frame(x=1:2) X$A - A both insert A as a single column. On Wed, 30 May 2007, Lina Hultin-Rosenberg wrote: Dear list! I have run into a problem that seems very simple but I can't find any solution to it (have searched the internet, help-files and An introduction to R etc without any luck). The problem is the following: I would like to create a data.frame with two components (columns), the first component being a matrix and the second component a vector. Whatever I have tried so far, I end up with a data.frame containing all the columns from the matrix plus the vector which is not what I am after. I have seen this kind of data.frame among R example datasets (oliveoil and yarn). I would greatly appreciate some help with this problem! Kind regards, Lina Hultin Rosenberg Karolinska Biomics Center Karolinska Institute Sweden -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch 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] Matrix package: writeMM
On 5/15/07, Jose Quesada [EMAIL PROTECTED] wrote: Hi, I'm finding that readMM() cannot read a file written with writeMM(). Example: library(Matrix) a = Matrix(c(1,0,3,0,0,5), 10, 10) a = as(a, CsparseMatrix) writeMM(a, kk.mm) b = readMM(kk.mm) Error in validObject(.Object) : invalid class dgTMatrix object: all row indices must be between 0 and nrow-1 You're right (and thanks for including a reproducible example). The writeMM function is writing 0-based indices when they should be 1-based. Thanks for bringing this to our attention. It's rather embarrassing that we didn't create such a test and discover it for ourselves. This will be fixed in the next release. __ R-help@stat.math.ethz.ch 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] Matrix package: writeMM
Hi, I'm finding that readMM() cannot read a file written with writeMM(). Example: library(Matrix) a = Matrix(c(1,0,3,0,0,5), 10, 10) a = as(a, CsparseMatrix) writeMM(a, kk.mm) b = readMM(kk.mm) Error in validObject(.Object) : invalid class dgTMatrix object: all row indices must be between 0 and nrow-1 Thoughts? Thanks, -Jose -- Jose Quesada, PhD. http://www.andrew.cmu.edu/~jquesada __ R-help@stat.math.ethz.ch 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] Matrix column name
Dear R users, Having searched the mail archive I think the conclusion was that it is not possible to have a column name when there is only one column in the matrix. But I thought I'd check with the more experienced users. What I tried to do was: in a loop I pick a column, record the column name and remove the column from the matrix. But when there were 2 columns left, after one column was removed, the last column name disappeared by default. It means that I always miss out the last column. I tried this by hand: matrix.a 801 802 803 [1,] -0.0906346 0.0906346 0.0906346 [2,] -0.0804911 0.0804911 0.0804911 [3,] -0.0703796 0.0703796 0.0703796 matrix.a-as.matrix(matrix.a[,-1]) matrix.a 802 803 [1,] 0.0906346 0.0906346 [2,] 0.0804911 0.0804911 [3,] 0.0703796 0.0703796 matrix.a-as.matrix(matrix.a[,-1]) matrix.a [,1] [1,] 0.0906346 [2,] 0.0804911 [3,] 0.0703796 Is there a way to force the column name to remain in such a case? Thanks, Alex sessionInfo() R version 2.4.1 (2006-12-18) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base Alex Lam PhD student Department of Genetics and Genomics Roslin Institute (Edinburgh) Roslin Midlothian EH25 9PS Great Britain Phone +44 131 5274471 Web http://www.roslin.ac.uk Roslin Institute is a company limited by guarantee, registered in Scotland (registered number SC157100) and a Scottish Charity (registered number SC023592). Our registered office is at Roslin, Midlothian, EH25 9PS. VAT registration number 847380013. The information contained in this e-mail (including any attachments) is confidential and is intended for the use of the addressee only. The opinions expressed within this e-mail (including any attachments) are the opinions of the sender and do not necessarily constitute those of Roslin Institute (Edinburgh) (the Institute) unless specifically stated by a sender who is duly authorised to do so on behalf of the Institute __ R-help@stat.math.ethz.ch 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] Matrix column name
On Tue, 2007-05-01 at 18:03 +0100, alex lam (RI) wrote: Dear R users, Having searched the mail archive I think the conclusion was that it is not possible to have a column name when there is only one column in the matrix. But I thought I'd check with the more experienced users. What I tried to do was: in a loop I pick a column, record the column name and remove the column from the matrix. But when there were 2 columns left, after one column was removed, the last column name disappeared by default. It means that I always miss out the last column. See R FAQ 7.5 Why do my matrices lose dimensions: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-dimensions_003f which has some examples, along with ?Extract To wit: MAT - matrix(1:12, ncol = 3) colnames(MAT) - LETTERS[1:3] MAT A B C [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 MAT[, 1] [1] 1 2 3 4 MAT[, 1, drop = FALSE] A [1,] 1 [2,] 2 [3,] 3 [4,] 4 HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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] Matrix column name
You seem to be looking for matrix.a[,-1, drop = TRUE] On Tue, 1 May 2007, alex lam (RI) wrote: Dear R users, Having searched the mail archive I think the conclusion was that it is not possible to have a column name when there is only one column in the matrix. But I thought I'd check with the more experienced users. What I tried to do was: in a loop I pick a column, record the column name and remove the column from the matrix. But when there were 2 columns left, after one column was removed, the last column name disappeared by default. It means that I always miss out the last column. And the matrix became a vector. I tried this by hand: matrix.a 801 802 803 [1,] -0.0906346 0.0906346 0.0906346 [2,] -0.0804911 0.0804911 0.0804911 [3,] -0.0703796 0.0703796 0.0703796 matrix.a-as.matrix(matrix.a[,-1]) matrix.a 802 803 [1,] 0.0906346 0.0906346 [2,] 0.0804911 0.0804911 [3,] 0.0703796 0.0703796 matrix.a-as.matrix(matrix.a[,-1]) matrix.a [,1] [1,] 0.0906346 [2,] 0.0804911 [3,] 0.0703796 Is there a way to force the column name to remain in such a case? Thanks, Alex sessionInfo() R version 2.4.1 (2006-12-18) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base Alex Lam PhD student Department of Genetics and Genomics Roslin Institute (Edinburgh) Roslin Midlothian EH25 9PS Great Britain Phone +44 131 5274471 Web http://www.roslin.ac.uk -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch 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] Matrix: how to re-use the symbolic Cholesky factorization?
I have been playing around with sparse matrices in the Matrix package, in particularly with the Cholesky factorization of matrices of class dsCMatrix. And BTW, what a fantastic package. My problem is that I have to carry out repeated Cholesky factorization of a spares symmetric matrices, say Q_1, Q_2, ...,Q_n, where the Q's have the same non-zero pattern. I know in this case one does only need to carry out the symbolic factorization _once_ and then follow that up with a numerical factorization for each of the Q_i's (re-using the general symbolic factorization each time). Does anybody know if this is possible using the Matrix package? Thanks, Gardar Johannesson Lawrence Livermore National Laboratory __ R-help@stat.math.ethz.ch 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] Matrix: how to re-use the symbolic Cholesky factorization?
On 4/24/07, Gardar Johannesson [EMAIL PROTECTED] wrote: I have been playing around with sparse matrices in the Matrix package, in particularly with the Cholesky factorization of matrices of class dsCMatrix. And BTW, what a fantastic package. My problem is that I have to carry out repeated Cholesky factorization of a spares symmetric matrices, say Q_1, Q_2, ...,Q_n, where the Q's have the same non-zero pattern. I know in this case one does only need to carry out the symbolic factorization _once_ and then follow that up with a numerical factorization for each of the Q_i's (re-using the general symbolic factorization each time). Does anybody know if this is possible using the Matrix package? At present that is not possible without writing your own C code that calls functions in the CHOLMOD library of C functions directly. We'll add that to the ToDo list. The easiest interface I can picture is to pass a Cholesky factorization object along with the dsCMatrix object that contains the new values with the old pattern. __ R-help@stat.math.ethz.ch 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] Matrix or grid conversion of spatial data
Marco, I've done something similar with spatial data. I defined the points as SpatialPoints, the grid as SpatialGrid (using the sp package). Then table(overlay(grid, points)) will give you the number of points inside each gridcell. library(sp) points - SpatialPoints(your.data.frame) cellsize - 1 cellcentre.offset - bbox(points)[, 1] coords.span - diff(t(bbox(points))) cells.dim - ceiling(coords.span / cellsize) grid - SpatialGrid(GridTopology(cellcentre.offset, rep(cellsize, nrow(bbox(points))), cells.dim)) in.cell - overlay(grid, points) table(in.cell) Cheers, Thierry PS R-sig-geo is a better list to ask spatial releated questions. ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 [EMAIL PROTECTED] www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Marco Visser Verzonden: woensdag 18 april 2007 20:57 Aan: r-help@stat.math.ethz.ch Onderwerp: [R] Matrix or grid conversion of spatial data Dear Happy R-users experts, I am in need of advice, While working with spatial data (x y coordinates of seed locations) I have come accross the problem that I need to convert my point data into a matrix or grid system. I then need to count how often a point falls into a certain position in the matrix or grid. I have searched all day online, asked collegeas but nothing works. Sadly my R box of tricks has run out. My (point) data looks like this; x y 2.34.5 3.4 0.2 and continues for another million records. Now my question; is there any function that is able to count how often a point falls into a grid based on the x and y location? So I need to discretize the spatial locations to a regular grid and then counting how often a point occurs. Many thanks for your thoughts on this problem. Marco Visser __ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix building with two variables
Dear R-Experts, I have two variables coming from my calculations: Tissues -- WT sun41 Revertante NullPoint -- 0.826 0.871 0.859 Now I want to build a matrix, where row1=Tissues and row2=NullPoint is. How can I realize this? Thanks, Corinna __ R-help@stat.math.ethz.ch 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] Matrix or grid conversion of spatial data
--- Marco Visser [EMAIL PROTECTED] wrote: Dear Happy R-users experts, I am in need of advice, While working with spatial data (x y coordinates of seed locations) I have come accross the problem that I need to convert my point data into a matrix or grid system. I then need to count how often a point falls into a certain position in the matrix or grid. I have searched all day online, asked collegeas but nothing works. Sadly my R box of tricks has run out. My (point) data looks like this; x y 2.34.5 3.4 0.2 and continues for another million records. Now my question; is there any function that is able to count how often a point falls into a grid based on the x and y location? So I need to discretize the spatial locations to a regular grid and then counting how often a point occurs. Many thanks for your thoughts on this problem. Marco Visser Would something like ?crossprod do it? __ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix building with two variables
Now I want to build a matrix, where row1=Tissues and row2=NullPoint is. How can I realize this? ?rbind It seems you ask a lot of questions these times on the list. Maybe you should read the R mailing lists posting guide, it contains useful resources which could help you to find the answers by yourself. http://www.r-project.org/posting-guide.html -- Julien __ R-help@stat.math.ethz.ch 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] matrix building with two variables
Thanks. I did read everything I could but could not understand everything. Hopefully with more programming practice it will become more less. Corinna -Ursprüngliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Julien Barnier Gesendet: Donnerstag, 19. April 2007 14:02 An: r-help@stat.math.ethz.ch Betreff: Re: [R] matrix building with two variables Now I want to build a matrix, where row1=Tissues and row2=NullPoint is. How can I realize this? ?rbind It seems you ask a lot of questions these times on the list. Maybe you should read the R mailing lists posting guide, it contains useful resources which could help you to find the answers by yourself. http://www.r-project.org/posting-guide.html -- Julien __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix building with two variables
Hi, I did read everything I could but could not understand everything. Hopefully with more programming practice it will become more less. Then maybe you read everything you could a bit too fast. Because the answer to your question is in the first document to read, An introduction to R, section 5.8 : http://cran.r-project.org/doc/manuals/R-intro.html#Forming-partitioned-matrices -- Julien __ R-help@stat.math.ethz.ch 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] matrix building with two variables
2007/4/19, Schmitt, Corinna [EMAIL PROTECTED]: Thanks. I did read everything I could but could not understand everything. Hopefully with more programming practice it will become more less. Here is a great book: Uwe Ligges: Programmieren mit R. worth every Rappen :-) __ R-help@stat.math.ethz.ch 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] Matrix or grid conversion of spatial data
Dear Happy R-users experts, I am in need of advice, While working with spatial data (x y coordinates of seed locations) I have come accross the problem that I need to convert my point data into a matrix or grid system. I then need to count how often a point falls into a certain position in the matrix or grid. I have searched all day online, asked collegeas but nothing works. Sadly my R box of tricks has run out. My (point) data looks like this; x y 2.34.5 3.4 0.2 and continues for another million records. Now my question; is there any function that is able to count how often a point falls into a grid based on the x and y location? So I need to discretize the spatial locations to a regular grid and then counting how often a point occurs. Many thanks for your thoughts on this problem. Marco Visser __ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] Matrix or grid conversion of spatial data
On Wed, 18 Apr 2007, Marco Visser wrote: Dear Happy R-users experts, I am in need of advice, While working with spatial data (x y coordinates of seed locations) I have come accross the problem that I need to convert my point data into a matrix or grid system. I then need to count how often a point falls into a certain position in the matrix or grid. I have searched all day online, asked collegeas but nothing works. Sadly my R box of tricks has run out. My (point) data looks like this; x y 2.34.5 3.4 0.2 and continues for another million records. Now my question; is there any function that is able to count how often a point falls into a grid based on the x and y location? So I need to discretize the spatial locations to a regular grid and then counting how often a point occurs. see ?table and ?cut Maybe something like x.breakpoints - sensible breakpoints for x y.breakpoints - sensible breakpoints for y my.grid - table( cut( x, x.breakpoints ), cut( y, y.breakpoints ) ) see also ?xtab and ?quantile Many thanks for your thoughts on this problem. Marco Visser __ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0901 __ R-help@stat.math.ethz.ch 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] Matrix manipulation
Hi, This is a very basic question, but apparently I am too stupid for it. I have a large matrix A, and I need to avoid for loops. How could I apply a function f(a,r,c) on each element of A, using the subscript (row and column) of a as the other arguments? Thanks in advance, Markku Karhunen National Public Health Institute, Finland __ R-help@stat.math.ethz.ch 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] Matrix manipulation
It would be helpful if you could be more specific of what exactly you'd like to compute. Have a look also at the posting guide available at: http://www.R-project.org/posting-guide.html Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Markku Karhunen [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Monday, April 16, 2007 2:52 PM Subject: [R] Matrix manipulation Hi, This is a very basic question, but apparently I am too stupid for it. I have a large matrix A, and I need to avoid for loops. How could I apply a function f(a,r,c) on each element of A, using the subscript (row and column) of a as the other arguments? Thanks in advance, Markku Karhunen National Public Health Institute, Finland __ R-help@stat.math.ethz.ch 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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] Matrix manipulation
[EMAIL PROTECTED] napsal dne 16.04.2007 14:52:55: Hi, This is a very basic question, but apparently I am too stupid for it. I have a large matrix A, and I need to avoid for loops. How could I apply a function f(a,r,c) on each element of A, using the subscript (row and column) of a as the other arguments? Hi fff-function(a,b,c) a*b+c x-1:12 dim(x)-c(3,4) x [,1] [,2] [,3] [,4] [1,]147 10 [2,]258 11 [3,]369 12 fff(x, col(x), row(x)) [,1] [,2] [,3] [,4] [1,]29 22 41 [2,]4 12 26 46 [3,]6 15 30 51 works. However from your function description is really tough to understand what the function really does so maybe this is not what you expected. Regards Petr Thanks in advance, Markku Karhunen National Public Health Institute, Finland __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Matrix manipulation
Agreed. What Petr Pikal wrote, works exactly, so thank you all! Best, Markku It would be helpful if you could be more specific of what exactly you'd like to compute. Have a look also at the posting guide available at: http://www.R-project.org/posting-guide.html Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Markku Karhunen [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Monday, April 16, 2007 2:52 PM Subject: [R] Matrix manipulation Hi, This is a very basic question, but apparently I am too stupid for it. I have a large matrix A, and I need to avoid for loops. How could I apply a function f(a,r,c) on each element of A, using the subscript (row and column) of a as the other arguments? Thanks in advance, Markku Karhunen National Public Health Institute, Finland __ R-help@stat.math.ethz.ch 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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] matrix construction
Hallo, can anyone tell me how I can create a matrix in R? I have two arrays A = c(0:3), B=c(0:3). C should be the matrix. I just found the description that a matrix is just an array with two substricpts. Thanks, Corinna __ R-help@stat.math.ethz.ch 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] matrix construction
Have a look at ?cbind, ?rbind and ?matrix C - cbind(A, B) C - rbind(A, B) C - matrix(c(0:3, 0:3), ncol = 2) Cheers, Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 [EMAIL PROTECTED] www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Schmitt, Corinna Verzonden: maandag 26 maart 2007 11:34 Aan: r-help@stat.math.ethz.ch Onderwerp: [R] matrix construction Hallo, can anyone tell me how I can create a matrix in R? I have two arrays A = c(0:3), B=c(0:3). C should be the matrix. I just found the description that a matrix is just an array with two substricpts. Thanks, Corinna __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix similarity comparison
Good morning to you all, I have a problem with a set of matrices that I want to compare. I want to see the similarity between them, and to be able to extract the differences between them. They have all the same number of columns and rows, and correspond presence absence data: for example: m1 - matrix(c(1,0,0,0,1,0,1,1,1,1,1,1), 3,4) m2 - matrix(c(1,0,1,0,1,0,0,1,0,1,0,1), 3,4) I tried with the function cor2m() [package=edodist] but it didn't worked and my matrices are much bigger than the ones from the example. Thank you, Carlos -- Carlos GUERRA Gabinete de Sistemas de Informacao Geografica Escola Superior Agraria de Ponte de Lima Mosteiro de Refoios do Lima 4990-706 Ponte de Lima Tlm: +351 91 2407109 Tlf: +351 258 909779 Reclaim your Inbox...!!! http://www.mozilla.org/products/thunderbird/ __ R-help@stat.math.ethz.ch 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] matrix similarity comparison
hi Carlos, its not really clear what you're asking here. If all you want is to see what entries are the same and which are different between two matrices of the same dimensions, then this does it: #same m1==m2 #diff m1 != m2 If you want to extract the ones that are the same, indx - m1==m2 indx [,1] [,2] [,3] [,4] [1,] TRUE TRUE FALSE TRUE [2,] TRUE TRUE TRUE FALSE [3,] FALSE TRUE FALSE TRUE m1[indx] 1 0 0 1 0 1 1 1 On 3/19/07, Carlos Guerra [EMAIL PROTECTED] wrote: Good morning to you all, I have a problem with a set of matrices that I want to compare. I want to see the similarity between them, and to be able to extract the differences between them. They have all the same number of columns and rows, and correspond presence absence data: for example: m1 - matrix(c(1,0,0,0,1,0,1,1,1,1,1,1), 3,4) m2 - matrix(c(1,0,1,0,1,0,0,1,0,1,0,1), 3,4) I tried with the function cor2m() [package=edodist] but it didn't worked and my matrices are much bigger than the ones from the example. Thank you, Carlos -- Carlos GUERRA Gabinete de Sistemas de Informacao Geografica Escola Superior Agraria de Ponte de Lima Mosteiro de Refoios do Lima 4990-706 Ponte de Lima Tlm: +351 91 2407109 Tlf: +351 258 909779 Reclaim your Inbox...!!! http://www.mozilla.org/products/thunderbird/ __ R-help@stat.math.ethz.ch 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. -- Matthew C Keller Postdoctoral Fellow Virginia Institute for Psychiatric and Behavioral Genetics __ R-help@stat.math.ethz.ch 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] matrix similarity comparison
Hi Carlos, I want to see the similarity between them, and to be able to extract the differences between them. You need to explain a bit more. Are you looking for number of elements in common? How are your data set up? (eg species as columns and sites as rows) One way to get number of joint presences is this (although there are certainly more elegant ways), assuming that columns are species m1 - matrix(c(1,0,0,0,1,0,1,1,1,1,1,1), 3,4) m2 - matrix(c(1,0,1,0,1,0,0,1,0,1,0,1), 3,4) apply((m1 + m2), 1, function(x)sum(x == 2)) [1] 2 2 1 I tried with the function cor2m() [package=edodist] but it didn't worked and my matrices are much bigger than the ones from the example. Didn't work? That's a bit vague, but anyway this won't help, since cor2m is intended for use with a matrix of environmental variables as the second matrix, and not for binary data (the first matrix can be binary). I doubt that correlations are really the measure you want anyway - if they are, then you can use simply cor(m1, m2) Sarah -- Sarah Goslee http://www.functionaldiversity.org __ R-help@stat.math.ethz.ch 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] matrix similarity comparison
On 3/19/07, Sarah Goslee [EMAIL PROTECTED] wrote: Hi Carlos, I want to see the similarity between them, and to be able to extract the differences between them. You need to explain a bit more. Are you looking for number of elements in common? How are your data set up? (eg species as columns and sites as rows) I thought of something else. If you want similarity, you could always do m12.dist - dist(cbind(m1, m2), binary) and just look at the elements of the result that correspond to columns of m1 vs m2 (rather than m1 vs m2 or m2 vs m2). Sarah -- Sarah Goslee http://www.functionaldiversity.org __ R-help@stat.math.ethz.ch 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] matrix similarity comparison
Let me see if I can explain my problem better, I have: m1 - matrix(c(1,0,0,0,1,0,1,1,1,1,1,1), 3, 4) rownames(m1) - c(station1, station2, station3) colnames(m1) - c(A,B,C,D) m2 - matrix(c(1,0,1,1,1,0,1,1,0,0,1,1), 3, 4) rownames(m2) - c(station1, station2, station3) colnames(m2) - c(A,B,C,D) ... and I want to: - find the correlation between the two matrices - for each station, extract the names of the columns that don't match in the two matrices Thanks for the previous comments, Carlos -- Carlos GUERRA Gabinete de Sistemas de Informacao Geografica Escola Superior Agraria de Ponte de Lima Mosteiro de Refoios do Lima 4990-706 Ponte de Lima Tlm: +351 91 2407109 Tlf: +351 258 909779 Reclaim your Inbox...!!! http://www.mozilla.org/products/thunderbird/ __ R-help@stat.math.ethz.ch 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] Matrix conversion question
Hello, Please help - I'm blanking on this ... I have a matrix like this: [,1] [,2] [1,]12 [2,]13 [3,]23 and would like to have a list of vectors, where a vector contains the entries in a matrix row ... Can somebody nudge me to the place I need to go? Thanks, Joh __ R-help@stat.math.ethz.ch 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] Matrix conversion question
Try split(x, row(x)) -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Johannes Graumann Sent: Friday, March 09, 2007 10:30 AM To: r-help@stat.math.ethz.ch Subject: [R] Matrix conversion question Hello, Please help - I'm blanking on this ... I have a matrix like this: [,1] [,2] [1,]12 [2,]13 [3,]23 and would like to have a list of vectors, where a vector contains the entries in a matrix row ... Can somebody nudge me to the place I need to go? Thanks, Joh __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Matrix conversion question
Christos Hatzis wrote: Try split(x, row(x)) H! THE ELEGANCE! Thanks a lot! Joh __ R-help@stat.math.ethz.ch 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] Matrix/dataframe indexing
On Mon, 2007-03-05 at 12:49 -0500, Guenther, Cameron wrote: Hi all, I am hoping someone can help me out with this: If I have dataframe of years and ages and the first column and first row are filled with leading values: Df- age1age2age3 Yr1 1 0.4 0.16 Yr2 1.5 0 0 Yr3 0.9 0 0 Yr4 1 0 0 Yr5 1.2 0 0 Yr6 1.4 0 0 Yr7 0.8 0 0 Yr8 0.6 0 0 Yr9 1.1 0 0 Now the rest of the cells need to be filled according to the previous year and age cell so arbitrarily, cell [2,2] should be value in cell [1,1] * exp(0.3), and cell [2,3] should be the value in cell [1,2]* exp(0.3), etc. How do I write the for loop so that it will calculate the missing cell values over both dimensions of the dataframe? Thanks in advance Cameron, I have not seen a reply to this, but one of the problems that you can run into is that, depending upon the approach, you can execute the manipulation on the second column, in effect, before the first column in the actual source matrix has been updated, due to object subsetting and copying. So, my knee jerk reaction here is to simply do this in two lines of code, one on the first column and then a separate line for the second column. I think that this is what you want as an end result: DF age1 age2 age3 Yr1 1.0 0.4 0.16 Yr2 1.5 0.0 0.00 Yr3 0.9 0.0 0.00 Yr4 1.0 0.0 0.00 Yr5 1.2 0.0 0.00 Yr6 1.4 0.0 0.00 Yr7 0.8 0.0 0.00 Yr8 0.6 0.0 0.00 Yr9 1.1 0.0 0.00 DF[-1, 2] - DF[-9, 1] * exp(0.3) DF age1 age2 age3 Yr1 1.0 0.400 0.16 Yr2 1.5 1.3498588 0.00 Yr3 0.9 2.0247882 0.00 Yr4 1.0 1.2148729 0.00 Yr5 1.2 1.3498588 0.00 Yr6 1.4 1.6198306 0.00 Yr7 0.8 1.8898023 0.00 Yr8 0.6 1.0798870 0.00 Yr9 1.1 0.8099153 0.00 DF[-1, 3] - DF[-9, 2] * exp(0.3) DF age1 age2 age3 Yr1 1.0 0.400 0.160 Yr2 1.5 1.3498588 0.5399435 Yr3 0.9 2.0247882 1.8221188 Yr4 1.0 1.2148729 2.7331782 Yr5 1.2 1.3498588 1.6399069 Yr6 1.4 1.6198306 1.8221188 Yr7 0.8 1.8898023 2.1865426 Yr8 0.6 1.0798870 2.5509663 Yr9 1.1 0.8099153 1.4576950 I think that the risk inherent in R sometimes is that there can be a tendency to 'overthink' a problem in either trying to vectorize a function or in trying to create (or avoid) a loop, when individual code statements can just get the job done quickly and simply, and in many cases be more 'readable'. If this was something where you were going to do this repeatedly and needed to create a function to generalize the approach to matrices where the dimensions are not known a priori, then it might be worthwhile to encapsulate the above in a function where dims can be checked, etc. HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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] Matrix/dataframe indexing
Hi all, I am hoping someone can help me out with this: If I have dataframe of years and ages and the first column and first row are filled with leading values: Df-age1age2age3 Yr1 1 0.4 0.16 Yr2 1.5 0 0 Yr3 0.9 0 0 Yr4 1 0 0 Yr5 1.2 0 0 Yr6 1.4 0 0 Yr7 0.8 0 0 Yr8 0.6 0 0 Yr9 1.1 0 0 Now the rest of the cells need to be filled according to the previous year and age cell so arbitrarily, cell [2,2] should be value in cell [1,1] * exp(0.3), and cell [2,3] should be the value in cell [1,2]* exp(0.3), etc. How do I write the for loop so that it will calculate the missing cell values over both dimensions of the dataframe? Thanks in advance Cameron Guenther, Ph.D. 100 8th Ave. SE St. Petersburg, Fl 33701 727-896-8626 ext. 4305 [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch 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] Matrix looping
Hi all, I am having a problem getting my fucntion to work correctly. Here is my problem. I have three ages: Nage-c(1,2,3) I have an weight matrix: Wt-c( 0.04952867, 0.23808432, 0.34263880) I have an age schedule of maturity: Mat-c(0,1,1) where 0 is not mature, and 1 is mature I have a vulnerability schedule: Vul-c(0,1,1) I have an survivorship schedule: Survship-c(1,0.4,0.16) I also have leading parameters R0-130.66; recK-3.068; a-5.48; b-0.0282; S-0.4 I have annual catches for 100 years, ct-runif(100,5,20) Now I want a matrix of 100 years x 3 ages yr-c(1:100) Nt-matrix(0,nrow=length(yr)+1),ncol=length(Nage)) Now the first row of my matrix needs to be the product of R0 and Survship, no problem Nt[1,]-Ro*Survship I also need to create a new vector of egg production so Eggs-vector();Eggs[1]-sum(Mat*Nt[1,]) I also calculate the vulnerable biomass for each year so VulBio-vector();VulBio[1]-sum(Wt*Vul*Nt[1,]) Now I calculate the exploitation Ut-min(0.99,ct[1]/VulBio[1]) For (i in 1:length(Nt[,1])){ Now I need to calculate the first column of values where; Nt[i+1,1]-a*Eggs[i]/(1+b*Eggs[i]) Eggs[i+1]-sum(Mat*Nt[i+1,] VulBio[i+1]-sum(Wt*Vul*Nt[i+1,]) Ut[i+1]-min(0.99,ct[i+1]/VulBio[i+1]) Now here is the rub, I need to calculate the rest of the matrix based on the previous years values for the previous age, but I don't want to overwrite the first column or first row. Nt[i+1,i+1]-Nt[i,i]*S*(1-Ut[i]*Vul[i] gives the diagonal. How do I fill the matrix without overwriting colum and row 1? Cameron Guenther, Ph.D. 100 8th Ave. SE St. Petersburg, Fl 33701 727-896-8626 ext. 4305 [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch 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] Matrix Library failing to load
Dear R users I am trying to load the package lmer4 but it seems the Matrix library is failing to load and giving me the following error message : library(Matrix) Error in importIntoEnv(impenv, impnames, ns, impvars) : object 'Logic' is not exported by 'namespace:methods' Error: package/namespace load failed for 'Matrix' I have even reinstalled the Matrix package but it seems the problem is not going away. Regards, Luwis Diya Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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] matrix manipulations
Dear friends, I have a basic question with R. I'm generating a set of random variables and then combining them using the cbind statement. The code for that is given below. for (i in 1:100) { y - rpois(i,lambda=10) X0 - seq(1,1,length=i) X1 - rnorm(i,mean=5,sd=10) X2 - rnorm(i,mean=17,sd=12) X3 - rnorm(i,mean=3, sd=24) ind - rep(1:5,20) } data100 - cbind(y,X0,X1,X2,X3,ind) but when i look at the data100 table, the y values now take the observation count. (ie) the data under Y is not the poisson random generates but the observation number. Hence the last vector (ind) does not have a header. Is there any way i can drop the number of observation counts being added into the matrix. Thanks in advance for your help. Sincerely Anup However bad life may seem, there is always something you can do and succeed at. While there is life, there is hope. Stephen Hawking Anup Menon Nandialath * http://www.soundclick.com/bands/7/tailgunner_music.htm * * Looking for earth-friendly autos? Browse Top Cars by Green Rating at Yahoo! Autos' Green Center. __ R-help@stat.math.ethz.ch 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] matrix manipulations
Anup Nandialath wrote: Dear friends, I have a basic question with R. I'm generating a set of random variables and then combining them using the cbind statement. The code for that is given below. for (i in 1:100) { y - rpois(i,lambda=10) X0 - seq(1,1,length=i) X1 - rnorm(i,mean=5,sd=10) X2 - rnorm(i,mean=17,sd=12) X3 - rnorm(i,mean=3, sd=24) ind - rep(1:5,20) } data100 - cbind(y,X0,X1,X2,X3,ind) but when i look at the data100 table, the y values now take the observation count. (ie) the data under Y is not the poisson random generates but the observation number. Hence the last vector (ind) does not have a header. Is there any way i can drop the number of observation counts being added into the matrix. That is not what is going on. Sounds like you have your column labels misaligned with the column contents. Take a look at m - matrix(rnorm(4), 2, 2) m colnames(m) - c(a, b) m dim(m) (what was that for loop supposed to be good for, by the way?) __ R-help@stat.math.ethz.ch 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] matrix manipulations
Anup Menon Nandialath wrote: I have a basic question with R. I'm generating a set of random variables and then combining them using the cbind statement. The code for that is given below. for (i in 1:100) { y - rpois(i,lambda=10) X0 - seq(1,1,length=i) X1 - rnorm(i,mean=5,sd=10) X2 - rnorm(i,mean=17,sd=12) X3 - rnorm(i,mean=3, sd=24) ind - rep(1:5,20) } data100 - cbind(y,X0,X1,X2,X3,ind) First, why the loop? For i in 1:99, this code is a waste of computer time. The code should be: i - 100 y - rpois(i,lambda=10) X0 - seq(1,1,length=i) X1 - rnorm(i,mean=5,sd=10) X2 - rnorm(i,mean=17,sd=12) X3 - rnorm(i,mean=3, sd=24) ind - rep(1:5,20) data100 - cbind(y,X0,X1,X2,X3,ind) but when i look at the data100 table, the y values now take the observation count. The y values should be the same as y. y is a (random) array of integers. Alberto Monteiro __ R-help@stat.math.ethz.ch 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] matrix manipulations
Don't know what's wrong - it works: data100 y X0 X1 X2 X3 ind [1,] 11 1 2.79581511 -23.5477 -33.6123061 1 [2,] 8 1 21.43289242 21.52826214 3.8415209 2 [3,] 6 1 6.18688631 21.51057247 -50.5547410 3 [4,] 12 1 -5.95172686 13.74167916 -16.1798745 4 snip data100[y] [1] 9 10 8 18 10 10 9 7 11 11 7 10 9 14 7 18 9 7 10 10 18 10 9 11 9 10 11 11 11 10 8 9 8 7 18 9 11 15 7 [40] 10 7 9 7 18 11 11 18 11 11 18 7 10 11 7 11 10 18 7 9 9 9 7 7 7 14 18 18 14 11 12 11 8 15 7 7 9 18 14 [79] 10 7 7 12 7 7 9 8 7 8 11 7 15 18 18 7 15 7 7 11 11 10 These can pretty well be Poisson(10) variates... Remind that R is case-sensitive in names, y and Y is not the same. However, I really hope you are not using the code given below since it is, well... very original... to generate something 100 times and to keep just the last value at the end. Petr Anup Nandialath napsal(a): Dear friends, I have a basic question with R. I'm generating a set of random variables and then combining them using the cbind statement. The code for that is given below. for (i in 1:100) { y - rpois(i,lambda=10) X0 - seq(1,1,length=i) X1 - rnorm(i,mean=5,sd=10) X2 - rnorm(i,mean=17,sd=12) X3 - rnorm(i,mean=3, sd=24) ind - rep(1:5,20) } data100 - cbind(y,X0,X1,X2,X3,ind) but when i look at the data100 table, the y values now take the observation count. (ie) the data under Y is not the poisson random generates but the observation number. Hence the last vector (ind) does not have a header. Is there any way i can drop the number of observation counts being added into the matrix. Thanks in advance for your help. Sincerely Anup -- Petr Klasterecky Dept. of Probability and Statistics Charles University in Prague Czech Republic __ R-help@stat.math.ethz.ch 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] Matrix manipulation
Hi, let's say I have this A = matrix(c(1, 2, 4), nrow=1) colnames(A)=c(YOO1, YOO2, YOO3) # ie # YOO1 YOO2 YOO3 #[1,]124 HELLO - NULL HELLO$YOO1=BOO HELLO$YOO2=BOO HELLO$YOO3=HOO and I want a matrix that will sum my categorization.. how can I do it efficiently without any loop? #ie BOO HOO #[1,] 3 4 Thanks a lot! -- View this message in context: http://www.nabble.com/Matrix-manipulation-tf3223616.html#a8953806 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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] Matrix manipulation
Hi, let's say I have this A = matrix(c(1, 2, 4), nrow=1) colnames(A)=c(YOO1, YOO2, YOO3) # ie # YOO1 YOO2 YOO3 #[1,]124 HELLO - NULL HELLO$YOO1=BOO HELLO$YOO2=BOO HELLO$YOO3=HOO and I want a matrix that will sum my categorization.. how can I do it efficiently without any loop? #ie BOO HOO #[1,] 3 4 Thanks a lot! -- View this message in context: http://www.nabble.com/Matrix-manipulation-tf3223618.html#a8953808 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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] Matrix manipulation
Hi, let's say I have this A = matrix(c(1, 2, 4), nrow=1) colnames(A)=c(YOO1, YOO2, YOO3) Why do you need A to be a matrix and not simply a vector? # ie # YOO1 YOO2 YOO3 #[1,]124 HELLO - NULL HELLO$YOO1=BOO HELLO$YOO2=BOO HELLO$YOO3=HOO Why do you need HELLO to be a list and not simply a character vector? and I want a matrix that will sum my categorization.. how can I do it efficiently without any loop? #ie BOO HOO #[1,] 3 4 Anyway, here is a solution: x - tapply(A, match(unlist(HELLO), unique(unlist(HELLO))), sum) names(x) - unique(unlist(HELLO)) x HTH, Giovanni -- Giovanni Petris [EMAIL PROTECTED] Associate Professor Department of Mathematical Sciences University of Arkansas - Fayetteville, AR 72701 Ph: (479) 575-6324, 575-8630 (fax) http://definetti.uark.edu/~gpetris/ __ R-help@stat.math.ethz.ch 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] matrix of matrices
Dear all, it is likely a stupid question but I cannot solve it. I want to have a matrix of 100 elements. Each element must be a vector of 500 elements. If I do: imp-array(dim=100) imp[1]-vector(length=500) it does not work. Warning message: number of items to replace is not a multiple of replacement length If I do: imp - array(dim=c(100,500)) and then fill imp: for(i in c(1:500)) { imp[i,] - im[1:500,] #im[1:500,] is a vector of length 500, of class numeric. IT CONTAINS NAMES! } Now it works, but I loose the labels (names) associated to the original im variable. If I just do: j- im[1:500,] I do not loose the labels. names(j) = list of labels names(imp[1,]) = NULL Any clue? Thanks in advance! Federico __ R-help@stat.math.ethz.ch 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] matrix of matrices
For the case someone is interested in it, here it is the solution somebody suggested me: to use a list. imp - vector(list, 100) imp[[1]] - im[1:500,] names(imp[[1]]) = the list of labels of imp[1:500,] Thanks! Federico Federico Abascal wrote: Dear all, it is likely a stupid question but I cannot solve it. I want to have a matrix of 100 elements. Each element must be a vector of 500 elements. If I do: imp-array(dim=100) imp[1]-vector(length=500) it does not work. Warning message: number of items to replace is not a multiple of replacement length If I do: imp - array(dim=c(100,500)) and then fill imp: for(i in c(1:500)) { imp[i,] - im[1:500,] #im[1:500,] is a vector of length 500, of class numeric. IT CONTAINS NAMES! } Now it works, but I loose the labels (names) associated to the original im variable. If I just do: j- im[1:500,] I do not loose the labels. names(j) = list of labels names(imp[1,]) = NULL Any clue? Thanks in advance! Federico __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Matrix question: obtaining the square root of a positive definite matrix?
On Wed, 24 Jan 2007, gallon li wrote: I want to compute B=A^{1/2} such that B*B=A. According to your subject line A is positive definite and hence symmetric? The usual definition of a matrix square root involves a transpose, e.g. B'B = A. There are many square roots: were you looking for a symmetric one? For such an A, e - eigen(A) V - e$vectors V %*% diag(e$values) %*% t(V) recovers A (up to rounding errors), and B - V %*% diag(sqrt(e$values)) %*% t(V) is such that B %*% B = A. Even that is not unique, e.g. -B is an equally good answer. For example (with A = b and B = a, it seems) a=matrix(c(1,.2,.2,.2,1,.2,.2,.2,1),ncol=3) so a [,1] [,2] [,3] [1,] 1.0 0.2 0.2 [2,] 0.2 1.0 0.2 [3,] 0.2 0.2 1.0 a%*%a [,1] [,2] [,3] [1,] 1.08 0.44 0.44 [2,] 0.44 1.08 0.44 [3,] 0.44 0.44 1.08 b=a%*%a i have tried to use singular value decomposion c=svd(b) c$u%*%diag(sqrt(c$d)) [,1] [,2] [,3] [1,] -0.8082904 2.043868e-18 0.6531973 [2,] -0.8082904 -5.656854e-01 -0.3265986 [3,] -0.8082904 5.656854e-01 -0.3265986 this does not come close to the original a. Can anybody on this forum enlight me on how to get a which is the square root of b? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch 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] Matrix question: obtaining the square root of a positive definite matrix?
Prof Brian Ripley wrote: On Wed, 24 Jan 2007, gallon li wrote: I want to compute B=A^{1/2} such that B*B=A. According to your subject line A is positive definite and hence symmetric? The usual definition of a matrix square root involves a transpose, e.g. B'B = A. There are many square roots: were you looking for a symmetric one? If not, Choleski decomposition by chol() is often the expedient way. For such an A, e - eigen(A) V - e$vectors V %*% diag(e$values) %*% t(V) recovers A (up to rounding errors), and B - V %*% diag(sqrt(e$values)) %*% t(V) is such that B %*% B = A. Even that is not unique, e.g. -B is an equally good answer. and you can flip the sign of the individual root eigenvalues too, and if the eigenvalues are not unique, you can rotate the eigenspace coordinate systems at will and then flip signs. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch 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] Matrix subsetting {was ... vectorized nested loop...}
Hi Jose, I'm answering your second batch of questions, since Chuck Berry has already well done so with the first one Jose == Jose Quesada [EMAIL PROTECTED] on Tue, 23 Jan 2007 21:46:27 +0100 writes: [] Jose # example Jose library(Matrix) Jose x = as(x,CsparseMatrix) [..] Jose Also, I have noticed that getting a row from a Matrix Jose object produces a normal array (i.e., it does not Jose inherit Matrix class). This is very much on purpose, following the principle of least surprise so I'm surprised you're suprised.. : The 'Matrix' behavior has been modelled to follow the more than 20 years old 'matrix' behavior : matrix(1:9, 3) [,2] [1] 4 5 6 matrix(1:9, 3) [,2 , drop=FALSE] [,1] [1,]4 [2,]5 [3,]6 library(Matrix) Loading required package: lattice Matrix(1:9, 3) [,2] [1] 4 5 6 Matrix(1:9, 3) [,2, drop = FALSE] 3 x 1 Matrix of class dgeMatrix [,1] [1,]4 [2,]5 [3,]6 But then I should not be surprised, because there has been the R FAQ 7.5 Why do my matrices lose dimensions? for quite a while. *And* I think that there is only one thing in the S language about which every knowledgable one agrees that it's a design bug, and that's the fact that 'drop = TRUE' is the default, and not 'drop = FALSE' {but it's not possible to change now, please don't start that discussion!} Given what I say above, I wonder if our (new-style) 'Matrix' objects should not behave differently than (old-style) 'matrix' and indeed do use a default 'drop = FALSE'. This might break some Matrix-based code though, but then 'Matrix' is young enough, and working Matrix indexing is much younger, and there are only about 4 CRAN/Bioconductor packages depending on 'Matrix'. -- This discussion (about changing this behavior in the Matrix package) should definitely be lead on the R-devel mailing list -- CC'ing to R-devel {hence one (but please *only* one !) cross-post} Jose However, selecting 1 rows, Jose does produce a same-class matrix. If I convert with Jose as() the output of selecting one row, am I losing Jose performance? Is there any way to make the resulting Jose vector be a 1-D Matrix object? yes, , drop = FALSE, see above Martin __ R-help@stat.math.ethz.ch 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] Matrix operations in a list
I have matrices stored within a list like something as follows: a - list(matrix(rnorm(50), ncol=5), matrix(rnorm(50), ncol=5)) b - list(matrix(rnorm(50), nrow=5), matrix(rnorm(50), nrow=5)) I don't recall how to perform matrix multiplication on each list element such that the result is a new list result - list(a[[1]]%*%b[[1]], a[[2]]%*%b[[2]]) I think I'm close with mapply(), but I'm doing something silly mapply('%*%', a,b) Thanks. Harold [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] Matrix operations in a list
On Tue, 2007-01-23 at 10:21 -0500, Doran, Harold wrote: I have matrices stored within a list like something as follows: a - list(matrix(rnorm(50), ncol=5), matrix(rnorm(50), ncol=5)) b - list(matrix(rnorm(50), nrow=5), matrix(rnorm(50), nrow=5)) I don't recall how to perform matrix multiplication on each list element such that the result is a new list result - list(a[[1]]%*%b[[1]], a[[2]]%*%b[[2]]) I think I'm close with mapply(), but I'm doing something silly mapply('%*%', a,b) Thanks. Harold Harold, That should basically be working. Just note that by default, each resultant matrix is put into a column (vector) format, rather than as a matrix. Res - mapply(%*%, a, b) Res1 - a[[1]] %*% b[[1]] Res2 - a[[2]] %*% b[[2]] str(Res) num [1:100, 1:2] 0.1713 0.8290 -0.0864 3.5420 -1.4638 ... - attr(*, dimnames)=List of 2 ..$ : NULL ..$ : NULL all.equal(Res[, 1], as.vector(Res1)) [1] TRUE all.equal(Res[, 2], as.vector(Res2)) [1] TRUE If you want the results to be a list of two matrices, you would do something like: Res - mapply(%*%, a, b, SIMPLIFY = FALSE) str(Res) List of 2 $ : num [1:10, 1:10] 0.1713 0.8290 -0.0864 3.5420 -1.4638 ... $ : num [1:10, 1:10] 0.220 -2.048 -0.135 -2.121 -0.399 ... all.equal(Res1, Res[[1]]) [1] TRUE all.equal(Res2, Res[[2]]) [1] TRUE HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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] Matrix operations in a list
you need the 'SIMPLIFY' argument of mapply(), i.e., mapply(%*%, a, b, SIMPLIFY = FALSE) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Doran, Harold [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Tuesday, January 23, 2007 4:21 PM Subject: [R] Matrix operations in a list I have matrices stored within a list like something as follows: a - list(matrix(rnorm(50), ncol=5), matrix(rnorm(50), ncol=5)) b - list(matrix(rnorm(50), nrow=5), matrix(rnorm(50), nrow=5)) I don't recall how to perform matrix multiplication on each list element such that the result is a new list result - list(a[[1]]%*%b[[1]], a[[2]]%*%b[[2]]) I think I'm close with mapply(), but I'm doing something silly mapply('%*%', a,b) Thanks. Harold [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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] Matrix operations in a list
Try, mapply('%*%', a, b, SIMPLIFY=FALSE) -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Doran, Harold Sent: Tuesday, January 23, 2007 10:22 AM To: r-help@stat.math.ethz.ch Subject: [R] Matrix operations in a list I have matrices stored within a list like something as follows: a - list(matrix(rnorm(50), ncol=5), matrix(rnorm(50), ncol=5)) b - list(matrix(rnorm(50), nrow=5), matrix(rnorm(50), nrow=5)) I don't recall how to perform matrix multiplication on each list element such that the result is a new list result - list(a[[1]]%*%b[[1]], a[[2]]%*%b[[2]]) I think I'm close with mapply(), but I'm doing something silly mapply('%*%', a,b) Thanks. Harold [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Matrix question: obtaining the square root of a positive definite matrix?
I want to compute B=A^{1/2} such that B*B=A. For example a=matrix(c(1,.2,.2,.2,1,.2,.2,.2,1),ncol=3) so a [,1] [,2] [,3] [1,] 1.0 0.2 0.2 [2,] 0.2 1.0 0.2 [3,] 0.2 0.2 1.0 a%*%a [,1] [,2] [,3] [1,] 1.08 0.44 0.44 [2,] 0.44 1.08 0.44 [3,] 0.44 0.44 1.08 b=a%*%a i have tried to use singular value decomposion c=svd(b) c$u%*%diag(sqrt(c$d)) [,1] [,2] [,3] [1,] -0.8082904 2.043868e-18 0.6531973 [2,] -0.8082904 -5.656854e-01 -0.3265986 [3,] -0.8082904 5.656854e-01 -0.3265986 this does not come close to the original a. Can anybody on this forum enlight me on how to get a which is the square root of b? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] matrix size
Hello everyone Could anybody tell me how to set the following matrix? n2-matrix(nrow=10185,ncol=10185,seq(0,0,length=103734225)) R answer was Error: cannot allocate vector of size 810423 Kb Are there any solution? I tried to increase the memory size but it didn't work G This message has been scanned for viruses by TRENDMICRO,\ an...{{dropped}} __ R-help@stat.math.ethz.ch 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] matrix size
GArmelini == Armelini, Guillermo [EMAIL PROTECTED] on Mon, 1 Jan 2007 23:43:31 +0100 writes: GArmelini Hello everyone Could anybody tell me how to set GArmelini the following matrix? GArmelini n2-matrix(nrow=10185,ncol=10185,seq(0,0,length=103734225)) GArmelini R answer was Error: cannot allocate vector of GArmelini size 810423 Kb of course. GArmelini Are there any solution? I tried to increase the GArmelini memory size but it didn't work G You might consider using *sparse* matrices such as available by R's Matrix package. install.packages(Matrix) # once only library(Matrix) # every time you use it n2 - Matrix(0, nrow=10185, ncol=10185) will produce a sparse Matrix (of class dsCMatrix) that does not need a lot of memory. If you want to do anything reasonable, with 'n2' I assume you'll want to add some non-zero entries. To do that (and similary things) a bit more efficiently in the current implementations of Matrix, I'd recommend to work with a TsparseMatrix (instead of the `default' Csparse*), i.e. a sparse matrix representation working with so called triplets, e.g. m2 - as(n2, TsparseMatrix) m2[1,3] - 10 m2[2, 1:10] - 0:9 m2[1:10, 1:20] etc,.. BTW: Such sub-assignments should now work, but some or still slow. Till now the main emphasis for such matrices was general good organization and speed in matrix operations rather than index operations. I'm quite interested to hear what you want to do with your matrix. Use R-help if you think it could be of general interest, or reply privately if you prefer. Regards, Martin Maechler, ETH Zurich __ R-help@stat.math.ethz.ch 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] matrix size
On Jan 1, 2007, at 4:43 PM, Armelini, Guillermo wrote: Hello everyone Could anybody tell me how to set the following matrix? n2-matrix(nrow=10185,ncol=10185,seq(0,0,length=103734225)) You can use: library(SparseM) as.matrix.coo(0,10185,10185) but then you need to find something interesting to do with such a boring matrix... R answer was Error: cannot allocate vector of size 810423 Kb Are there any solution? I tried to increase the memory size but it didn't work G This message has been scanned for viruses by TRENDMICRO,\ an... {{dropped}} __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix - change values
Dear R Users, I have a matrix A, and I want to change every value of this matrix if these values are greater than an assuming value. For a vector it is simple, e.g. a-c(1:10); a[a5]-0. Of course, I can change matrix to vector, assign a value then change vector to matrix. But does there exist simpler way? Any suggestion are appreciate. Rob __ R-help@stat.math.ethz.ch 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] matrix - change values
[EMAIL PROTECTED] wrote: Dear R Users, I have a matrix A, and I want to change every value of this matrix if these values are greater than an assuming value. For a vector it is simple, e.g. a-c(1:10); a[a5]-0. Of course, I can change matrix to vector, assign a value then change vector to matrix. But does there exist simpler way? The same syntax as for a vector: A[A5] - 0 Remember that matrices are just vectors with a dim attribute. The dim attribute is unchanged by this operation: A - matrix(1:10, 2, 5) A [,1] [,2] [,3] [,4] [,5] [1,]13579 [2,]2468 10 A[A5] - 0 A [,1] [,2] [,3] [,4] [,5] [1,]13500 [2,]24000 Duncan Murdoch __ R-help@stat.math.ethz.ch 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] matrix - change values
Rob, Try a[a5]-0 Yup. It works for matrices (and for arrays). It also works with the replacement value being a vector. For example, try b - array(1:24, dim=c(3, 4, 2)) b[(b8) (b17)] - 101:108 I think the reason it works like this is that internally array are stored as vectors. Cheers, Andy __ Andy Jaworski 518-1-01 Process Laboratory 3M Corporate Research Laboratory - E-mail: [EMAIL PROTECTED] Tel: (651) 733-6092 Fax: (651) 736-3122 [EMAIL PROTECTED] 2.pl Sent by: To [EMAIL PROTECTED] r-help@stat.math.ethz.ch at.math.ethz.chcc Subject 12/14/2006 08:01 [R] matrix - change values AM Dear R Users, I have a matrix A, and I want to change every value of this matrix if these values are greater than an assuming value. For a vector it is simple, e.g. a-c(1:10); a[a5]-0. Of course, I can change matrix to vector, assign a value then change vector to matrix. But does there exist simpler way? Any suggestion are appreciate. Rob __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix - change values
I would like to thanks everybody for helpful suggestion. Rob Od: [EMAIL PROTECTED] Do: r-help@stat.math.ethz.ch Data: 14 grudnia 2006 15:01 Temat: [R] matrix - change values Dear R Users, I have a matrix A, and I want to change every value of this matrix if these values are greater than an assuming value. For a vector it is simple, e.g. a-c(1:10); a[a5]-0. Of course, I can change matrix to vector, assign a value then change vector to matrix. But does there exist simpler way? Any suggestion are appreciate. Rob __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix - change values
A matrix is already a vector, you don't need to do the transformations, just do the same thing directly: tmp - matrix( sample(1:12), ncol=3 ) tmp [,1] [,2] [,3] [1,] 1116 [2,]379 [3,]4 128 [4,]25 10 tmp[tmp 5] - 0 tmp [,1] [,2] [,3] [1,]010 [2,]300 [3,]400 [4,]250 If on the other hand, your matrix is really a data frame then functions like lapply, sapply, transform may help. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Thursday, December 14, 2006 7:01 AM To: r-help@stat.math.ethz.ch Subject: [R] matrix - change values Dear R Users, I have a matrix A, and I want to change every value of this matrix if these values are greater than an assuming value. For a vector it is simple, e.g. a-c(1:10); a[a5]-0. Of course, I can change matrix to vector, assign a value then change vector to matrix. But does there exist simpler way? Any suggestion are appreciate. Rob __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix comparison, and cbind issues.
Hello, This should be a fairly simple question. I have 2 matrix's and I want to get the difference between them and then remove the difference from one of them. So A vs B, anything I see in B I want to remove from A, and then print out the final matrix. I have some code here but at the moment I'm getting stuck on the creating a matrix, cbind problems. clean - function (A, B, rt=10, mass=0.01, pval=0.01){ i -0 j -0 d.A -dim(A) d.B -dim(B) clean -0 for (i in i:d.A[1]){ for (j in j:d.B[1]){ if(any((A[i,6] - B[j,6]) = mass)){ if (any((A[i,9] - B[j,9])= rt)){ if (B[j,5] = pval){ clean-cbind(clean,A[i,]) } } } } } return (clean) } I hope that I have explained myself well. If not please let me know and I'll try to explain more. Cheers, PB Research Technician Mass Spectrometry o The / o Scripps \ o Research / o Institute [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] matrix comparison, and cbind issues.
Mark, A and B will probably be different sizes. Won't 'C-A-B' just do the subtraction of matrix A by matrix B? So What I was thinking was that it would find 2 numbers one at position 6 and the other at position 9. If it finds roughtly these numbers in matrix B then it will identify this and remove that row from matrix A. Paul -Original Message- From: Leeds, Mark (IED) [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 29, 2006 12:12 PM To: H. Paul Benton Subject: RE: [R] matrix comparison, and cbind issues. If A and B are the same size ( I can't tell ),then you can C-A-B to get the dfiference but that might Be simplifying things too much. If it isn't simplifying thigns too, then let me know and I think I can show you how to do the rest. If it is, then my bad and I apologize. __ R-help@stat.math.ethz.ch 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] Matrix-vector multiplication without loops
On 11/14/06, Ravi Varadhan [EMAIL PROTECTED] wrote: I am trying to do the following computation: p - rep(0, n) coef - runif(K+1) U - matrix(runif(n*(2*K+1)), n, 2*K+1) for (i in 0:K){ for (j in 0:K){ p - p + coef[i+1]* coef[j+1] * U[,i+j+1] } } I would appreciate any suggestions on how to perform this computation efficiently without the for loops? This kicks butt on my machine: p - as.vector(U %*% convolve(coef,rev(coef),type=open)) HTH! Richard Graham JHU '84 EECS __ R-help@stat.math.ethz.ch 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] Matrix-vector multiplication without loops
I think you need something along these lines: ind - c(sapply(1:(K+1), seq, length = K + 1)) cf1 - rep(rep(coef, each = n), K + 1) cf2 - rep(rep(coef, each = n), each = K + 1) rowSums(cf1 * cf2 * U[, ind]) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Ravi Varadhan [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Tuesday, November 14, 2006 4:45 PM Subject: [R] Matrix-vector multiplication without loops Hi, I am trying to do the following computation: p - rep(0, n) coef - runif(K+1) U - matrix(runif(n*(2*K+1)), n, 2*K+1) for (i in 0:K){ for (j in 0:K){ p - p + coef[i+1]* coef[j+1] * U[,i+j+1] } } I would appreciate any suggestions on how to perform this computation efficiently without the for loops? Thank you, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: [EMAIL PROTECTED] Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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] Matrix-vector multiplication without loops
Hi, I am trying to do the following computation: p - rep(0, n) coef - runif(K+1) U - matrix(runif(n*(2*K+1)), n, 2*K+1) for (i in 0:K){ for (j in 0:K){ p - p + coef[i+1]* coef[j+1] * U[,i+j+1] } } I would appreciate any suggestions on how to perform this computation efficiently without the for loops? Thank you, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: [EMAIL PROTECTED] Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] Matrix-vector multiplication without loops
Ravi, Here is another version, somewhat similar to what Dimitris proposed but without using 'rep'. For large n, K 'rep' tries allocating two vectors, each of length n*(K+1)^2, which can be a problem. In this version 'outer' buys you some efficiency and compactness, but your looping version is faster. n - 1000 K - 1000 p - rep(0, n) cf - runif(K+1) U - matrix(runif(n*(2*K+1)), n, 2*K+1) # original code system.time( { for (i in 0:K) for (j in 0:K) p - p + cf[i+1]* cf[j+1] * U[,i+j+1] p.1 - p } ) # 'vectorized' system.time( { ind - sapply(1:(K+1), seq, length = K+1) cc - outer(cf,cf) p.2 - apply(U, 1, FUN=function(u) sum(cc * u[ind])) } ) all.equal(p.1, p.2) rm(n,K,p,U,cf,cc,ind,p.1,p.2) -Christos Christos Hatzis, Ph.D. Nuvera Biosciences, Inc. 400 West Cummings Park Suite 5350 Woburn, MA 01801 Tel: 781-938-3830 www.nuverabio.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dimitris Rizopoulos Sent: Tuesday, November 14, 2006 11:20 AM To: Ravi Varadhan Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Matrix-vector multiplication without loops I think you need something along these lines: ind - c(sapply(1:(K+1), seq, length = K + 1)) cf1 - rep(rep(coef, each = n), K + 1) cf2 - rep(rep(coef, each = n), each = K + 1) rowSums(cf1 * cf2 * U[, ind]) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Ravi Varadhan [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Tuesday, November 14, 2006 4:45 PM Subject: [R] Matrix-vector multiplication without loops Hi, I am trying to do the following computation: p - rep(0, n) coef - runif(K+1) U - matrix(runif(n*(2*K+1)), n, 2*K+1) for (i in 0:K){ for (j in 0:K){ p - p + coef[i+1]* coef[j+1] * U[,i+j+1] } } I would appreciate any suggestions on how to perform this computation efficiently without the for loops? Thank you, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: [EMAIL PROTECTED] Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Matrix-vector multiplication without loops
In my case, I have n K so the loop solution is faster than the solutions proposed by Christos and Dimitris. In any case, I would like to thank Christos and Dimitris for their solutions. Best, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: [EMAIL PROTECTED] Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html -Original Message- From: Christos Hatzis [mailto:[EMAIL PROTECTED] Sent: Tuesday, November 14, 2006 2:49 PM To: 'Dimitris Rizopoulos'; 'Ravi Varadhan' Cc: r-help@stat.math.ethz.ch Subject: RE: [R] Matrix-vector multiplication without loops Ravi, Here is another version, somewhat similar to what Dimitris proposed but without using 'rep'. For large n, K 'rep' tries allocating two vectors, each of length n*(K+1)^2, which can be a problem. In this version 'outer' buys you some efficiency and compactness, but your looping version is faster. n - 1000 K - 1000 p - rep(0, n) cf - runif(K+1) U - matrix(runif(n*(2*K+1)), n, 2*K+1) # original code system.time( { for (i in 0:K) for (j in 0:K) p - p + cf[i+1]* cf[j+1] * U[,i+j+1] p.1 - p } ) # 'vectorized' system.time( { ind - sapply(1:(K+1), seq, length = K+1) cc - outer(cf,cf) p.2 - apply(U, 1, FUN=function(u) sum(cc * u[ind])) } ) all.equal(p.1, p.2) rm(n,K,p,U,cf,cc,ind,p.1,p.2) -Christos Christos Hatzis, Ph.D. Nuvera Biosciences, Inc. 400 West Cummings Park Suite 5350 Woburn, MA 01801 Tel: 781-938-3830 www.nuverabio.com -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dimitris Rizopoulos Sent: Tuesday, November 14, 2006 11:20 AM To: Ravi Varadhan Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Matrix-vector multiplication without loops I think you need something along these lines: ind - c(sapply(1:(K+1), seq, length = K + 1)) cf1 - rep(rep(coef, each = n), K + 1) cf2 - rep(rep(coef, each = n), each = K + 1) rowSums(cf1 * cf2 * U[, ind]) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Ravi Varadhan [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Tuesday, November 14, 2006 4:45 PM Subject: [R] Matrix-vector multiplication without loops Hi, I am trying to do the following computation: p - rep(0, n) coef - runif(K+1) U - matrix(runif(n*(2*K+1)), n, 2*K+1) for (i in 0:K){ for (j in 0:K){ p - p + coef[i+1]* coef[j+1] * U[,i+j+1] } } I would appreciate any suggestions on how to perform this computation efficiently without the for loops? Thank you, Ravi. --- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: [EMAIL PROTECTED] Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix manipulation with a for loop
Your for-loops aren't set up properly: try for(i in 1:NCOL(F.zoo)) HTH, Fabian Scheipl -- __ R-help@stat.math.ethz.ch 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] matrix manipulation with a for loop
Hi, Having a matrix F.zoo (6575,189) with NA's in some columns I'm trying to extract from each column the percent of days within an specific range, so I've wrote this procedure: length(subset(F.zoo[,86],(F.zoo[,86]=5) (F.zoo[,86]= 9)))/(length(F.zoo[,86])-length(subset(F.zoo[,86],is.na(F.zoo[,86]*100 But to do this for each column (189) is pretty hard, so I want to write a function in order to perform this automatically, such I have the percent value corresponding to a specific column. I' tried these two formulas but I can't get it. I think the problem is how to set the initial values for the loop: Formula1: nnn-function(x){for (i in F.zoo[,i]){ print(length(subset(F.zoo[,i],(F.zoo[,i]=5) (F.zoo[,i]= 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]*100) } } Formula 2: H-t(matrix(1,189)) nnn-function(x){for (i in col(H){ print(length(subset(F.zoo[,i],(F.zoo[,i]=5) (F.zoo[,i]= 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]*100) } } Thanks, Antonio __ R-help@stat.math.ethz.ch 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] matrix manipulation with a for loop
Try this where m is the matrix: 100 * colMeans(m 5 m 9, na.rm = TRUE) On 11/1/06, antonio rodriguez [EMAIL PROTECTED] wrote: Hi, Having a matrix F.zoo (6575,189) with NA's in some columns I'm trying to extract from each column the percent of days within an specific range, so I've wrote this procedure: length(subset(F.zoo[,86],(F.zoo[,86]=5) (F.zoo[,86]= 9)))/(length(F.zoo[,86])-length(subset(F.zoo[,86],is.na(F.zoo[,86]*100 But to do this for each column (189) is pretty hard, so I want to write a function in order to perform this automatically, such I have the percent value corresponding to a specific column. I' tried these two formulas but I can't get it. I think the problem is how to set the initial values for the loop: Formula1: nnn-function(x){for (i in F.zoo[,i]){ print(length(subset(F.zoo[,i],(F.zoo[,i]=5) (F.zoo[,i]= 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]*100) } } Formula 2: H-t(matrix(1,189)) nnn-function(x){for (i in col(H){ print(length(subset(F.zoo[,i],(F.zoo[,i]=5) (F.zoo[,i]= 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]*100) } } Thanks, Antonio __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix manipulation with a for loop
Phil Spector escribió: Antonio - When you're operating on each column of a matrix, you really should consider the apply() function, which was written for the task. Also, it's usually easier to count things in R by taking the sum of a logical expression, rather than the length of a subsetted vector. Here's code that will solve your problem: apply(F.zoo,1,function(x)sum(x =5 x = 9)/sum(!is.na(x))*100) Dear Phil, The problem is that the columns have some missing values (NA's) so the result for: apply(F.zoo,2,function(x)sum(x =5 x = 9)/sum(!is.na(x))*100) # yields: X.1 X.2 X.3 X.4 X.5 X.6 X.7 X.8 X.9 X.10 X.11 X.12 X.13 NANANANANANANANANANANA NANA X.14 X.15 X.16 X.17 X.18 X.19 X.20 X.21 X.22 X.23 X.24 X.25 X.26 NANANANANANANANANANANA NANA So it is supposed that using na.rm=T should do the task, but if I write: apply(F.zoo,2,function(x)sum(F.zoo =5 F.zoo = 9)/sum(!is.na(F.zoo))*100,na.rm=T) I get: Erro en FUN(newX[, i], ...) : unused argument(s) (na.rm = TRUE) Antonio - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley [EMAIL PROTECTED] On Wed, 1 Nov 2006, antonio rodriguez wrote: Hi, Having a matrix F.zoo (6575,189) with NA's in some columns I'm trying to extract from each column the percent of days within an specific range, so I've wrote this procedure: length(subset(F.zoo[,86],(F.zoo[,86]=5) (F.zoo[,86]= 9)))/(length(F.zoo[,86])-length(subset(F.zoo[,86],is.na(F.zoo[,86]*100 But to do this for each column (189) is pretty hard, so I want to write a function in order to perform this automatically, such I have the percent value corresponding to a specific column. I' tried these two formulas but I can't get it. I think the problem is how to set the initial values for the loop: Formula1: nnn-function(x){for (i in F.zoo[,i]){ print(length(subset(F.zoo[,i],(F.zoo[,i]=5) (F.zoo[,i]= 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]*100) } } Formula 2: H-t(matrix(1,189)) nnn-function(x){for (i in col(H){ print(length(subset(F.zoo[,i],(F.zoo[,i]=5) (F.zoo[,i]= 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]*100) } } Thanks, Antonio __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] matrix manipulation with a for loop
Gabor Grothendieck escribió: Try this where m is the matrix: 100 * colMeans(m 5 m 9, na.rm = TRUE) Dear Gabor, Just perfect! Thanks a lot, Antonio On 11/1/06, antonio rodriguez [EMAIL PROTECTED] wrote: Hi, Having a matrix F.zoo (6575,189) with NA's in some columns I'm trying to extract from each column the percent of days within an specific range, so I've wrote this procedure: length(subset(F.zoo[,86],(F.zoo[,86]=5) (F.zoo[,86]= 9)))/(length(F.zoo[,86])-length(subset(F.zoo[,86],is.na(F.zoo[,86]*100 But to do this for each column (189) is pretty hard, so I want to write a function in order to perform this automatically, such I have the percent value corresponding to a specific column. I' tried these two formulas but I can't get it. I think the problem is how to set the initial values for the loop: Formula1: nnn-function(x){for (i in F.zoo[,i]){ print(length(subset(F.zoo[,i],(F.zoo[,i]=5) (F.zoo[,i]= 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]*100) } } Formula 2: H-t(matrix(1,189)) nnn-function(x){for (i in col(H){ print(length(subset(F.zoo[,i],(F.zoo[,i]=5) (F.zoo[,i]= 9)))/(length(F.zoo[,i])-length(subset(F.zoo[,i],is.na(F.zoo[,i]*100) } } Thanks, Antonio __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.