Re: [R] help with matrix
On 19/07/2013 10:17 AM, Kátia Emidio wrote: Hi, I am a beginner at R and I would like to get information from a matrix, where the numbers are larger than or equal to 50m . This means that plants need to be apart at least 50m. I need to get the selection as in a way that I can identify which pair of species are 50m or more apart. here a peace of my matrix: Nauc_calo_1 Nauc_calo_2 Nauc_calo_3 Nauc_calo_1 0 22 36 Nauc_calo_2 22 0 165.8 Nauc_calo_3 36 165.8 0 The which() function with arr.ind=TRUE will identify which matrix entries meet a condition. For example, M - matrix(rnorm(20), 4, 5) M [,1] [,2][,3][,4] [,5] [1,] -0.6947070 1.2079620 0.77996512 -0.04287046 -1.5487528 [2,] -0.2079173 -1.1231086 -0.08336907 1.36860228 0.5846137 [3,] -1.2653964 -0.4028848 0.25331851 -0.22577099 0.1238542 [4,] 2.1689560 -0.4666554 -0.02854676 1.51647060 0.2159416 which(M 1.5, arr.ind=TRUE) row col [1,] 4 1 [2,] 4 4 Duncan Murdoch __ 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 matrix
Hi, I am a beginner at R and I would like to get information from a matrix, where the numbers are larger than or equal to 50m . This means that plants need to be apart at least 50m. I need to get the selection as in a way that I can identify which pair of species are 50m or more apart. here a peace of my matrix: Nauc_calo_1 Nauc_calo_2 Nauc_calo_3 Nauc_calo_1 0 22 36 Nauc_calo_2 22 0 165.8 Nauc_calo_3 36 165.8 0 Thanks -- Kátia Emídio da Silva DSc Eng. Florestal Manaus/AM Forestry Engineer Manaus/AM-Brazil [[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.
Re: [R] help on matrix column removal based on another matrix results
Hello, Try removing the square parenthesis from the left hand side, Vsim - Vsim[, NSErr 0.6] Hope this helps, Rui Barradas Em 20-11-2012 04:07, Irucka Embry escreveu: Hi Rui, how are you? I want to thank you for your assistance again. I'm sorry, but the code that you provided for me did not work this time. The code and the warning message are both below: NSErr - t(matrix(NSEr)) # which is a 1x1000 double matrix Vsim[] - Vsim[ , NSErr 0.6] Warning message: In Vsim[] - Vsim[, NSErr 0.6] : number of items to replace is not a multiple of replacement length Thanks again. Irucka -Original Message- From: Rui Barradas [ruipbarra...@sapo.pt] Sent: 11/19/2012 10:35:00 AM To: iruc...@mail2world.com Cc: r-help@r-project.org Subject: Re: [R] help on matrix column removal based on another matrix results Sorry, the comma is in the wrong place, it should be Vsim[] - Vsim[ , NSErr 0.6] Rui Barradas Em 19-11-2012 16:18, Rui Barradas escreveu: Hello, Try Vsim[] - Vsim[NSErr 0.6, ] Hope this helps, Rui Barradas Em 19-11-2012 14:30, iembry escreveu: Hi everyone, now I am trying to finish writing the code (I had asked for assistance on subtracting arrays) This is what I what I am running in R: source(/home/ie/Documents/TTU/GA_Research/GLUE/R-Project/R_GLUE_Exampl e/NSEr.R) NSEr - function (obs, sim) { {jjh - (as.vector(obs) - sim)^2 Xjjhs - apply(Xjjh, 2, sum) Yii - (obs - mean(obs))^2 Yiis - apply(Yii, 2, sum) NSEr - 1 - (Xjjhs/Yiis) } NSEr} Vsim - read.csv(1000Samples_Vsim.csv, header = TRUE, sep =,) Vsim - as.matrix(Vsim[,-1]) # remove column 1 from analysis Vobs - read.csv(Observed_Flow.csv, header = TRUE, sep =,) Vobs - as.matrix(Vobs[,-1]) # remove column 1 from analysis NSEr - NSEr(Vobs,Vsim); write.table(NSEr, NSEr.csv, sep =,) NSErr - t(matrix(NSEr)) ## select the behavioural simulations and discard the rest Vsim - Vsim[NSErr 0.6] write.table(Vsim, Vsim.csv, sep =,) **Vsim becomes numeric[42016] rather than a double matrix of 101x416. What is the proper way to remove the columns in Vsim where the NSEr for that column is less than 0.6? I am trying to make Vsim a double matrix of 101x416. Thank-you again. Below is the rest of the code in R: ## normalise Qsim and compute the quantiles NSEr - NSEr[NSEr 0.6] write.table(NSEr, NSEr_great_0.6.csv, sep =,) NSEr - NSEr - 0.6 write.table(NSEr, NSEr_minus0.6.csv, sep =,) NSEr - NSEr/sum(NSEr) write.table(NSEr, NSEr_normalized.csv, sep =,) #NSEr = sum(NSEr) limits - apply(Vsim, 1, wtd.quantile, weights = NSEr, probs = c(0.05,0.95), normwt=F) -- View this message in context: http://r.789695.n4.nabble.com/help-on-matrix-column-removal-based-on-an other-matrix-results-tp4650043.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@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. . span id=m2wTlpfont face=Arial, Helvetica, sans-serif size=2 style=font-size:13.5px___BRGet the Free email that has everyone talking at a href=http://www.mail2world.com target=newhttp://www.mail2world.com/abr font color=#99Unlimited Email Storage #150; POP3 #150; Calendar #150; SMS #150; Translator #150; Much More!/font/font/span __ 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 on matrix column removal based on another matrix results
Hi Rui, thank-you very much! That worked perfectly. Irucka -Original Message- From: Rui Barradas [ruipbarra...@sapo.pt] Sent: 11/20/2012 5:42:36 AM To: iruc...@mail2world.com Cc: r-help@r-project.org Subject: Re: [R] help on matrix column removal based on another matrix results Hello, Try removing the square parenthesis from the left hand side, Vsim - Vsim[, NSErr 0.6] Hope this helps, Rui Barradas Em 20-11-2012 04:07, Irucka Embry escreveu: Hi Rui, how are you? I want to thank you for your assistance again. I'm sorry, but the code that you provided for me did not work this time. The code and the warning message are both below: NSErr - t(matrix(NSEr)) # which is a 1x1000 double matrix Vsim[] - Vsim[ , NSErr 0.6] Warning message: In Vsim[] - Vsim[, NSErr 0.6] : number of items to replace is not a multiple of replacement length Thanks again. Irucka -Original Message- From: Rui Barradas [ruipbarra...@sapo.pt] Sent: 11/19/2012 10:35:00 AM To: iruc...@mail2world.com Cc: r-help@r-project.org Subject: Re: [R] help on matrix column removal based on another matrix results Sorry, the comma is in the wrong place, it should be Vsim[] - Vsim[ , NSErr 0.6] Rui Barradas Em 19-11-2012 16:18, Rui Barradas escreveu: Hello, Try Vsim[] - Vsim[NSErr 0.6, ] Hope this helps, Rui Barradas Em 19-11-2012 14:30, iembry escreveu: Hi everyone, now I am trying to finish writing the code (I had asked for assistance on subtracting arrays) This is what I what I am running in R: source(/home/ie/Documents/TTU/GA_Research/GLUE/R-Project/R_GLUE_Exampl e/NSEr.R) NSEr - function (obs, sim) { {jjh - (as.vector(obs) - sim)^2 Xjjhs - apply(Xjjh, 2, sum) Yii - (obs - mean(obs))^2 Yiis - apply(Yii, 2, sum) NSEr - 1 - (Xjjhs/Yiis) } NSEr} Vsim - read.csv(1000Samples_Vsim.csv, header = TRUE, sep =,) Vsim - as.matrix(Vsim[,-1]) # remove column 1 from analysis Vobs - read.csv(Observed_Flow.csv, header = TRUE, sep =,) Vobs - as.matrix(Vobs[,-1]) # remove column 1 from analysis NSEr - NSEr(Vobs,Vsim); write.table(NSEr, NSEr.csv, sep =,) NSErr - t(matrix(NSEr)) ## select the behavioural simulations and discard the rest Vsim - Vsim[NSErr 0.6] write.table(Vsim, Vsim.csv, sep =,) **Vsim becomes numeric[42016] rather than a double matrix of 101x416. What is the proper way to remove the columns in Vsim where the NSEr for that column is less than 0.6? I am trying to make Vsim a double matrix of 101x416. Thank-you again. Below is the rest of the code in R: ## normalise Qsim and compute the quantiles NSEr - NSEr[NSEr 0.6] write.table(NSEr, NSEr_great_0.6.csv, sep =,) NSEr - NSEr - 0.6 write.table(NSEr, NSEr_minus0.6.csv, sep =,) NSEr - NSEr/sum(NSEr) write.table(NSEr, NSEr_normalized.csv, sep =,) #NSEr = sum(NSEr) limits - apply(Vsim, 1, wtd.quantile, weights = NSEr, probs = c(0.05,0.95), normwt=F) -- View this message in context: http://r.789695.n4.nabble.com/help-on-matrix-column-removal-based-on-an other-matrix-results-tp4650043.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@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. . span id=m2wTlpfont face=Arial, Helvetica, sans-serif size=2 style=font-size:13.5px__ _BRGet the Free email that has everyone talking at a href=http://www.mail2world.com target=newhttp://www.mail2world.com/abr font color=#99Unlimited Email Storage - POP3 - Calendar - SMS - Translator - Much More!/font/font/span . span id=m2wTlpfont face=Arial, Helvetica, sans-serif size=2 style=font-size:13.5px___BRGet the Free email that has everyone talking at a href=http://www.mail2world.com target=newhttp://www.mail2world.com/abr font color=#99Unlimited Email Storage #150; POP3 #150; Calendar #150; SMS #150; Translator #150; Much More!/font/font/span [[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.
[R] help on matrix column removal based on another matrix results
Hi everyone, now I am trying to finish writing the code (I had asked for assistance on subtracting arrays) This is what I what I am running in R: source(/home/ie/Documents/TTU/GA_Research/GLUE/R-Project/R_GLUE_Example/NSEr.R) NSEr - function (obs, sim) { {jjh - (as.vector(obs) - sim)^2 Xjjhs - apply(Xjjh, 2, sum) Yii - (obs - mean(obs))^2 Yiis - apply(Yii, 2, sum) NSEr - 1 - (Xjjhs/Yiis) } NSEr} Vsim - read.csv(1000Samples_Vsim.csv, header = TRUE, sep =,) Vsim - as.matrix(Vsim[,-1]) # remove column 1 from analysis Vobs - read.csv(Observed_Flow.csv, header = TRUE, sep =,) Vobs - as.matrix(Vobs[,-1]) # remove column 1 from analysis NSEr - NSEr(Vobs,Vsim); write.table(NSEr, NSEr.csv, sep =,) NSErr - t(matrix(NSEr)) ## select the behavioural simulations and discard the rest Vsim - Vsim[NSErr 0.6] write.table(Vsim, Vsim.csv, sep =,) **Vsim becomes numeric[42016] rather than a double matrix of 101x416. What is the proper way to remove the columns in Vsim where the NSEr for that column is less than 0.6? I am trying to make Vsim a double matrix of 101x416. Thank-you again. Below is the rest of the code in R: ## normalise Qsim and compute the quantiles NSEr - NSEr[NSEr 0.6] write.table(NSEr, NSEr_great_0.6.csv, sep =,) NSEr - NSEr - 0.6 write.table(NSEr, NSEr_minus0.6.csv, sep =,) NSEr - NSEr/sum(NSEr) write.table(NSEr, NSEr_normalized.csv, sep =,) #NSEr = sum(NSEr) limits - apply(Vsim, 1, wtd.quantile, weights = NSEr, probs = c(0.05,0.95), normwt=F) -- View this message in context: http://r.789695.n4.nabble.com/help-on-matrix-column-removal-based-on-another-matrix-results-tp4650043.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 on matrix column removal based on another matrix results
Hello, Try Vsim[] - Vsim[NSErr 0.6, ] Hope this helps, Rui Barradas Em 19-11-2012 14:30, iembry escreveu: Hi everyone, now I am trying to finish writing the code (I had asked for assistance on subtracting arrays) This is what I what I am running in R: source(/home/ie/Documents/TTU/GA_Research/GLUE/R-Project/R_GLUE_Example/NSEr.R) NSEr - function (obs, sim) { {jjh - (as.vector(obs) - sim)^2 Xjjhs - apply(Xjjh, 2, sum) Yii - (obs - mean(obs))^2 Yiis - apply(Yii, 2, sum) NSEr - 1 - (Xjjhs/Yiis) } NSEr} Vsim - read.csv(1000Samples_Vsim.csv, header = TRUE, sep =,) Vsim - as.matrix(Vsim[,-1]) # remove column 1 from analysis Vobs - read.csv(Observed_Flow.csv, header = TRUE, sep =,) Vobs - as.matrix(Vobs[,-1]) # remove column 1 from analysis NSEr - NSEr(Vobs,Vsim); write.table(NSEr, NSEr.csv, sep =,) NSErr - t(matrix(NSEr)) ## select the behavioural simulations and discard the rest Vsim - Vsim[NSErr 0.6] write.table(Vsim, Vsim.csv, sep =,) **Vsim becomes numeric[42016] rather than a double matrix of 101x416. What is the proper way to remove the columns in Vsim where the NSEr for that column is less than 0.6? I am trying to make Vsim a double matrix of 101x416. Thank-you again. Below is the rest of the code in R: ## normalise Qsim and compute the quantiles NSEr - NSEr[NSEr 0.6] write.table(NSEr, NSEr_great_0.6.csv, sep =,) NSEr - NSEr - 0.6 write.table(NSEr, NSEr_minus0.6.csv, sep =,) NSEr - NSEr/sum(NSEr) write.table(NSEr, NSEr_normalized.csv, sep =,) #NSEr = sum(NSEr) limits - apply(Vsim, 1, wtd.quantile, weights = NSEr, probs = c(0.05,0.95), normwt=F) -- View this message in context: http://r.789695.n4.nabble.com/help-on-matrix-column-removal-based-on-another-matrix-results-tp4650043.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@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 on matrix column removal based on another matrix results
Sorry, the comma is in the wrong place, it should be Vsim[] - Vsim[ , NSErr 0.6] Rui Barradas Em 19-11-2012 16:18, Rui Barradas escreveu: Hello, Try Vsim[] - Vsim[NSErr 0.6, ] Hope this helps, Rui Barradas Em 19-11-2012 14:30, iembry escreveu: Hi everyone, now I am trying to finish writing the code (I had asked for assistance on subtracting arrays) This is what I what I am running in R: source(/home/ie/Documents/TTU/GA_Research/GLUE/R-Project/R_GLUE_Example/NSEr.R) NSEr - function (obs, sim) { {jjh - (as.vector(obs) - sim)^2 Xjjhs - apply(Xjjh, 2, sum) Yii - (obs - mean(obs))^2 Yiis - apply(Yii, 2, sum) NSEr - 1 - (Xjjhs/Yiis) } NSEr} Vsim - read.csv(1000Samples_Vsim.csv, header = TRUE, sep =,) Vsim - as.matrix(Vsim[,-1]) # remove column 1 from analysis Vobs - read.csv(Observed_Flow.csv, header = TRUE, sep =,) Vobs - as.matrix(Vobs[,-1]) # remove column 1 from analysis NSEr - NSEr(Vobs,Vsim); write.table(NSEr, NSEr.csv, sep =,) NSErr - t(matrix(NSEr)) ## select the behavioural simulations and discard the rest Vsim - Vsim[NSErr 0.6] write.table(Vsim, Vsim.csv, sep =,) **Vsim becomes numeric[42016] rather than a double matrix of 101x416. What is the proper way to remove the columns in Vsim where the NSEr for that column is less than 0.6? I am trying to make Vsim a double matrix of 101x416. Thank-you again. Below is the rest of the code in R: ## normalise Qsim and compute the quantiles NSEr - NSEr[NSEr 0.6] write.table(NSEr, NSEr_great_0.6.csv, sep =,) NSEr - NSEr - 0.6 write.table(NSEr, NSEr_minus0.6.csv, sep =,) NSEr - NSEr/sum(NSEr) write.table(NSEr, NSEr_normalized.csv, sep =,) #NSEr = sum(NSEr) limits - apply(Vsim, 1, wtd.quantile, weights = NSEr, probs = c(0.05,0.95), normwt=F) -- View this message in context: http://r.789695.n4.nabble.com/help-on-matrix-column-removal-based-on-another-matrix-results-tp4650043.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@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@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 on matrix column removal based on another matrix results
Hi Rui, how are you? I want to thank you for your assistance again. I'm sorry, but the code that you provided for me did not work this time. The code and the warning message are both below: NSErr - t(matrix(NSEr)) # which is a 1x1000 double matrix Vsim[] - Vsim[ , NSErr 0.6] Warning message: In Vsim[] - Vsim[, NSErr 0.6] : number of items to replace is not a multiple of replacement length Thanks again. Irucka -Original Message- From: Rui Barradas [ruipbarra...@sapo.pt] Sent: 11/19/2012 10:35:00 AM To: iruc...@mail2world.com Cc: r-help@r-project.org Subject: Re: [R] help on matrix column removal based on another matrix results Sorry, the comma is in the wrong place, it should be Vsim[] - Vsim[ , NSErr 0.6] Rui Barradas Em 19-11-2012 16:18, Rui Barradas escreveu: Hello, Try Vsim[] - Vsim[NSErr 0.6, ] Hope this helps, Rui Barradas Em 19-11-2012 14:30, iembry escreveu: Hi everyone, now I am trying to finish writing the code (I had asked for assistance on subtracting arrays) This is what I what I am running in R: source(/home/ie/Documents/TTU/GA_Research/GLUE/R-Project/R_GLUE_Exampl e/NSEr.R) NSEr - function (obs, sim) { {jjh - (as.vector(obs) - sim)^2 Xjjhs - apply(Xjjh, 2, sum) Yii - (obs - mean(obs))^2 Yiis - apply(Yii, 2, sum) NSEr - 1 - (Xjjhs/Yiis) } NSEr} Vsim - read.csv(1000Samples_Vsim.csv, header = TRUE, sep =,) Vsim - as.matrix(Vsim[,-1]) # remove column 1 from analysis Vobs - read.csv(Observed_Flow.csv, header = TRUE, sep =,) Vobs - as.matrix(Vobs[,-1]) # remove column 1 from analysis NSEr - NSEr(Vobs,Vsim); write.table(NSEr, NSEr.csv, sep =,) NSErr - t(matrix(NSEr)) ## select the behavioural simulations and discard the rest Vsim - Vsim[NSErr 0.6] write.table(Vsim, Vsim.csv, sep =,) **Vsim becomes numeric[42016] rather than a double matrix of 101x416. What is the proper way to remove the columns in Vsim where the NSEr for that column is less than 0.6? I am trying to make Vsim a double matrix of 101x416. Thank-you again. Below is the rest of the code in R: ## normalise Qsim and compute the quantiles NSEr - NSEr[NSEr 0.6] write.table(NSEr, NSEr_great_0.6.csv, sep =,) NSEr - NSEr - 0.6 write.table(NSEr, NSEr_minus0.6.csv, sep =,) NSEr - NSEr/sum(NSEr) write.table(NSEr, NSEr_normalized.csv, sep =,) #NSEr = sum(NSEr) limits - apply(Vsim, 1, wtd.quantile, weights = NSEr, probs = c(0.05,0.95), normwt=F) -- View this message in context: http://r.789695.n4.nabble.com/help-on-matrix-column-removal-based-on-an other-matrix-results-tp4650043.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@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. . span id=m2wTlpfont face=Arial, Helvetica, sans-serif size=2 style=font-size:13.5px___BRGet the Free email that has everyone talking at a href=http://www.mail2world.com target=newhttp://www.mail2world.com/abr font color=#99Unlimited Email Storage #150; POP3 #150; Calendar #150; SMS #150; Translator #150; Much More!/font/font/span [[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.
Re: [R] Help: a matrix exponential
This is a matrix algebra problem, not an R problem. ej fails because betaligne and ZIcolone are not conformable matrices. dim(betaligne) 1 2 dim(ZIcolone) 76 1 Let a matrix be m x n, then you can only matrix multiply it with a matrix that is n x k (where m and k are arbitrary positive integers, but n must be the same). The resulting matrix will be m x k. So you can postmultiply t(betaligne) with t(ZIcolone) or ZIcolone with betaligne. Everything else is not conformable. HTH, Daniel Hi, I have a problem with the final results fj ; fj must firstly be a vector indexed by j = 8, and the same avoire Chifre (9.5), beta0=64.91151 beta1=17.70393 beta=c(beta0, beta1) j=1:8 n=76 i=1:n zi-c(26, 26, 26, 28, 28, 28, 28, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 38, 38, 38, 38, 38, 38, 38, 38) Xi-c(5.79, 1579.52, 2323.7, 68.85, 426.07, 110.29, 108.29, 1067.6, 17.05, 22.66, 21.02, 175.88, 139.07, 144.12, 20.46, 43.4, 194.9, 47.3, 7.74, 0.4, 82.85, 9.88, 89.29, 215.1, 2.75, 0.79, 15.93, 3.91, 0.27, 0.69, 100.58, 27.8, 13.95, 53.24, 0.96, 4.15, 0.19, 0.78, 8.01, 31.75, 7.35, 6.5, 8.27, 33.91, 32.52, 3.16, 4.85, 2.78, 4.67, 1.31, 12.06, 36.71, 72.89, 1.97, 0.59, 2.58, 1.69, 2.71, 25.5, 0.35, 0.99, 3.99, 3.67, 2.07, 0.96, 5.35, 2.9, 13.77, 0.47, 0.73, 1.4, 0.74, 0.39, 1.13, 0.09, 2.38) ZI-log(zi) betaligne-t(as.matrix(-beta)) ZIcolone-as.matrix(ZI) k=8 ej-1/k*sum(exp(betaligne %*% ZIcolone)*Xi) Error in betaligne %*% ZIcolone : non-conformable arguments PLEASE HELP ME THANKS Goual -- View this message in context: http://r.789695.n4.nabble.com/Help-a-matrix-exponential-tp4642585p4642589.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, please! matrix operations inside 3 nested loops
thank you for your help. my input data looks like this (tab separated): Ind.nr. Pop.nr. scm266 rms1280 scm247 rms1107 1 101 305 318 222 135 1 101 305 318 231 135 2 101 305 313 999 96 2 101 305 321 999 130 3 101 305 324 231 135 3 101 305 324 231 135 4 101 305 313 230 126 4 101 305 313 230 135 6 101 305 313 231 135 6 101 305 321 231 135 it is a dataset with genetic marker alleles for single individuals. the first row is the header, all following rows are individuals. 2 rows count for 1 individual. first colum is the individual's number, second colum is the number for the population the individual comes from, and all following colums are different genetic markers. what i want to do with this data in R, is to compare one individual with each of the other individuals, allele-wise. there are five possibilities: the two compared individuals share 4,3,2,1,0 alleles of the currently examined marker (=colum). for each shared allele this pair of individuals shall get 1 scoring point. for each pair of individuals, all scoring points shall be summarized over all markers. my code again, modified according to your suggestions: #1) read in data: daten-read.table('K:/Analysen/STRUCTURE/test.txt', header=TRUE, sep=\t) daten-as.data.frame(daten) #2) create empty matrix: indxind-matrix(0,nrow=617, ncol=617) indxind[1:20,1:19] #3) compare cells to each other, score: #for the whole dataset: s in 3:34, z1 in 1:617, z2 in 1:617 for (s in 3:6) { #walks though the matrix colum by colum, starting at colum 3 for (z1 in 1:6) { #for each current colum, take one row (z1)... for (z2 in 1:6) { #...and compare it to another row (z2) of the current colum if (z1!=z2) {topf-indxind[z1,z2] if (daten[2*z1-1,s]==daten[2*z2-1,s]) topf-topf+1 #actually, 2 rows make up 1 individual, if (daten[2*z1-1,s]==daten[2*z2,s]) topf-topf+1 #therefore i compare 2 rows if (daten[2*z1,s]==daten[2*z2-1,s]) topf-topf+1 #with another 2 rows if (daten[2*z1,s]==daten[2*z2,s]) topf-topf+1 indxind[z1,z2]-topf indxind[z2,z1]-topf } #print(c(s,z1,z2,indxind[1,2])) ##counts s, z1 and z2 properly, but gives always 8 for indxind[1,2] } #indxind[1:5,1:5] #empty matrix } #indxind[1:5,1:5] #empty matrix } #4) check: indxind[1:5,1:5] @ Michael Weylandt: i've done my best with regard to the big picture of my algorithm and the small reproducible example. i hope both is sufficient. @ Petr Pikal-3: in this case, there are only numerical values, but it's a useful hint for my other codes. @ Petr Pikal-3 and Berend Hasselman: initializing indxind with 0's instead of NAs helps, it fills something in indxind now. but it does the calculation only for the first marker (colum 3), afterwards i get an error: Fehler in if (daten[2 * z1 - 1, s] == daten[2 * z2 - 1, s]) topf - topf + : Fehlender Wert, wo TRUE/FALSE nötig ist Error in if (daten[2 * z1 - 1, s] == daten[2 * z2 - 1, s]) topf - topf + : Missing value, where TRUE/FAlse is required Has this something to do with the changing to daten-as.data.frame(daten) in line 3 (instead of as.matrix before)? -- View this message in context: http://r.789695.n4.nabble.com/help-please-matrix-operations-inside-3-nested-loops-tp4639592p4639730.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, please! matrix operations inside 3 nested loops
SORRY it should be: Fridolin wrote for (s in 3:6) { #walks though the matrix colum by colum, starting at colum 3 for (z1 in 1:5) { #for each current colum, take one row (z1)... for (z2 in 1:5) { #...and compare it to another row (z2) of the current colum error is gone now SORRY!!! -- View this message in context: http://r.789695.n4.nabble.com/help-please-matrix-operations-inside-3-nested-loops-tp4639592p4639735.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, please! matrix operations inside 3 nested loops
all problems solved. thank you for your help! for the sake of completeness, here my solution: #1) read in data: daten-read.table('K:/Analysen/STRUCTURE/test.txt', header=TRUE, sep=\t) daten-as.data.frame(daten) #2) create empty matrix: indxind-matrix(0,nrow=617, ncol=617) #indxind[1:20,1:19] #3) compare cells to each other, score: #for the whole dataset: s in 3:34, z1 in 1:617, z2 in 1:617 z1-1 #running variable for rows in daten z2-1 #running variable for rows in daten l1-1 #running variable for rows in indxind l2-1 #running variable for rows in indxind for (s in 3:6) { #walks though the matrix colum by colum, starting at colum 3 while (z111) { #for each current colum, take one row (z1)... while (z211) { #...and compare it to another row (z2) of the current colum if (z1!=z2) { l1 topf-indxind[l1,l2] if (daten[z1,s]==daten[z2,s]) topf-topf+1 #actually, 2 rows make up 1 individual, if (daten[z1,s]==daten[z2+1,s]) topf-topf+1 #therefore i compare 2 rows if (daten[z1+1,s]==daten[z2,s]) topf-topf+1 #with another 2 rows if (daten[z1+1,s]==daten[z2+1,s]) topf-topf+1 indxind[l1,l2]-topf } z2-z2+2 l2-l2+1 } z2-1 l2-1 z1-z1+2 l1-l1+1 } z1-1 l1-1 } #4) check: indxind[1:5,1:5] -- View this message in context: http://r.789695.n4.nabble.com/help-please-matrix-operations-inside-3-nested-loops-tp4639592p4639744.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, please! matrix operations inside 3 nested loops
Hi thank you for your help. my input data looks like this (tab separated): Ind.nr. Pop.nr. scm266 rms1280 scm247 rms1107 1 101 305 318 222 135 1 101 305 318 231 135 2 101 305 313 999 96 2 101 305 321 999 130 3 101 305 324 231 135 3 101 305 324 231 135 4 101 305 313 230 126 4 101 305 313 230 135 6 101 305 313 231 135 6 101 305 321 231 135 Better to use dput(your.data) for sharing data. Anyway I am still confused but you probably are able to clarify things further. it is a dataset with genetic marker alleles for single individuals. the first row is the header, all following rows are individuals. 2 rows count for 1 individual. first colum is the individual's number, second colum is the number for the population the individual comes from, and all following colums are different genetic markers. what i want to do with this data in R, is to compare one individual with In those 2 rows for one individual sometimes the genetic marker differs test[1:2, scm247] [1] 222 231 What do you want to do with them? each of the other individuals, allele-wise. there are five possibilities: the two compared individuals share 4,3,2,1,0 alleles of the currently examined marker (=colum). for each shared allele this pair of individuals shall get 1 scoring point. for each pair of individuals, all scoring points shall be summarized over all markers. Based on your example, dput(test) structure(list(Ind.nr. = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 6L, 6L), Pop.nr. = c(101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L, 101L), scm266 = c(305L, 305L, 305L, 305L, 305L, 305L, 305L, 305L, 305L, 305L), rms1280 = c(318L, 318L, 313L, 321L, 324L, 324L, 313L, 313L, 313L, 321L), scm247 = c(222L, 231L, 999L, 999L, 231L, 231L, 230L, 230L, 231L, 231L), rms1107 = c(135L, 135L, 96L, 130L, 135L, 135L, 126L, 135L, 135L, 135L)), .Names = c(Ind.nr., Pop.nr., scm266, rms1280, scm247, rms1107), class = data.frame, row.names = c(NA, -10L)) what is your desired result? Regards Petr my code again, modified according to your suggestions: #1) read in data: daten-read.table('K:/Analysen/STRUCTURE/test.txt', header=TRUE, sep=\t) daten-as.data.frame(daten) #2) create empty matrix: indxind-matrix(0,nrow=617, ncol=617) indxind[1:20,1:19] #3) compare cells to each other, score: #for the whole dataset: s in 3:34, z1 in 1:617, z2 in 1:617 for (s in 3:6) { #walks though the matrix colum by colum, starting at colum 3 for (z1 in 1:6) { #for each current colum, take one row (z1)... for (z2 in 1:6) { #...and compare it to another row (z2) of the current colum if (z1!=z2) {topf-indxind[z1,z2] if (daten[2*z1-1,s]==daten[2*z2-1,s]) topf-topf+1 #actually, 2 rows make up 1 individual, if (daten[2*z1-1,s]==daten[2*z2,s]) topf-topf+1 #therefore i compare 2 rows if (daten[2*z1,s]==daten[2*z2-1,s]) topf-topf+1 #with another 2 rows if (daten[2*z1,s]==daten[2*z2,s]) topf-topf+1 indxind[z1,z2]-topf indxind[z2,z1]-topf } #print(c(s,z1,z2,indxind[1,2])) ##counts s, z1 and z2 properly, but gives always 8 for indxind[1,2] } #indxind[1:5,1:5] #empty matrix } #indxind[1:5,1:5] #empty matrix } #4) check: indxind[1:5,1:5] @ Michael Weylandt: i've done my best with regard to the big picture of my algorithm and the small reproducible example. i hope both is sufficient. @ Petr Pikal-3: in this case, there are only numerical values, but it's a useful hint for my other codes. @ Petr Pikal-3 and Berend Hasselman: initializing indxind with 0's instead of NAs helps, it fills something in indxind now. but it does the calculation only for the first marker (colum 3), afterwards i get an error: Fehler in if (daten[2 * z1 - 1, s] == daten[2 * z2 - 1, s]) topf - topf + : Fehlender Wert, wo TRUE/FALSE nötig ist Error in if (daten[2 * z1 - 1, s] == daten[2 * z2 - 1, s]) topf - topf + : Missing value, where TRUE/FAlse is required Has this something to do with the changing to daten-as.data.frame(daten) in line 3 (instead of as.matrix before)? -- View this message in context: http://r.789695.n4.nabble.com/help-please- matrix-operations-inside-3-nested-loops-tp4639592p4639730.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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
Re: [R] help, please! matrix operations inside 3 nested loops
Hi all problems solved. thank you for your help! for the sake of completeness, here my solution: #1) read in data: daten-read.table('K:/Analysen/STRUCTURE/test.txt', header=TRUE, sep=\t) daten-as.data.frame(daten) not needed, daten is already data frame #2) create empty matrix: indxind-matrix(0,nrow=617, ncol=617) #indxind[1:20,1:19] #3) compare cells to each other, score: #for the whole dataset: s in 3:34, z1 in 1:617, z2 in 1:617 z1-1 #running variable for rows in daten z2-1 #running variable for rows in daten l1-1 #running variable for rows in indxind l2-1 #running variable for rows in indxind for (s in 3:6) { #walks though the matrix colum by colum, starting at colum 3 while (z111) { #for each current colum, take one row (z1)... while (z211) { #...and compare it to another row (z2) of the current colum if (z1!=z2) { l1 topf-indxind[l1,l2] if (daten[z1,s]==daten[z2,s]) topf-topf+1 #actually, 2 rows make up 1 individual, if (daten[z1,s]==daten[z2+1,s]) topf-topf+1 #therefore i compare 2 rows if (daten[z1+1,s]==daten[z2,s]) topf-topf+1 #with another 2 rows if (daten[z1+1,s]==daten[z2+1,s]) topf-topf+1 indxind[l1,l2]-topf } z2-z2+2 l2-l2+1 } z2-1 l2-1 z1-z1+2 l1-l1+1 } z1-1 l1-1 } #4) check: indxind[1:5,1:5] I believe that above cycles can be simplified, maybe by changing your daten to three dimensional array or some clever **ply construction but if your loops works it is not probably worth en effort. Regards Petr -- View this message in context: http://r.789695.n4.nabble.com/help-please- matrix-operations-inside-3-nested-loops-tp4639592p4639744.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@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, please! matrix operations inside 3 nested loops
hello, this is my script: #1) read in data: daten-read.table('K:/Analysen/STRUCTURE/input_STRUCTURE_tab_excl_5_282_559.txt', header=TRUE, sep=\t) daten-as.matrix(daten) #2) create empty matrix: indxind-matrix(nrow=617, ncol=617) indxind[1:20,1:19] #3) compare cells to each other, score: for (s in 3:34) { #walks though the matrix colum by colum, starting at colum 3 for (z1 in 1:617) { #for each current colum, take one row (z1)... for (z2 in 1:617) { #...and compare it to another row (z2) of the current colum if (z1!=z2) {topf-indxind[z1,z2] if (daten[2*z1-1,s]==daten[2*z2-1,s]) topf-topf+1 #actually, 2 rows make up 1 individual, if (daten[2*z1-1,s]==daten[2*z2,s]) topf-topf+1 #therefore i compare 2 rows if (daten[2*z1,s]==daten[2*z2-1,s]) topf-topf+1 #with another 2 rows if (daten[2*z1,s]==daten[2*z2,s]) topf-topf+1 indxind[z1,z2]-topf indxind[z2,z1]-topf } #print(c(s,z1,z2,indxind[1,2])) ##counts s, z1 and z2 properly, but gives NA for indxind[1,2] } #indxind[1:5,1:5] #empty matrix } #indxind[1:5,1:5] #empty matrix } #4) check: indxind[1:5,1:5] this results no errors, but my matrix indxind remains empty (only NAs). though all columns and rows are counted properly. R needs quite a while to get through all this (there are probably smarter and faster ways to calculate this but i am not too deep into R and bioinformatics, and i need to calculate this only once). could the 3 for-loops already be too computationally intense for adding matrix operations? any help would be much appreciated! thx, frido -- View this message in context: http://r.789695.n4.nabble.com/help-please-matrix-operations-inside-3-nested-loops-tp4639592.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, please! matrix operations inside 3 nested loops
On Wed, Aug 8, 2012 at 9:06 AM, Fridolin smells_like_r...@gmx.net wrote: hello, this is my script: #1) read in data: daten-read.table('K:/Analysen/STRUCTURE/input_STRUCTURE_tab_excl_5_282_559.txt', header=TRUE, sep=\t) daten-as.matrix(daten) #2) create empty matrix: indxind-matrix(nrow=617, ncol=617) indxind[1:20,1:19] #3) compare cells to each other, score: for (s in 3:34) { #walks though the matrix colum by colum, starting at colum 3 for (z1 in 1:617) { #for each current colum, take one row (z1)... for (z2 in 1:617) { #...and compare it to another row (z2) of the current colum if (z1!=z2) {topf-indxind[z1,z2] if (daten[2*z1-1,s]==daten[2*z2-1,s]) topf-topf+1 #actually, 2 rows make up 1 individual, if (daten[2*z1-1,s]==daten[2*z2,s]) topf-topf+1 #therefore i compare 2 rows if (daten[2*z1,s]==daten[2*z2-1,s]) topf-topf+1 #with another 2 rows if (daten[2*z1,s]==daten[2*z2,s]) topf-topf+1 indxind[z1,z2]-topf indxind[z2,z1]-topf } #print(c(s,z1,z2,indxind[1,2])) ##counts s, z1 and z2 properly, but gives NA for indxind[1,2] } #indxind[1:5,1:5] #empty matrix } #indxind[1:5,1:5] #empty matrix } #4) check: indxind[1:5,1:5] this results no errors, but my matrix indxind remains empty (only NAs). though all columns and rows are counted properly. R needs quite a while to get through all this (there are probably smarter and faster ways to calculate this but i am not too deep into R and bioinformatics, and i need to calculate this only once). could the 3 for-loops already be too computationally intense for adding matrix operations? any help would be much appreciated! thx, frido Hi Frido, I'm afraid I get a little lost in your code, but I'd be willing to bet we can cut the loops out entirely and speed things up. Can you give us a big picture description of the algorithm you're implementing as well as (if it's not too hard) a small reproducible example [1]? Note also that most of us don't use Nabble so you'll need to explicitly quote any relevant context. Thanks, Michael [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example __ 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, please! matrix operations inside 3 nested loops
Fridolin wrote hello, this is my script: #1) read in data: daten-read.table('K:/Analysen/STRUCTURE/input_STRUCTURE_tab_excl_5_282_559.txt', header=TRUE, sep=\t) daten-as.matrix(daten) #2) create empty matrix: indxind-matrix(nrow=617, ncol=617) indxind[1:20,1:19] You should at least initialize indxind to 0 with indxind-matrix(0,nrow=617, ncol=617) because the default for matrix is to use NA for data. Berend -- View this message in context: http://r.789695.n4.nabble.com/help-please-matrix-operations-inside-3-nested-loops-tp4639592p4639621.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 in matrix
Hi I have a matrix a = 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 b = 5 6 7 8 9 10 I want output as c = 5 2 3 8 2 3 1 6 3 1 9 3 1 2 7 1 2 10 Please any on help to avoid reduncy of my code - Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/Help-in-matrix-tp4449622p4449622.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 in matrix
On Mar 6, 2012, at 13:02 , arunkumar wrote: Hi I have a matrix a = 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 b = 5 6 7 8 9 10 I want output as c = 5 2 3 8 2 3 1 6 3 1 9 3 1 2 7 1 2 10 Please any on help to avoid reduncy of my code Here's one way: a - matrix(rep(1:3,each=6),6) a [,1] [,2] [,3] [1,]123 [2,]123 [3,]123 [4,]123 [5,]123 [6,]123 b - matrix(5:10,2,byrow=T) ix - cbind(1:6,rep(1:3,each=2)) a[ix] - b a [,1] [,2] [,3] [1,]523 [2,]823 [3,]163 [4,]193 [5,]127 [6,]12 10 -- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.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 in matrix
Thanks so much it worked very well - Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/Help-in-matrix-tp4449622p4450560.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 Matrix code optimization
The chol and solve methods for dpoMatrix (Matrix package) are much faster than the default methods. But, the time required to coerce a regular matrix to dpoMatrix swamps the advantage. Hence, I have the following problem, where use of dpoMatrix is worse than a regular matrix. library(Matrix) x - diag(10) system.time( for(r in seq(0.1, 0.9, length.out=1000)) { m - r^abs(row(x)-col(x)); chol(m); solve(m); }) system.time( for(r in seq(0.1, 0.9, length.out=1000)) { M - as(r^abs(row(x)-col(x)), 'dpoMatrix') chol(M); solve(M); }) Any ideas? __ 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 matrix column cleaning
Dear R Group I have a matrix object in R, where i want to retain only those columns which have each row from a different group.. see the example below.. team1-c(a1,a2,b1,b2,b3,c1,c2) teams-combn(team1, 3) I want a function which will delete all columns in the teams matrix that have more than 1 row having same letter starts.. For instance in the example object teams, I want to retain column 8,11,12,13,14,18,19,21,22,23,24 . I am looking at doing this in larger matrix objects using a function preferrably that i can apply to the matrix object to check for the beginning letter of each cell from each row and delete columns that have more than one row having the same start letter. Thanks for your help. Regards Vijayan Padmanabhan What is expressed without proof can be denied without proof - Euclide. Please visit us at www.itcportal.com ** This Communication is for the exclusive use of the intended recipient (s) and shall not attach any liability on the originator or ITC Ltd./its Subsidiaries/its Group Companies. If you are the addressee, the contents of this email are intended for your use only and it shall not be forwarded to any third party, without first obtaining written authorisation from the originator or ITC Ltd./its Subsidiaries/its Group Companies. It may contain information which is confidential and legally privileged and the same shall not be used or dealt with by any third party in any manner whatsoever without the specific consent of ITC Ltd./its Subsidiaries/its Group Companies. [[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.
Re: [R] help with matrix column cleaning
On Feb 14, 2012, at 12:46 AM, Vijayan Padmanabhan wrote: Dear R Group I have a matrix object in R, where i want to retain only those columns which have each row from a different group.. see the example below.. team1-c(a1,a2,b1,b2,b3,c1,c2) teams-combn(team1, 3) I want a function which will delete all columns in the teams matrix that have more than 1 row having same letter starts.. For instance in the example object teams, I want to retain column 8,11,12,13,14,18,19,21,22,23,24 . This will give you a logical vector which you can use with [ in the column position to select those columns: apply(teams, 2, function(x) length( unique(substr(x, 1,1))) ==3 ) [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE [13] TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE I am looking at doing this in larger matrix objects using a function preferrably that i can apply to the matrix object to check for the beginning letter of each cell from each row and delete columns that have more than one row having the same start letter. Thanks for your help. Regards Vijayan Padmanabhan -- David Winsemius, MD West Hartford, CT __ 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 matrix
Hi I have 3 vectors, say g1, g2 and f, where g1 and g2 contain the row and column indices of a matrix, with corresponding elements in f. How can I create a matrix containing the values in f ? For example: g1 g2 f 3 4 10 2 1 30 4 4 50 How can then get a matrix that looks as follows: 0 0 0 0 30 0 0 0 0 0 0 10 0 0 0 50 I am still learning R. I think reshape will help, but it is not clear how that works here. Any help is much appreciated. Thank you. Jacob Jacob L van Wyk Department of Statistics University of Johannesburg, APK Box 524 Auckland Park 2006 South Africa Office Tel: +27 11 559 3080 Fax: +27 11 559 2832 [[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.
[R] help with matrix
Hi, dear all: I am a beginner. I appreciate any help or hint from you. I am trying to do calculation with matrices. I have 3 matrices. One is matrixA, 2nd is matrixB, and last is matrixC. Here is matrixA: 1.8511.40.0831.001 0.8771.30.1161.33 1.9021.21.1020.302 0.8640.1261.110.252 1.8230.2161.0020.307 Next is matrixB: 0.8761.770.1930.328 0.8911.0090.2381.004 0.8641.1150.2760.22 0.8871.3060.1660.239 0.8521.0011.0080.251 Last is matrixC: 0.5 1.0 0.5 1.0 1.0 1.0 1.0 0.5 1.0 1.0 1.0 0.5 1.0 1.0 1.0 0.5 0.5 0.5 1.0 0.5 What I want is to match both matrixA and matrixB with matrixC. Ignore the 1st column in matrixA and matrixB. When the element im matrixC is 1.0, multilply the corresponding element in matrixA by the corresponding element in matrixB. Otherwise, ignore it. I wrote the following code with some error in the logic: A-read.table('matrixA.txt', header=F) B-read.table('matrixB.txt', header=F) C-read.table('matrixC.txt', header=F) a1-as.matrix(A[,1]) arest-as.matrix(A[,2:5]) b1-as.matrix(B[,1]) brest-as.matrix(B[,2:5]) C-as.matrix(C) if(C= =1){ ra-(arest*brest) }else { throw-(arest*brest) } ra I got a warning message: Warning message:the condition has length 1 and only the first element will be used in: if. When I changed the if part to: ifelse (C= =1,ra-(arest*brest),throw-(arest*brest)) The result is the multiplication of each element in matrixA and matrixB. Anyone has an idea about what is wrong. Thanks Riddle Be a better friend, newshound, and [[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.