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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Re: [R] matrix manipulation with a for loop
Phil Spector escribió: Antonio - You need the na.rm in the first invocation of sum -- is.na() will never return a missing value: apply(F.zoo,2,function(x)sum(x =5 x = 9,na.rm=TRUE)/sum(!is.na(x))*100) - Phil Dear Phil, I understand now. Many thanks!! Best regards, Antonio On Wed, 1 Nov 2006, antonio rodriguez wrote: 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 dimnames
Daniel Gatti wrote: How does one assign names to the columns of a matrix so that the columns can be accesses using the '$' operator? It seem that x$c1 below should return column 1. x = matrix(1:4,2) dimnames(x) = list(c(r1, r2), c(c1, c2)) x c1 c2 r1 1 3 r2 2 4 x$c1 NULL x$c2 NULL Is this useful?-- x[,c1] r1 r2 1 2 I have only seen $ used with lists. A data frame is a kind of list, but a matrix isn't. From the R Reference Manual: The form [of indexing] using $ applies to recursive objects such as lists and pairlists. HTH Mike Prager Southeast Fisheries Science Center, NOAA Beaufort, North Carolina USA __ 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
Is this what you want ? A1-matrix(c(1,0,0,0,0,0,0,0,0),3) A2-matrix(c(1,0,0,0,1,0,0,0,0),3) A3-matrix(c(1,0,0,0,1,0,0,1,0),3) X - matrix(1:24,8) XX-list() for(i in 1:3){ + XX[[i]]-X%*%A[[i]] + } XX [[1]] [,1] [,2] [,3] [1,]100 [2,]200 [3,]300 [4,]400 [5,]500 [6,]600 [7,]700 [8,]800 [[2]] [,1] [,2] [,3] [1,]190 [2,]2 100 [3,]3 110 [4,]4 120 [5,]5 130 [6,]6 140 [7,]7 150 [8,]8 160 [[3]] [,1] [,2] [,3] [1,]199 [2,]2 10 10 [3,]3 11 11 [4,]4 12 12 [5,]5 13 13 [6,]6 14 14 [7,]7 15 15 [8,]8 16 16 Ya-Hsiu Chuang wrote: Dear all, I have 2 matrices, one is a 8x3 matrix, called X; the other matrix is a 3x3 indicator matrix with the diagonal element as 0 or 1. when a variable is included in the model, the corresponding diagonal element is 1, otherwise, it is 0. Let A be a set of matrices that contain the possible indicator matrix. e.g., A= [A1, A2, A3], where A1= [1,0,0;0,0,0,0,0,0], A2 =[1,0,0;0,1,0,0,0,0], A3 =[1,0,0;0,0,0,0,1,0] In order to derive the new X matries depending on the indicator matrices, I use for loops to multiply both X and A as following p-3 for ( i in 1:p){ XX- X%*%A[i] } However, it only shows the result when i=p. How can I derive the results which include all possible value of XX[i], i=1,..,p, rather than just i=p thanks for any help Ya-Hsiu [[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. -- Ferdinand Alimadhi Programmer / Analyst Harvard University The Institute for Quantitative Social Science (617) 496-0187 [EMAIL PROTECTED] www.iq.harvard.edu [[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 input problem
On 10/5/2006 4:52 PM, Bill Wyatt wrote: Hi, Included is an R.script that came from a much large date set being read in to R from a .txt file. Why is it that the first line codes with an error, but the second line works fine? I get syntax errors on both, due to the missing comma at the end of the very long line. Is there some line length limit in R? Yes, there's a 1024 character length limit (which is documented in the Intro to R manual in the current release; not sure how long it's been there). Why not put in some line breaks? R source is supposed to be human readable, not just machine readable. Duncan Murdoch This happens at random places all through my data. Any help would be appreciated. Bill Wyatt Associate Instructor Ergonomics Graduate Student Department of Kinesiology School of Health, Physical Education, and Recreation Indiana University O:(812)856-5924 [EMAIL PROTECTED] //orginal line b1x-cbind(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-132.5,-132.5,-132.5,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-133.6,-133.6,-133.05,-133.05,-131.35,-131.35,-129.7,-129.7,-126.9,-126.9,-124.7,-124.7,-121.35,-121.35,-118,-118,-114.7,-114.7,-110.8,-110.8,-106.9,-106.9,-102.45,-102.45,-98,-98,-92.95,-92.95,-87.95,-87.95,-82.95,-82.95,-77.95,-77.95,-72.35,-6 7.3 5,-67.35,-61.8,-61.8,-55.65,-55.65)) //copy of line with end )) moved one data point forward b1x-cbind(c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-132.5,-132.5,-132.5,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-134.15,-133.6,-133.6,-133.05,-133.05,-131.35,-131.35,-129.7,-129.7,-126.9,-126.9,-124.7,-124.7,-121.35,-121.35,-118,-118,-114.7,-114.7,-110.8,-110.8,-106.9,-106.9,-102.45,-102.45,-98,-98,-92.95,-92.95,-87.95,-87.95,-82.95,-82.95,-77.95,-77.95,-72.35,-6 7.3 5,-67.35,-61.8,-61.8,-55.65)) ,-55.65 __ 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 with additional upper, botton, left and right cells
How about something like this: x - matrix(1:100,10) x.1 - array(-3, dim=c(12,12)) x.1[2:11, 2:11] - x x.1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] -3 -3 -3 -3 -3 -3 -3 -3 -3-3-3-3 [2,] -31 11 21 31 41 51 61 718191-3 [3,] -32 12 22 32 42 52 62 728292-3 [4,] -33 13 23 33 43 53 63 738393-3 [5,] -34 14 24 34 44 54 64 748494-3 [6,] -35 15 25 35 45 55 65 758595-3 [7,] -36 16 26 36 46 56 66 768696-3 [8,] -37 17 27 37 47 57 67 778797-3 [9,] -38 18 28 38 48 58 68 788898-3 [10,] -39 19 29 39 49 59 69 798999-3 [11,] -3 10 20 30 40 50 60 70 8090 100-3 [12,] -3 -3 -3 -3 -3 -3 -3 -3 -3-3-3-3 On 9/26/06, Milton Cezar [EMAIL PROTECTED] wrote: Dear R Gurus, I have a matrix dim(1000x1000) and I need create a second matrix with dim(1002x1002) and insert my first matrix at position col=2,line=2. Please, see an example below: 0050055050 555000 5000505005 5005000500 000555 and I need 300500550503 35550003 350005050053 350050005003 30005553 Thanks a lot, miltinho __ [[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. -- 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 from a vector?
Hi other approach is to use embed embed(c(rhsel,rhsel),length(rhsel))[-1,] which is a little bit quicker rhsel-rnorm(1000) n - length(rhsel) system.time({ i - sapply(1:n, function(i) (1:n - i)%%n + 1) x2-matrix(rhsel[i], n, n) }) system.time(x1-embed(c(rhsel,rhsel),length(rhsel))[-1,]) all.equal(x1,x2) HTH Petr On 21 Sep 2006 at 14:53, Sundar Dorai-Raj wrote: Date sent: Thu, 21 Sep 2006 14:53:52 -0500 From: Sundar Dorai-Raj [EMAIL PROTECTED] Organization: PDF Solutions, Inc. To: kone [EMAIL PROTECTED] Copies to: r-help@stat.math.ethz.ch Subject:Re: [R] Matrix from a vector? kone said the following on 9/21/2006 2:30 PM: Hi, Is there some function, which generates this kind of n x n -matrix from a vector? rhset [1] 1792 256 13312 512 1024 2048 8192 4096 m=matrix(nrow=length(rhset),ncol=length(rhset)) for(i in 1:length(rhset)) + { + m[,i]=rhset + rhset=c(rhset[length(rhset)], rhset[2:length(rhset)-1]) + } m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1792 4096 8192 2048 1024 512 13312 256 [2,] 256 1792 4096 8192 2048 1024 512 13312 [3,] 13312 256 1792 4096 8192 2048 1024 512 [4,] 512 13312 256 1792 4096 8192 2048 1024 [5,] 1024 512 13312 256 1792 4096 8192 2048 [6,] 2048 1024 512 13312 256 1792 4096 8192 [7,] 8192 2048 1024 512 13312 256 1792 4096 [8,] 4096 8192 2048 1024 512 13312 256 1792 Atte Tenkanen University of Turku, 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. Does this work for you? rhsel - c(1792, 256, 13312, 512, 1024, 2048, 8192, 4096) n - length(rhsel) i - sapply(1:n, function(i) (1:n - i)%%n + 1) matrix(rhsel[i], n, n) HTH, --sundar __ 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. Petr Pikal [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.
Re: [R] Matrix from a vector?
kone said the following on 9/21/2006 2:30 PM: Hi, Is there some function, which generates this kind of n x n -matrix from a vector? rhset [1] 1792 256 13312 512 1024 2048 8192 4096 m=matrix(nrow=length(rhset),ncol=length(rhset)) for(i in 1:length(rhset)) + { + m[,i]=rhset + rhset=c(rhset[length(rhset)], rhset[2:length(rhset)-1]) + } m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1792 4096 8192 2048 1024 512 13312 256 [2,] 256 1792 4096 8192 2048 1024 512 13312 [3,] 13312 256 1792 4096 8192 2048 1024 512 [4,] 512 13312 256 1792 4096 8192 2048 1024 [5,] 1024 512 13312 256 1792 4096 8192 2048 [6,] 2048 1024 512 13312 256 1792 4096 8192 [7,] 8192 2048 1024 512 13312 256 1792 4096 [8,] 4096 8192 2048 1024 512 13312 256 1792 Atte Tenkanen University of Turku, 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. Does this work for you? rhsel - c(1792, 256, 13312, 512, 1024, 2048, 8192, 4096) n - length(rhsel) i - sapply(1:n, function(i) (1:n - i)%%n + 1) matrix(rhsel[i], n, n) HTH, --sundar __ 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 using apply() or lappy() ?
Jeffrey J. Hallman jhallman at frb.gov writes: Tim Hesterberg timh at insightful.com writes: toby_marks at americancentury.com asked: I am trying to divide the columns of a matrix by the first row in the matrix. Dividing columns of a matrix by a vector is a pretty fundamental operation, and the query resulted in a large number of suggestions: It is unsatisfactory when such a fundamental operation is done in so many different ways. * It makes it hard to read other people's code. * Some of these are very inefficient. But since you have no way to force people to use your new 'standard' operators, people can and will still use any of those myriad ways you decry to do the same thing. It's part of the price you pay for working with a flexible system. Besides, nothing will ever make it easy to read other people's code. Reading others code is never an easy task, but there are huge differences between various authors and I agree with Tim, that it is nice to have some standardized operators for common/fundamental operations. If there are standard operators, people will use it more an more. __ 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 using apply() or lappy() ?
Tim Hesterberg [EMAIL PROTECTED] writes: [EMAIL PROTECTED] asked: I am trying to divide the columns of a matrix by the first row in the matrix. Dividing columns of a matrix by a vector is a pretty fundamental operation, and the query resulted in a large number of suggestions: It is unsatisfactory when such a fundamental operation is done in so many different ways. * It makes it hard to read other people's code. * Some of these are very inefficient. But since you have no way to force people to use your new 'standard' operators, people can and will still use any of those myriad ways you decry to do the same thing. It's part of the price you pay for working with a flexible system. Besides, nothing will ever make it easy to read other people's code. :-) Jeff __ 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 using apply() or lappy() ?
[EMAIL PROTECTED] asked: I am trying to divide the columns of a matrix by the first row in the matrix. Dividing columns of a matrix by a vector is a pretty fundamental operation, and the query resulted in a large number of suggestions: x/matrix(v, nrow(x), ncol(x), byrow = TRUE)) sweep(x, 2, v, /) x / rep(v, each = nrow(x)) x / outer(rep(1, nrow(x)), v) x %*% diag(1/v) t(apply(x, 1, function(x) x/v)) x/rep(v, each=nrow(x)) t(apply(x, 1, /, v)) library(reshape); iapply(x, 1, /, v) # R only t(t(x)/v) scale(x, center = FALSE, v) # not previously suggested It is unsatisfactory when such a fundamental operation is done in so many different ways. * It makes it hard to read other people's code. * Some of these are very inefficient. I propose to create standard functions and possibly operator forms for this and similar operators: colPlus(x, v) x %c+% v colMinus(x, v) x %c-% v colTimes(x, v) x %c*% v colDivide(x, v) x %c/% v colPower(x, v) x %c^% v Goals are: * more readable code * generic functions, with methods for objects such as data frames and S-PLUS bigdata objects (this would be for both S-PLUS and R) * efficiency -- use the fastest of the above methods, or drop to C to avoid replicating v. * allow error checking (that length of v matches number of columns of x) I'd like feedback (to me, I'll summarize for the list) on: * the suggestion in general * are names like colPlus OK, or do you have other suggestions? * create both functions and operators, or just the functions? * should there be similar operations for rows? Note: similar operations for rows are not usually needed, because x * v # e.g. where v = colMeans(x) is equivalent to (but faster than) x * rep(v, length = length(x)) The advantage would be that colTimes(x, v) could throw an error if length(v) != nrow(x) Tim Hesterberg P.S. Of the suggestions, my preference is a / rep(v, each=nrow(a)) It was to support this and similar +-*^ operations that I originally added the each argument to rep. | Tim Hesterberg Research Scientist | | [EMAIL PROTECTED] Insightful Corp.| | (206)802-23191700 Westlake Ave. N, Suite 500 | | (206)283-8691 (fax) Seattle, WA 98109-3044, U.S.A. | | www.insightful.com/Hesterberg | Download the S+Resample library from www.insightful.com/downloads/libraries __ 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 using apply() or lappy() ?
See ?sweep sweep(a, 2, a[1,],/) -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Wednesday, September 06, 2006 11:49 AM To: r-help@stat.math.ethz.ch Subject: [R] Matrix multiplication using apply() or lappy() ? I am trying to divide the columns of a matrix by the first row in the matrix. I have tried to get this using apply and I seem to be missing a concept regarding the apply w/o calling a function but rather command args %*% / etc. Would using apply be more efficient than this approach? I have observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3][,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.000 1.000 1. 1.000 1.00 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{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.
Re: [R] Matrix multiplication using apply() or lappy() ?
Here are a few possibilities: a - matrix(1:24, 4) # test data a / rep(a[1,], each = 4) a / outer(rep(1, nrow(a)), a[1,]) a %*% diag(1/a[1,]) sweep(a, 2, a[1,], /) On 9/6/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I am trying to divide the columns of a matrix by the first row in the matrix. I have tried to get this using apply and I seem to be missing a concept regarding the apply w/o calling a function but rather command args %*% / etc. Would using apply be more efficient than this approach? I have observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3][,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.000 1.000 1. 1.000 1.00 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{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.
Re: [R] Matrix multiplication using apply() or lappy() ?
And here is one more: t(apply(a, 1, function(x) x/a[1,])) On 9/6/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Here are a few possibilities: a - matrix(1:24, 4) # test data a / rep(a[1,], each = 4) a / outer(rep(1, nrow(a)), a[1,]) a %*% diag(1/a[1,]) sweep(a, 2, a[1,], /) On 9/6/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I am trying to divide the columns of a matrix by the first row in the matrix. I have tried to get this using apply and I seem to be missing a concept regarding the apply w/o calling a function but rather command args %*% / etc. Would using apply be more efficient than this approach? I have observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3][,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.000 1.000 1. 1.000 1.00 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{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.
Re: [R] Matrix multiplication using apply() or lappy() ?
On Wed, 6 Sep 2006, Christos Hatzis wrote: See ?sweep sweep(a, 2, a[1,],/) That is less efficient than a/rep(a[1,], each=nrow(a)) -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Wednesday, September 06, 2006 11:49 AM To: r-help@stat.math.ethz.ch Subject: [R] Matrix multiplication using apply() or lappy() ? I am trying to divide the columns of a matrix by the first row in the matrix. I have tried to get this using apply and I seem to be missing a concept regarding the apply w/o calling a function but rather command args %*% / etc. Would using apply be more efficient than this approach? I have observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3][,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.000 1.000 1. 1.000 1.00 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{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. -- 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 multiplication using apply() or lappy() ?
This last one could also be written slightly shorter as: t(apply(a, 1, /, a[1,])) On 9/6/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: And here is one more: t(apply(a, 1, function(x) x/a[1,])) On 9/6/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Here are a few possibilities: a - matrix(1:24, 4) # test data a / rep(a[1,], each = 4) a / outer(rep(1, nrow(a)), a[1,]) a %*% diag(1/a[1,]) sweep(a, 2, a[1,], /) On 9/6/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I am trying to divide the columns of a matrix by the first row in the matrix. I have tried to get this using apply and I seem to be missing a concept regarding the apply w/o calling a function but rather command args %*% / etc. Would using apply be more efficient than this approach? I have observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3][,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.000 1.000 1. 1.000 1.00 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{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.
Re: [R] Matrix multiplication using apply() or lappy() ?
Yet another one using the idempotent apply in reshape package that eliminates the transpose: library(reshape) iapply(a, 1, /, a[1,]) On 9/6/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: This last one could also be written slightly shorter as: t(apply(a, 1, /, a[1,])) On 9/6/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: And here is one more: t(apply(a, 1, function(x) x/a[1,])) On 9/6/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Here are a few possibilities: a - matrix(1:24, 4) # test data a / rep(a[1,], each = 4) a / outer(rep(1, nrow(a)), a[1,]) a %*% diag(1/a[1,]) sweep(a, 2, a[1,], /) On 9/6/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I am trying to divide the columns of a matrix by the first row in the matrix. I have tried to get this using apply and I seem to be missing a concept regarding the apply w/o calling a function but rather command args %*% / etc. Would using apply be more efficient than this approach? I have observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3][,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.000 1.000 1. 1.000 1.00 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{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.
Re: [R] Matrix multiplication using apply() or lappy() ?
Prof. Brian Ripley wrote: On Wed, 6 Sep 2006, Christos Hatzis wrote: See ?sweep sweep(a, 2, a[1,],/) That is less efficient than a/rep(a[1,], each=nrow(a)) *My* first instinct was to use t(t(a)/a[1,]) (which has not heretofore been suggested). This seems to be more efficient still (at least in respect of Prof. Grothendieck's toy example) by between 20 and 25 percent: a - matrix(1:24,4) system.time(for(i in 1:1000) junk - a / rep(a[1,], each = 4)) [1] 0.690 0.080 1.051 0.000 0.000 system.time(for(i in 1:1000) junk - t(t(a)/a[1,])) [1] 0.520 0.120 0.647 0.000 0.000 system.time(for(i in 1:1) junk - a / rep(a[1,], each = 4)) [1] 7.08 0.99 10.08 0.00 0.00 system.time(for(i in 1:1) junk - t(t(a)/a[1,])) [1] 5.530 0.940 7.856 0.000 0.000 cheers, Rolf Turner __ 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 using apply() or lappy() ?
The apply was exactly what I was after. And, I will check out the others as well. great tips! Gabor Grothendieck [EMAIL PROTECTED] 09/06/2006 11:11 AM To [EMAIL PROTECTED] [EMAIL PROTECTED] cc r-help@stat.math.ethz.ch Subject Re: [R] Matrix multiplication using apply() or lappy() ? This last one could also be written slightly shorter as: t(apply(a, 1, /, a[1,])) On 9/6/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: And here is one more: t(apply(a, 1, function(x) x/a[1,])) On 9/6/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Here are a few possibilities: a - matrix(1:24, 4) # test data a / rep(a[1,], each = 4) a / outer(rep(1, nrow(a)), a[1,]) a %*% diag(1/a[1,]) sweep(a, 2, a[1,], /) On 9/6/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I am trying to divide the columns of a matrix by the first row in the matrix. I have tried to get this using apply and I seem to be missing a concept regarding the apply w/o calling a function but rather command args %*% / etc. Would using apply be more efficient than this approach? I have observed examples in the archives using this type of approach. Does anybody have a snippet of a call to apply() that would accomplish this as well? Thanks! seed=50 $a = array(rnorm(20),dim=c(4,5)) $b = matrix(a[1,],dim(a)[1],dim(a)[2],byrow=T) $a [,1] [,2] [,3][,4] [,5] [1,] -1.3682810 -0.4314462 1.57572752 0.67928882 -0.3672346 [2,] 0.4328180 0.6556479 0.64289931 0.08983289 0.1852306 [3,] -0.8113932 0.3219253 0.08976065 -2.99309008 0.5818237 [4,] 1.4441013 -0.7838389 0.27655075 0.28488295 1.3997368 $a/b [,1] [,2] [,3] [,4] [,5] [1,] 1.000 1.000 1. 1.000 1.00 [2,] -0.3163225 -1.5196515 0.40800157 0.1322455 -0.504393 [3,] 0.5930018 -0.7461539 0.05696457 -4.4062113 -1.584338 [4,] -1.0554128 1.8167710 0.17550671 0.4193841 -3.811560 CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{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. CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{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 multiplication using apply() or lappy() ?
In terms of speed Toby's original idea was actually the fastest. Here they are decreasing order of the largest timing in each row of system.time. I also tried it with a 100x10 matrix and got almost the same order: library(reshape) system.time(for(i in 1:1000) iapply(a, 1, /, a[1,])) [1] 11.51 0.01 18.65NANA system.time(for(i in 1:1000) t(apply(a, 1, /, a[1,]))) [1] 0.83 0.00 1.36 NA NA system.time(for(i in 1:1000) sweep(a, 2, a[1,], /)) [1] 0.27 0.00 0.39 NA NA system.time(for(i in 1:1000) a/outer(rep(1, nrow(a)), a[1,])) [1] 0.23 0.00 0.39 NA NA system.time(for(i in 1:1000) a %*% diag(1/a[1,])) [1] 0.25 0.00 0.38 NA NA system.time(for(i in 1:1000) a/rep(a[1,], each = nrow(a))) [1] 0.09 0.00 0.16 NA NA system.time(for(i in 1:1000) t(t(a)/a[1,])) [1] 0.10 0.00 0.13 NA NA system.time(for(i in 1:1000) a/matrix(a[1,], nrow(a), ncol(a), byrow = TRUE)) [1] 0.05 0.00 0.12 NA NA On 9/6/06, Rolf Turner [EMAIL PROTECTED] wrote: Prof. Brian Ripley wrote: On Wed, 6 Sep 2006, Christos Hatzis wrote: See ?sweep sweep(a, 2, a[1,],/) That is less efficient than a/rep(a[1,], each=nrow(a)) *My* first instinct was to use t(t(a)/a[1,]) (which has not heretofore been suggested). This seems to be more efficient still (at least in respect of Prof. Grothendieck's toy example) by between 20 and 25 percent: a - matrix(1:24,4) system.time(for(i in 1:1000) junk - a / rep(a[1,], each = 4)) [1] 0.690 0.080 1.051 0.000 0.000 system.time(for(i in 1:1000) junk - t(t(a)/a[1,])) [1] 0.520 0.120 0.647 0.000 0.000 system.time(for(i in 1:1) junk - a / rep(a[1,], each = 4)) [1] 7.08 0.99 10.08 0.00 0.00 system.time(for(i in 1:1) junk - t(t(a)/a[1,])) [1] 5.530 0.940 7.856 0.000 0.000 cheers, Rolf Turner __ 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 multiplication using apply() or lappy() ?
What version of R was this? In 2.4.0 alpha a - matrix(1:24,4) system.time(for(i in 1:1000) junk - a / rep(a[1,], each = 4)) [1] 0.014 0.000 0.014 0.000 0.000 system.time(for(i in 1:1000) junk - t(t(a)/a[1,])) [1] 0.057 0.000 0.058 0.000 0.000 shows a large margin the other way, which increases with bigger matrices a - matrix(pi*1:100, 100, 1000) system.time(for(i in 1:1000) junk - t(t(a)/a[1,])) [1] 18.329 2.238 20.595 0.000 0.000 system.time(for(i in 1:1000) junk - a / rep(a[1,], each = 4)) [1] 2.589 1.021 3.610 0.000 0.000 On Wed, 6 Sep 2006, Rolf Turner wrote: Prof. Brian Ripley wrote: On Wed, 6 Sep 2006, Christos Hatzis wrote: See ?sweep sweep(a, 2, a[1,],/) That is less efficient than a/rep(a[1,], each=nrow(a)) *My* first instinct was to use t(t(a)/a[1,]) (which has not heretofore been suggested). This seems to be more efficient still (at least in respect of Prof. Grothendieck's toy example) by between 20 and 25 percent: a - matrix(1:24,4) system.time(for(i in 1:1000) junk - a / rep(a[1,], each = 4)) [1] 0.690 0.080 1.051 0.000 0.000 system.time(for(i in 1:1000) junk - t(t(a)/a[1,])) [1] 0.520 0.120 0.647 0.000 0.000 system.time(for(i in 1:1) junk - a / rep(a[1,], each = 4)) [1] 7.08 0.99 10.08 0.00 0.00 system.time(for(i in 1:1) junk - t(t(a)/a[1,])) [1] 5.530 0.940 7.856 0.000 0.000 cheers, Rolf Turner __ 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 Adjoint function
Jessica M. Maia [EMAIL PROTECTED] writes: Hi there, I'm new to R and despite searching today, I can't find a function which will compute the adjoint of a matrix A. Does this adjoint function exist in R? I don't think so, but the adjoint matrix is also known as the conjugate transpose and we do have t() and Conj(). (If your matrix is real, t() will do.) -- 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.
Re: [R] matrix Adjoint function
On Mon, 28 Aug 2006, Jessica M. Maia wrote: Sorry but I wasn't very clear in my previous message. I want to compute the adjoint of a real matrix A, which is the transpose of the cofactor matrix of A. There is an example here: http://www.mathwords.com/a/adjoint.htm Does R have a function which will compute the cofactor of matrix A or the adjoint of a real matrix A? If that's what you mean by adjoint, then the adjoint is the inverse multiplied by the determinant. adjoint-function(A) det(A)*solve(A) -thomas Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle __ 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 Adjoint function
Try: RSiteSearch(cofactor) On 8/28/06, Jessica M. Maia [EMAIL PROTECTED] wrote: Sorry but I wasn't very clear in my previous message. I want to compute the adjoint of a real matrix A, which is the transpose of the cofactor matrix of A. There is an example here: http://www.mathwords.com/a/adjoint.htm Does R have a function which will compute the cofactor of matrix A or the adjoint of a real matrix A? 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. __ 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 Adjoint function
Sometimes the term adjoint matrix refers to the matrix of cofactors, that is, the matrix of signed determinants of n-1 x n-1 dimensional submatrices. This is just the inverse multiplied by the determinant. As with both the inverse and determinant, if this is part of a larger computation there are often better ways to solve the problem avoiding explicit computation of determinants or inverses. Reid Huntsinger Peter Dalgaard wrote: Jessica M. Maia [EMAIL PROTECTED] writes: Hi there, I'm new to R and despite searching today, I can't find a function which will compute the adjoint of a matrix A. Does this adjoint function exist in R? I don't think so, but the adjoint matrix is also known as the conjugate transpose and we do have t() and Conj(). (If your matrix is real, t() will do.) __ 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 binary for Mac OS X
Just checked the list from Bristol. Matrix 0.995-11 is on there. Regards, Rob On Jul 13, 2006, at 1:47 AM, A.R. Criswell wrote: Hello all, I can't seem to find package Matrix for Mac OS X. The R package installer, when pointed to Bristol (UK) does not have Matrix amongst its list. Thank you __ 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 __ 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
Re: [R] matrix selection return types
jim holtman a écrit : z1 - m[m[,1] == 2,, drop=FALSE] What is the problem you are trying to solve? It was not really a big problem. Just that without the drop argument, The changing returned type complicated the next computations. drop=TRUE makes it homogeneous, which is what I needed. Thanks for your answer. __ 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
Re: [R] matrix log
I'm not aware of any R implementation of a logarithm of a matrix. I've seen discussions on this list of the difficulties involved in computing functions of matrices, but I know of nothing. (The 'nlme' package has 'pdLogChol', which may be related, but I think it's different.) I wish you luck, and I hope you will find the time to package it for the rest of us. Best Wishes, Spencer Graves [EMAIL PROTECTED] wrote: Dear R users, Has anyone implemented a matrix log function in R similar to the function logm() in Matlab? I did a quick R site search and browsed the contributed packages to no avail. The octave function is far too simplistic and fails for the Matlab test matrix. Ideally, the code of Cheng, Higham, and Laub (2001) or something similar could be utilized. Just checking before I spend time porting code. cheers... Brandon Translational Medicine Genetics GlaxoSmithKline [[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 __ 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
Re: [R] matrix selection return types
Hi On 15 Jun 2006 at 15:41, [EMAIL PROTECTED] wrote: Date sent: Thu, 15 Jun 2006 15:41:14 +0200 From: [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject:[R] matrix selection return types Dear Rusers, I would like some comments about the following results (under R-2.2.0) m = matrix(1:6 , 2 , 3) m [,1] [,2] [,3] [1,]135 [2,]246 z1 = m[(m[,1]==2),] z1 [1] 2 4 6 is.matrix(z1) [1] FALSE z2 = m[(m[,1]==0),] z2 [,1] [,2] [,3] is.matrix(z2) [1] TRUE Considered together, I'm a bit surprised about the returned types from z1 and z2. I would not have been surprised if z1 would still have been a matrix, or z2=NULL. If you want z1 to be matrix see argument drop in ?[ z1 = m[(m[,1]==2),,drop=F] however why matrix is retained in second case i am not sure. Probably only if the result has exactly one dimension it is stripped from dim attribute by default. HTH Petr There is certainly a logic behind this choice but it's not very clear for me, so any help/comment appreciated. Thanks Vincent __ 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 Petr Pikal [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
Re: [R] matrix selection return types
m = matrix(1:6 , 2 , 3) ?'[' z1 - m[m[,1] == 2,] z1 [1] 2 4 6 is.matrix(z1) [1] FALSE z1 - m[m[,1] == 2,, drop=FALSE] z1 [,1] [,2] [,3] [1,]246 is.matrix(z1) [1] TRUE On 6/15/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Dear Rusers, I would like some comments about the following results (under R-2.2.0) m = matrix(1:6 , 2 , 3) m [,1] [,2] [,3] [1,]135 [2,]246 z1 = m[(m[,1]==2),] z1 [1] 2 4 6 is.matrix(z1) [1] FALSE z2 = m[(m[,1]==0),] z2 [,1] [,2] [,3] is.matrix(z2) [1] TRUE Considered together, I'm a bit surprised about the returned types from z1 and z2. I would not have been surprised if z1 would still have been a matrix, or z2=NULL. There is certainly a logic behind this choice but it's not very clear for me, so any help/comment appreciated. Thanks Vincent __ 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 -- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What is the problem you are trying to solve? [[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
Re: [R] Matrix in 3D
[EMAIL PROTECTED] wrote: Dear R Users, Is it possible to add another (third) index to matrix (as in MATLAB). For some analysis e.g. finite mixture models is necessary. Simple example i-3 matrix[, , i]-matrixA[, ,i]%*%matrixB[, , i] See ?array Uwe Ligges I would appreciate any help 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 __ 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
Re: [R] Matrix in 3D
Hi there are dozens of examples of this kind of thing in the R-and-octave.txt file, in the contributed docs section of CRAN. See the multidimensional arrays section, near the bottom, for reproduction of all of matlab/octave's array handling capabilities. best wishes Robin On 22 May 2006, at 09:56, Uwe Ligges wrote: [EMAIL PROTECTED] wrote: Dear R Users, Is it possible to add another (third) index to matrix (as in MATLAB). For some analysis e.g. finite mixture models is necessary. Simple example i-3 matrix[, , i]-matrixA[, ,i]%*%matrixB[, , i] See ?array Uwe Ligges I would appreciate any help 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 __ 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 -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743 __ 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
Re: [R] Matrix in 3D
Thank you very much you Robin and Uwe. Od: Robin Hankin [EMAIL PROTECTED] Do: Uwe Ligges [EMAIL PROTECTED] Data: 22 maja 2006 11:04 Temat: Re: [R] Matrix in 3D Hi there are dozens of examples of this kind of thing in the R-and-octave.txt file, in the contributed docs section of CRAN. See the multidimensional arrays section, near the bottom, for reproduction of all of matlab/octave's array handling capabilities. best wishes Robin On 22 May 2006, at 09:56, Uwe Ligges wrote: [EMAIL PROTECTED] wrote: Dear R Users, Is it possible to add another (third) index to matrix (as in MATLAB). For some analysis e.g. finite mixture models is necessary. Simple example i-3 matrix[, , i]-matrixA[, ,i]%*%matrixB[, , i] See ?array Uwe Ligges I would appreciate any help 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 __ 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 -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743 __ 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
Re: [R] matrix transformation into 3 columns
probably ?reshape() could be used in this case; 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 Quoting Rado Bonk [EMAIL PROTECTED]: Dear R-users, I have matrix (mprecip) with headers: dim(mprecip) [1] 6268 170 mprecip date GilzeRijen Eindhoven Volkel ZuidLimburg Arcen Ubachsberg 101/01/1978 NA 0.0 NA 0.1NA NA 201/02/1978 NA 0.0 NA 0.0NA NA 301/03/1978 NA 1.9 NA 0.7NA NA 401/04/1978 NA 3.5 NA 6.9NA 6.0 501/05/1978 NA 1.6 NA 1.8NA 1.3 601/06/1978 NA 0.0 NA 0.0NA NA 701/07/1978 NA 0.0 NA 0.0NA NA 801/08/1978 NA 0.0 NA 0.0NA NA Columns are: DATE and PRECIP values for each station listed in the header. I would like to transform the matrix into three columns (database like) to be able to load the data in the database: STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP . . . . STATION_NAME2 DATE PRECIP STATION_NAME1 DATE PRECIP. . . . STATION_NAME3 DATE PRECIP -- Dr. Radoslav Bonk European Commission - DG Joint Research Centre (JRC) Institute of Environment and Sustainability (IES) LM Unit - Natural Hazards Via E. Fermi, TP 261, 210 20 Ispra (VA), ITALY tel: 0039 0332 78 6013 fax: 0039 0332 78 6653 http://natural-hazards.jrc.it/floods __ 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 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
Re: [R] matrix transformation into 3 columns II.
On Thu, 18 May 2006, Rado Bonk wrote: Dear R-users Sorry for posting the previous message too soon before I have finished it. Hi Rado, I think reshape() of mprecip as a data frame will do it, or stack(), my closest attempts so far were: reshape(mprecip, direction=long, idvar=date, varying=list(names(mprecip)[2:7])) which gets date right, has the data in the right order, but has a time variable instead of the station names, or: stack(mprecip) which doesn't have the dates, but is otherwise in the expected order. The reshape() arguments are a black art ... Best wishes, Roger I have matrix (mprecip) with headers: dim(mprecip) [1] 6268 170 mprecip date GilzeRijen Eindhoven Volkel ZuidLimburg Arcen Ubachsberg 101/01/1978 NA 0.0 NA 0.1NA NA 201/02/1978 NA 0.0 NA 0.0NA NA 301/03/1978 NA 1.9 NA 0.7NA NA 401/04/1978 NA 3.5 NA 6.9NA6.0 501/05/1978 NA 1.6 NA 1.8NA1.3 601/06/1978 NA 0.0 NA 0.0NA NA 701/07/1978 NA 0.0 NA 0.0NA NA 801/08/1978 NA 0.0 NA 0.0NA NA Columns are: DATE and PRECIP values for each station listed in the header. I would like to transform the matrix into three columns (database like) to be able to load the data in the database. Here is the output I would like to get get it, number of rows = ncol x nrow. Output should look like this: STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP STATION_NAME1 DATE PRECIP . . . . STATION_NAME2 DATE PRECIP STATION_NAME2 DATE PRECIP STATION_NAME2 DATE PRECIP . . . STATION_NAME170 DATE PRECIP STATION_NAME170 DATE PRECIP STATION_NAME170 DATE PRECIP STATION_NAME170 DATE PRECIP STATION_NAME170 DATE PRECIP Thanks again for your help. Rado -- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: [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