[R] Help with apply
Hello R gurus, I have a simple routine using for loops that i was just trying to make it faster y - rnorm(10) Nindiv - 10 x -matrix(sample(c(0:2),50,re = T),10) gtemp - rnorm(10) ycorr - array(0,c(Nindiv,1)) for (i in 1:Nindiv) { for (k in 1:ncol(x)) { ycorr[i] - y[i] - x[i,k]*gtemp[k] } } It works fine but I wanted to make it faster and I think an apply function could probably solve this problem. Any suggestions? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Help-with-apply-tp3518773p3518773.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] Help with apply
Ok, I am sorry, I was building a reproducible example to post here and I made a mistake. Th second loop (in the variable k) updates ycorr. Therefore, it would be ycorr - rnorm(10) Nindiv - 10 x -matrix(sample(c(0:2),50,re = T),10) gtemp - rnorm(10) for (i in 1:Nindiv) { for (k in 1:ncol(x)) { ycorr[i] - ycorr[i] - x[i,k]*gtemp[k] } } -- View this message in context: http://r.789695.n4.nabble.com/Help-with-apply-tp3518773p3519270.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] Faster way to do it??...using apply?
Hi, I have a simple task, but I am looking for a clever and fast way to do it: I have a vector x with 0,1 or 2 and I want to create another vector y with the same length following the rules: If the element in x is equal to 0, the element in y is equal to 0 If the element in x is equal to 2, the element in y is equal to 1 If the element in x is equal to 1, the element in y is either 0 or 1 (sample from c(0,1)) thus the vector x [,1] [1,]0 [2,]2 [3,]1 [4,]2 [5,]0 [6,]1 [7,]2 could produce the vector y (this is one of the possibilities since y|x=1 is either 0 or 1 y [,1] [1,]0 [2,]1 [3,]1 [4,]1 [5,]0 [6,]0 [7,]1 I know how to do this using for loops but I was wondering if you guys could suggest a better way Thanks -- View this message in context: http://r.789695.n4.nabble.com/Faster-way-to-do-it-using-apply-tp3166161p3166161.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] Faster way to do it??...using apply?
Hi Henrique, Thanks for the fast answer, The only problem in your code, which I think I didn't mention in my message is that I would like one different random sampling procedure for each 1 in my vector The way it was written, it samples only once and replace by every 1: x = as.matrix(c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)) replace(replace(x, x == 1, sample(0:1, 1)), x == 2, 1) [,1] [1,]1 [2,]1 [3,]1 [4,]1 [5,]1 [6,]1 [7,]1 [8,]1 [9,]1 [10,]1 [11,]1 [12,]1 [13,]1 [14,]1 [15,]1 Thanks -- View this message in context: http://r.789695.n4.nabble.com/Faster-way-to-do-it-using-apply-tp3166161p3166203.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] empity value in colnames
Hi Guys. I have a matrix which has names in every other column and the other one is empity () example X1 X10001 X10002 [1,] AG AG G [2,] GG AG A [3,] GG AA A [4,] GG AA A [5,] AG AA A I am creating another matrix (I called here subset) which cbinds information from another matrix (not shown) and a subset of this example matrix above (let's say column 1 and 2) and save as .prn file subset Sequence family clone female male X1 V2 X10001 V4 140003400 540003 10005 22055 A G A G 240011400 540011 10005 22055 G G A G 340014400 540014 10005 22055 G G A A 440042400 540042 10005 22055 G G A A 540057400 540057 10005 22055 A G A A Everytime I do it, it creates a column name (V2 and V4 in bold) where it should be empty (). Do you guys have any clue on how to to this? Thanks -- View this message in context: http://r.789695.n4.nabble.com/empity-value-in-colnames-tp3054564p3054564.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 scan df from a specific word?
Hi R-helpers, I need to read some file with different lines (I don't know the number of lines to skip) and I would like to find a way to start reading the data.frame from the word source. ex: djhsafk asdfhkjash shdfjkash asfhjkash #those lines contain numbers and words, I want to skip then but they have different sizes asdfhjkash asdfhjksa source tret 2 res 3 Can anybody help? Thanks -- View this message in context: http://r.789695.n4.nabble.com/How-to-scan-df-from-a-specific-word-tp3019841p3019841.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] How to scan df from a specific word?
Sorry, the explanation wasn't very good...just to explain better. I am writing a loop to read and process different files in the same script. And what I want to load into a variable is a data.frame that is above the word source in all of my files. So I would like to recognize the word Source in the text fileand read the table bellow source until the next blank line (the file has more written stuff bellow the data frame that I want to read too) Here is an example of the file. I want the df to read from source until the blank line right above the words Analysis of Variance Notice: 37 singularities detected in design matrix. 1 LogL=-2664.01 S2= 1. 8367 df: 2 components constrained 2 LogL=-2269.45 S2= 1. 8367 df 3 LogL=-1698.47 S2= 1. 8367 df 4 LogL=-1252.72 S2= 1. 8367 df 5 LogL=-1013.52 S2= 1. 8367 df 6 LogL=-957.409 S2= 1. 8367 df 7 LogL=-944.252 S2= 1. 8367 df 8 LogL=-939.976 S2= 1. 8367 df 9 LogL=-938.908 S2= 1. 8367 df 10 LogL=-938.798 S2= 1. 8367 df 11 LogL=-938.795 S2= 1. 8367 df 12 LogL=-938.795 S2= 1. 8367 df SourceModel terms Gamma ComponentComp/SE % C Residual 8383 8367 at(type,1).Nfam 62 62 10.1131 10.1131 1.81 0 P at(type,2).Nfam 62 62 28.1153 28.1153 2.16 0 P rep.iblk768768 63.2919 63.2919 10.94 0 P at(type,1).Nfemale 44 44 29.9049 29.9049 2.93 0 P at(type,1).Nclone 2689 2689 109.560 109.560 12.66 0 P at(type,2).Nfemale 44 44 14.0305 14.0305 1.68 0 P Variance 0 0 479.040 479.040 36.23 0 P Variance 0 0 490.580 490.580 17.51 0 P Variance 0 0 469.932 469.932 36.51 0 P Variance 0 0 544.654 544.654 17.86 0 P Analysis of Variance NumDF F_inc 27 mu15860.84 12 culture 1 0.07 10 type 1 29.59 28 culture.rep 6 14.06 30 culture.rep.type 7 2.17 36 at(type,1).Nfam 62 effects fitted -- View this message in context: http://r.789695.n4.nabble.com/How-to-scan-df-from-a-specific-word-tp3019841p3019844.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] How to scan df from a specific word?
Sorry, the explanation wasn't very good...just to explain better. I am writing a loop to read and process every time a different file in the same script. And what I want to load into a variable each time is a data.frame that is bellow the word source in all of my files. So I would like to recognize the word Source in the text fileand read the table bellow source until the next blank line (the file has more written stuff bellow the data frame that I want to read too) Here is an example of the file. I want the df to read from source until the blank line right above the words Analysis of Variance Notice: 37 singularities detected in design matrix. 1 LogL=-2664.01 S2= 1. 8367 df: 2 components constrained 2 LogL=-2269.45 S2= 1. 8367 df 3 LogL=-1698.47 S2= 1. 8367 df 4 LogL=-1252.72 S2= 1. 8367 df 5 LogL=-1013.52 S2= 1. 8367 df 6 LogL=-957.409 S2= 1. 8367 df 7 LogL=-944.252 S2= 1. 8367 df 8 LogL=-939.976 S2= 1. 8367 df 9 LogL=-938.908 S2= 1. 8367 df 10 LogL=-938.798 S2= 1. 8367 df 11 LogL=-938.795 S2= 1. 8367 df 12 LogL=-938.795 S2= 1. 8367 df SourceModel terms Gamma ComponentComp/SE % C Residual 8383 8367 at(type,1).Nfam 62 62 10.1131 10.1131 1.81 0 P at(type,2).Nfam 62 62 28.1153 28.1153 2.16 0 P rep.iblk768768 63.2919 63.2919 10.94 0 P at(type,1).Nfemale 44 44 29.9049 29.9049 2.93 0 P at(type,1).Nclone 2689 2689 109.560 109.560 12.66 0 P at(type,2).Nfemale 44 44 14.0305 14.0305 1.68 0 P Variance 0 0 479.040 479.040 36.23 0 P Variance 0 0 490.580 490.580 17.51 0 P Variance 0 0 469.932 469.932 36.51 0 P Variance 0 0 544.654 544.654 17.86 0 P Analysis of Variance NumDF F_inc 27 mu15860.84 12 culture 1 0.07 10 type 1 29.59 28 culture.rep 6 14.06 30 culture.rep.type 7 2.17 36 at(type,1).Nfam 62 effects fitted -- View this message in context: http://r.789695.n4.nabble.com/How-to-scan-df-from-a-specific-word-tp3019841p3019846.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] Differenciate numbers from reference for rows
So, I am having a tricky reference file to extract information from. The format of the file is x 1 + 4 * 3 + 5 + 6 + 11 * 0.5 So, the elements that are not being multiplied (1, 5 and 6) and the elements before the multiplication sign (4 and 11) means actually the reference for the row in a matrix where I need to extract the element from. The numbers after the multiplication sign are regular numbers Ex: x-matrix(20:35) x [,1] [1,] 20 [2,] 21 [3,] 22 [4,] 23 [5,] 24 [6,] 25 [7,] 26 [8,] 27 [9,] 28 [10,] 29 [11,] 30 [12,] 31 [13,] 32 [14,] 33 [15,] 34 [16,] 35 I would like to read the rows 1,4,5,6 and 11 and sum then. However the numbers in the elements row 4 and 11 are multiplied by 3 and 0.5 So it would be 20 + 23 * 3 + 24 + 25 + 30 * 0.5. And I have this format in different files so I can't do all by hand. Can anybody help me with a script that can differentiate this? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Differenciate-numbers-from-reference-for-rows-tp3019853p3019853.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] Dimnames of array inside loop
any clue?? hello R-helpers, I have an array acuracia - array(NA, dim = c(1, 1, A, B, C)) which is first defined and in the example above with dimensions 1x1xAxBxC. My array is then filled using 3 loops (I am not well familiar yet with lapply or sapply functions so I am still a loop-user): for (i in 1:A){ for (j in 1:B){ for (k in 1:C){ acuracia[,,i,j,k] - correlacao / (sqrt(ac)) } } } then I want to put names using dimnames but filling the names each at a time after each iteration: My first idea was to try acuracia - array(NA, dim = c(1, 1, A,B,C),dimnames = list(acc,NULL, paste(rep., i, sep = ), paste(variable: , j), paste(mark.val = , k)) but it did not work. Can anybody help? Thank you very much -- View this message in context: http://r.789695.n4.nabble.com/Dimnames-of-array-inside-loop-tp1594215p2265736.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] error on Windows OS
I received by email an R package (file.tar.gz) that was created in Linux. The package was already installed in another computer in linux using install.packages and it worked I am not familiar with installing packages but I would like to install it on Windows I downloaded the Rtools29.exe and tryed to install using install.packages(foo.tar.gz, repos=NULL, type=source) but the message was Warning in install.packages(GR_1.0.tar.gz, repos = NULL, type = source) : argument 'lib' is missing: using 'C:\Documents and Settings\mr\My Documents/R/win-library/2.9' 'sh' is not recognized as an internal or external command, operable program or batch file. Warning message: In install.packages(GR_1.0.tar.gz, repos = NULL, type = source) : installation of package 'GR_1.0.tar.gz' had non-zero exit status my questios are: Where shall I save the .tar.gz file?? Do I need to do anything else with the Rtools besides installing (C:/Rtools)?? Is the problem with the way I did or with the package?? Thanks a lot Cheers -- View this message in context: http://r.789695.n4.nabble.com/error-on-Windows-OS-tp2236758p2236758.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] error on Windows OS
Ok, I re-installed the Rtools (now Rtools211 because I was in another computer with R version 2.10) , and the message now when I tried to install the package was Warning: invalid package 'GWSR_1.0.tar.gz' Erro: ERROR: no packages specified Warning message: In install.packages(GWSR_1.0.tar.gz, repos = NULL, type = source) : installation of package 'GWSR_1.0.tar.gz' had non-zero exit status Any Clue? Thanks a lot Cheers -- View this message in context: http://r.789695.n4.nabble.com/error-on-Windows-OS-tp2236758p2237341.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] error on Windows OS
Thanks for all the help, So let me undestand, The Rtools is currently installed in c:\Rtools\bin; c:\Rtools\perl\bin; c:\Rtools\MinGW\bin; %SystemRoot%\system32; %SystemRoot%; %SystemRoot%\System32\Wbem; C:\Program Files (x86)\QuickTime\QTSystem\; C:\Program Files (x86)\SAS\Shared Files\Formats (I don't know why in here) Should I re-install in a C:/Windows/Rtools directory ? Thanks again Cheers -- View this message in context: http://r.789695.n4.nabble.com/error-on-Windows-OS-tp2236758p2237308.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] Counting Frequencies in Data Frame
Hi, I am sure there is an easy way to do it, but I can't find it. I have a data frame that has 15 columns and 7000 rows. The only values inside the data.frame are aa, ab, bb as you can see an example bellow. 1 2 3 1 aa ab ab 2 ab ab ab 3 aa aa aa 4 bb bb bb What I would like to do, is to generate a vector (or another data.frame) with 7000 rows, and 3 columns. In the first column, the information about how many aa, the second about how many ab, and the third about how many bb are there in each line aa ab bb 1 1 2 0 2 0 2 0 3 3 0 0 4 0 0 3 Thank you very much Cheers -- View this message in context: http://r.789695.n4.nabble.com/Counting-Frequencies-in-Data-Frame-tp2221342p2221342.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.