[R] How to sum only a few elements in a line
Hello, I have a matrix with the numbers 0,1 and 9 I would like to write a function that could sum each line skiping everytime a number 9 appears for example [0 1 0 1 1 9 1] the sum would be 4. However I cannot replace 9 by 0 otherwise after the sum is done I wouldn´t be able to distiguish which ones were real zeros and which ones were nines replaced by zero just to sum. Thank you very much -- View this message in context: http://old.nabble.com/How-to-sum-only-a-few-elements-in-a-line-tp26519740p26519740.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Is there a faster way to do it?
#Mdarts is a matrix 2343x788 #frequencia is a vector 2343x1 # 9 in Mdarts[fri,frj] stands for my missing values which i want to replace by the value in the vector frequencia Mdarts-t(matrix(scan(C:/GWS/CNB/dartg.txt),ncol=nindT,nrow=nm, byrow=T)) frequencia - matrix(scan(C:/GWS/CNB/freq.txt),ncol=1) for (fri in 1:nindT){ for (frj in 1:nm){ Mdarts[fri,frj] - if (Mdarts[fri,frj] == 9) frequencia[frj] else Mdarts[fri,frj] Mdarts[fri,frj] - Mdarts[fri,frj]/1-(frequencia[frj]^2) } } Is there a faster way to it? Maybe using any apply function? Thanks in advance -- View this message in context: http://www.nabble.com/Is-there-a-faster-way-to-do-it--tp26098223p26098223.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Sampling procedure
I would like to divide a vector in 9 groups in a way that each number is present in only one group. In a vector of 783 I would like to divide in 9 different groups of 87 Example - matrix(c(1:783),ncol = 1) s1 - as.matrix(sample(Example,87, re = FALSE)) Example - Example[-s1] s2 - as.matrix(sample(Example,87, re = FALSE)) #however I don´t know how to remove the second group from the Example to continue sampling. There is probably an easy and faster way to do this. Could anybody help me? Thanks -- View this message in context: http://www.nabble.com/Sampling-procedure-tp25909497p25909497.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] help with loops
Hi guys, my doubt is quite simple, I´ll try to explain: test = matrix(0, nrow = 783, ncol = 12) for (x in 1:9){ for (y in 1:12){ ### In the original script for each y its generated a vector (87x1) ### 87 times 9(x) = 783 (equals the number of rows I want to fill in the test matrix } } What I would like to do is to fill the test matrix with the ys in the collumns and the 87 elements of each x in the rows. for example, x = 1 y = 1 test[1:87, 1] = vector generated when y is 1 x = 2 y = 1 test [88:174,1] = another vector generated when y is 2 Could anybody help me? The script is quite long so i couldn´t just paste it here I think what i wrote above is undestandable. Thank you very much -- View this message in context: http://www.nabble.com/help-with-loops-tp25898891p25898891.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] for cycle with uncontinuous numbers
Hi nice people, I would like to do a for cycle but i wish it to assume only the numers 50, 100, 200, 300, 900 and 2343 I tried to do something like x - c(50,100,200,300,900,2343) for (i in x){ #. } But it didn´t work Could anybody help me? Thanks in advance Marcio -- View this message in context: http://www.nabble.com/for-cycle-with-uncontinuous-numbers-tp25630831p25630831.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Teach me how to transpose in R
Hi Hyo, I am kinda of new in R but I think if you use x - as.matrix(data2) #with the numer of collumns you wish y - t(x) it should work Hope I´ve helped Márcio Hyo Lee wrote: Hi guys, I need your help!! My goal is to make a csv file from ncdf file. This is the code i've used : hyo=open.ncdf(C:/CRUTEM3.nc) hyo [1] file C:/CRUTEM3.nc has 4 dimensions: [1] longitude Size: 72 [1] latitude Size: 36 [1] unspecified Size: 1 [1] t Size: 1916 [1] [1] file C:/CRUTEM3.nc has 1 variables: [1] float temp[longitude,latitude,unspecified,t] Longname:Temperature T Missval:2.0004008175e+20 data2=get.var.ncdf(hyo) write.csv(data2,file=C:/ple.csv) But the problem is, I expected this data would be 17000 * 72 (row* col) ; but, it is the other way around. 72*17000 Because the maximum col number in excel is 16383, this cvs file doesn't show all data. Obviously, I need to transpose the matrix.. I tried to use transpose function but failed. bbb=t(data2) Error in t.default(data2) : argument is not a matrix ccc=t(hyo) ccc [1] file has dimensions: Error in if (nc$ndims 0) for (i in 1:nc$ndims) { : argument is of length zero Teach me how to deal with this problem. Thank you very much. -Hyo [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://www.nabble.com/Teach-me-how-to-transpose-in-R-tp25630869p25630947.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] for cycle with uncontinuous numbers
Hi Tobias, thanks for the help, the code I am using is quite long, but basically what I tried to do was test - matrix(0,6,1) x - matrix( c(50,100,200,300,900,2343) ,ncol = 1) for (i in x){ test [i] - (i) } but this code returns NA for all the elements which are not x Tobias Verbeke-2 wrote: Hi nice people, :-) I would like to do a for cycle but i wish it to assume only the numers 50, 100, 200, 300, 900 and 2343 I tried to do something like x - c(50,100,200,300,900,2343) for (i in x){ #. } But it didn´t work If you would use a reproducible code example we could point out where the error comes from; otherwise we can only tell this should work; try x - c(50,100,200,300,900,2343) for (i in x){ cat(i^2, \n) } # 2500 # 1 # 4 # 9 # 81 # 5489649 HTH, Tobias __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://www.nabble.com/for-cycle-with-uncontinuous-numbers-tp25630831p25633906.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] for cycle with uncontinuous numbers
Dear John, What I am trying to do is a genetic analysis and the i in my cycle are the numbers of markers I am testing. I know the code is not right, but what i wanted to do was to fill this 6 row matrix with the numbers in x So basically I wanted the cycle to loop only with the values on x (50,100,200,300,900,2343) and not the the rest of the values Do you know how to do it? In the example I wanted my final test matrix to be [50 100 200 300 900 2343] dim(test) - still 6x1 Thanks for the help jholtman wrote: It is unclear exactly what you are trying to do. Since your 'test' matrix is only 6 rows in length, then when you extend it by trying to store into 50 for example, it will fill in the rest of the new values with NA. Can you explain what you think the code is supposed to do? It is doing exactly what you are asking it to do with the script you provided. On Sun, Sep 27, 2009 at 10:09 AM, Marcio Resende mresende...@yahoo.com.br wrote: Hi Tobias, thanks for the help, the code I am using is quite long, but basically what I tried to do was test - matrix(0,6,1) x - matrix( c(50,100,200,300,900,2343) ,ncol = 1) for (i in x){ test [i] - (i) } but this code returns NA for all the elements which are not x Tobias Verbeke-2 wrote: Hi nice people, :-) I would like to do a for cycle but i wish it to assume only the numers 50, 100, 200, 300, 900 and 2343 I tried to do something like x - c(50,100,200,300,900,2343) for (i in x){ #. } But it didn´t work If you would use a reproducible code example we could point out where the error comes from; otherwise we can only tell this should work; try x - c(50,100,200,300,900,2343) for (i in x){ cat(i^2, \n) } # 2500 # 1 # 4 # 9 # 81 # 5489649 HTH, Tobias __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://www.nabble.com/for-cycle-with-uncontinuous-numbers-tp25630831p25633906.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://www.nabble.com/for-cycle-with-uncontinuous-numbers-tp25630831p25634676.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Check value interval in a if loop
Hi Lucas, try: if(pvalue0.05 pvalue0.1) HTH Marcio Lucas Sevilla García wrote: Hi R community I have a little problem, and I tried to solve it by myself but I couldn't. I building an if loop, and I want to check a value inside an interval. This would be the case: pvalue=0,2999 if(pvalue0.05 or pvalue0.1) as you can see I would like to check in that if loop if my pvalue is inside of that interval(from 0.05 to 0.1), and I tried these options: if(pvalue0.05 or pvalue0.1) (not possible because R don't recognise OR as operator) if(pvalue0.05 || pvalue0.1) (this one is not good enough, cause it fulfills one condition pvalue0.05 but it doesn't get other contidion pvalue0.1) does anyone know a way to stablish a inteval as an statement for my if loop. Thanks in advance Lucas _ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://www.nabble.com/Check-value-interval-in-a-if-loop-tp25635562p25635651.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Count number of zeros in a collumn
I have a matrix 700x2000 which is sampled in each cycle from another matrix 788x2000 with the numbers 0,1 and 9 There is one specific collumn of this matrix, dart[,1977], that usually, after the samplimg procedure has only 1 and 9 (because the zero frequency in this collumn is low). However, when this happens, I want to include an IF conditional in my code. so basically what i wanted to do was to count the number of zeros in this collumn to use this information in my conditional: something like: if (the number of zeros in collumn [,1977] is zero) ... else ... I tried to find out indirectly by the rowsum but because of the sampling procedure the rowsum is not always the same Thank you very much -- View this message in context: http://www.nabble.com/Count-number-of-zeros-in-a-collumn-tp25637516p25637516.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] BLUP with missing data
hello guys, I need to do a BLUP in the simplest model y = Xm + Zg + e however I have missing data in the analysis which I can´t consider as 0(zero). So I need to generate the matrix X'Z, Z'X and Z'Z step by step; I can´t use crossprod(x) #neither X'X - t(x)%*%x because I should skip the elements with missing data in the matrix I´ll try to be more clear, supposing a matrix x and a z dim (t(x)) = 2275 788 dim (z) = 788 1 but I have in my matrix the effect 0(zero) which is not missing, therefore I can´t just replace the missing values by 0(zero) and i can´t just remove it from the matrix because it would unbalance it A way to do it could be generate Z'X step by step e.g Z'X [1, 1] is equal the sum of the product between the elements in the first collumn of X and the first collumn of Z skipping the elements whenever there is a missing data However I can´t do this in R Does anybody know how to do it this way or an easier way to do it? -- View this message in context: http://www.nabble.com/BLUP-with-missing-data-tp25530949p25530949.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] BLUP with missing data
Sorry, I sent it quickly and forgot to thank in advance Marcio Marcio Resende wrote: hello guys, I need to do a BLUP in the simplest model y = Xm + Zg + e however I have missing data in the analysis which I can´t consider as 0(zero). So I need to generate the matrix X'Z, Z'X and Z'Z step by step; I can´t use crossprod(x) #neither X'X - t(x)%*%x because I should skip the elements with missing data in the matrix I´ll try to be more clear, supposing a matrix x and a z dim (t(x)) = 2275 788 dim (z) = 788 1 but I have in my matrix the effect 0(zero) which is not missing, therefore I can´t just replace the missing values by 0(zero) and i can´t just remove it from the matrix because it would unbalance it A way to do it could be generate Z'X step by step e.g Z'X [1, 1] is equal the sum of the product between the elements in the first collumn of X and the first collumn of Z skipping the elements whenever there is a missing data However I can´t do this in R Does anybody know how to do it this way or an easier way to do it? -- View this message in context: http://www.nabble.com/BLUP-with-missing-data-tp25530949p25530951.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Sum of Product in a Matrix
Hi, I am new in R and I don´t know how to sum the product of two elements at the time in a matrix X=[ 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16] I would like to do (1*5+2*6+3*7+4*8) I need to do it step by step because I will further put a conditional in the formula It worked this way x - matrix ( c (1 : 16),ncol = 4) #generating my x matrix qw - matrix (0, ncol = 4, nrow = 4) answer- matrix (0, ncol = 4, nrow = 4) #my final objective is to generate this matrix soma - function (d,q){ (d [q, 1] * d [q, 2]) #a function to multiplicate two collumns and two elements at a time } for (q in 1:4){ m - soma qw [q, 2] - m (d, q) } answer - sum (qw [, 2]) However I am doing this to generate a X'X matrix (since I can´t do t(X)$*$X because i would like to include a conditional in the formula. Threrefore this script above is not good to me) Could anybody help me? Thanks in advance -- View this message in context: http://www.nabble.com/Sum-of-Product-in-a-Matrix-tp25530977p25530977.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] chage the output file name
After I run my script I save all my output e.g. write.table(d, file = C:/PINEheight.txt) write.table(v, file = C:/PINEvolume.txt) write.table(v, file = C:/PINEdiameter.txt) write.table(v, file = C:/PINEdensity.txt) and then I would like to run again with another tree and save again e.g write.table(d, file = C:/POPULUSheight.txt) Is there anyhow I can define a variable as tree name and make it change everytime in all the output files names e.g. write.table(d, file = C:/tree name height.txt) Thanks in advance Márcio -- View this message in context: http://www.nabble.com/chage-the-output-file-name-tp25530892p25530892.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to separate a function by 2 probabilities
Good Mourning, I have a function to generate a matrix as I show part of it; g[j,i]-if (gen[j,i]==0) al1[i,1]+al1[i,1] else ... However i would like that this function occurred with a probability P and that another function (another formula to generate g matrix) with probability P-1 That´s it, if P is .7, i would like that in 70% of the times (for random i and j) the matrix g was generated according to the formula above and in 30% of the times with a different formula which i did not write could anyone help me? Thank you very much Márcio -- View this message in context: http://www.nabble.com/How-to-separate-a-function-by-2-probabilities-tp25491943p25491943.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Incorrect Dimension
I am new in R and i am having trouble here. I´ve already searched in the list but hasn´t helped When i run this script above i get the message Error in gen[j, i] : incorrect number of dimensions. However gen is 1000x200 (ind x loc) and so is g could anybody help me for (i in 1 : loc) { #loc=200 for (j in 1 : ind) { #ind=1000 g1 = function ( gen ) matrix ( if (gen[j, i] == 0) al1 [i, 1] + al1 [i, 1] else if (gen [j, i] == 1) al1 [i, 1] + al2 [i, 1] else if (gen [j, i] == 2) al2 [i, 1] + al2 [i, 1] else 999, ncol = loc, nrow = ind) g2 = function ( gen ) sample (b1, 1, re = T) #b1 is 1x3 and came from a vector 1000X1 (e.g b1 - c(x [1000,1]...) xx = if (runif (1) = (1 - r2)) g1 else g2 g [j, i] = xx (gen [j, i])#g was already generated as an 0 matrix (1000x200) and i would like to replace ##by those functios } } Thank you very much -- View this message in context: http://www.nabble.com/Incorrect-Dimension-tp25502336p25502336.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.