[R] matrix manipulation
This is as much a mathematics as an R question, in the this should be easy but I don't see it category. Assume I have a full rank p by p matrix V (aside: V = (X'X)^{-1} for a particular setup), a p by k matrix B, and I want to complete an orthagonal basis for the space with distance function V. That is, find A such that t(B) %*% V %*% A =0, where A has p rows and p-k columns. With V=identity this is easy. I can do it in 1-2 lines using qr(), lm(), or several other tools. A part of me is quite certain that the general problem isn't more than 3 lines of R, but after a day of beating my head on the issue I still don't see it. Math wise it looks like a simple homework problem in a mid level class, but I'm not currently sure that I'd pass said class. If someone could show the way I would be grateful. Either that or assurance that the problem actually IS hard and I'm not as dense as I think. Terry T. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 Terry, maybe I'm missing something, but why not define a matrix BB = V'B; then t(B) %*% V = t(BB), then your problem reduces to finding A such that t(BB) %*% A = 0? Peter On Thu, Jul 16, 2015 at 10:28 AM, Therneau, Terry M., Ph.D. thern...@mayo.edu wrote: This is as much a mathematics as an R question, in the this should be easy but I don't see it category. Assume I have a full rank p by p matrix V (aside: V = (X'X)^{-1} for a particular setup), a p by k matrix B, and I want to complete an orthagonal basis for the space with distance function V. That is, find A such that t(B) %*% V %*% A =0, where A has p rows and p-k columns. With V=identity this is easy. I can do it in 1-2 lines using qr(), lm(), or several other tools. A part of me is quite certain that the general problem isn't more than 3 lines of R, but after a day of beating my head on the issue I still don't see it. Math wise it looks like a simple homework problem in a mid level class, but I'm not currently sure that I'd pass said class. If someone could show the way I would be grateful. Either that or assurance that the problem actually IS hard and I'm not as dense as I think. Terry T. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 -solved
Yes it is obvious --- once someone else pointed it out. Thanks for the hint. Terry T. On 07/16/2015 12:52 PM, Peter Langfelder wrote: Hi Terry, maybe I'm missing something, but why not define a matrix BB = V'B; then t(B) %*% V = t(BB), then your problem reduces to finding A such that t(BB) %*% A = 0? Peter On Thu, Jul 16, 2015 at 10:28 AM, Therneau, Terry M., Ph.D. thern...@mayo.edu wrote: This is as much a mathematics as an R question, in the this should be easy but I don't see it category. Assume I have a full rank p by p matrix V (aside: V = (X'X)^{-1} for a particular setup), a p by k matrix B, and I want to complete an orthagonal basis for the space with distance function V. That is, find A such that t(B) %*% V %*% A =0, where A has p rows and p-k columns. With V=identity this is easy. I can do it in 1-2 lines using qr(), lm(), or several other tools. A part of me is quite certain that the general problem isn't more than 3 lines of R, but after a day of beating my head on the issue I still don't see it. Math wise it looks like a simple homework problem in a mid level class, but I'm not currently sure that I'd pass said class. If someone could show the way I would be grateful. Either that or assurance that the problem actually IS hard and I'm not as dense as I think. Terry T. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 R
On Jul 4, 2015, at 3:09 AM, Alex Kim dumboisveryd...@gmail.com wrote: Hi guys, Suppose I have an extremely large data frame with 2 columns and .5 mil rows. For example, the last 6 rows may look like this: . .. ... 89 100 93 120 95 125 101NA 115NA 123NA 124NA I would like to manipulate this data frame to output a data frame that looks like:, 10089, 93, 95 120101, 115 125123, 124 What would be the absolute quickest way to do this, given that there are many rows? Currently I have this: # m is the large two column data frame end - na.omit(m[,'V2']); out - data.frame(End=end, Start=unname(sapply(split(m[,'V1'],findInterval(m[,'V1'],end))[as.character(0:c(length(end)-1))],paste,collapse='.'))) This might be a little faster. It skips some of the steps in your version: dput(m) structure(list(V1 = c(89, 93, 95, 101, 115, 123, 124), V2 = c(100, 120, 125, NA, NA, NA, NA)), .Names = c(V1, V2), row.names = c(NA, -7L), class = data.frame) end - na.omit(m[,'V2’]) # this will only work if that vector is sorted data.frame(End = end, Start = sapply( split( m$V1, findInterval(m$V1, c(-Inf, end))), paste,collapse=, ) ) EndStart 1 100 89,93,95 2 120 101,115 3 125 123,124 However this is taking a little bit too long. Thank you for your help! [[alternative HTML version deleted]] This is a plain-text mailing list and posting triplicate questions is poor form. Do read the posting guide. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. — David Winsemius, MD Alameda, CA, USA __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Matrix Manipulation R
Hi guys, Suppose I have an extremely large data frame with 2 columns and .5 mil rows. For example, the last 6 rows may look like this: . .. ... 89 100 93 120 95 125 101NA 115NA 123NA 124NA I would like to manipulate this data frame to output a data frame that looks like:, 10089, 93, 95 120101, 115 125123, 124 What would be the absolute quickest way to do this, given that there are many rows? Currently I have this: # m is the large two column data frame end - na.omit(m[,'V2']); out - data.frame(End=end, Start=unname(sapply(split(m[,'V1'],findInterval(m[,'V1'],end))[as.character(0:c(length(end)-1))],paste,collapse='.'))) However this is taking a little bit too long. Thank you for your help! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Matrix Manipulation R
Hi guys, Suppose I have an extremely large data frame with 2 columns and .5 mil rows. For example, the last 6 rows may look like this: . .. ... 89 100 93 120 95 125 101NA 115NA 123NA 124NA I would like to manipulate this data frame to output a data frame that looks like:, 10089, 93, 95 120101, 115 125123, 124 What would be the absolute quickest way to do this, given that there are many rows? Currently I have this: # m is the large two column data frame end - na.omit(m[,'V2']); out - data.frame(End=end, Start=unname(sapply(split(m[,'V1'],findInterval(m[,'V1'],end))[as.character(0:c(length(end)-1))],paste,collapse='.'))) However this is taking a little bit too long. Thank you for your help! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Matrix Manipulation
Hi guys, Suppose I have an extremely large data frame with 2 columns and .5 mil rows. For example, the last 6 rows may look like this: . .. ... 89 100 93 120 95 125 101NA 115NA 123NA 124NA I would like to manipulate this data frame to output a data frame that looks like:, 10089, 93, 95 120101, 115 125123, 124 What would be the absolute quickest way to do this, given that there are many rows? Currently I have this: # m is the large two column data frame end - na.omit(m[,'V2']); out - data.frame(End=end, Start=unname(sapply(split(m[,'V1'],findInterval(m[,'V1'],end))[as.character(0:c(length(end)-1))],paste,collapse='.'))) However this is taking a little bit too long. Thank you for your help! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
Many thanks, Stéphane Le 30 mars 2015 à 10:42, peter dalgaard pda...@gmail.com a écrit : On 30 Mar 2015, at 09:59 , Stéphane Adamowicz stephane.adamow...@avignon.inra.fr wrote: However, in order to help me understand, would you be so kind as to give me a matrix or data.frame example where « complete.cases(X)== T » or « complete.cases(X)== TRUE » would give some unwanted result ? The standard problem with T for TRUE is if T has been used for some other purpose, like a time variable. E.g., T - 0 ; complete.cases(X)==T. complete.cases()==TRUE is just silly, like (x==0)==TRUE or ((x==0)==TRUE)==TRUE). (However, notice that x==TRUE is different from as.logical(x) if x is numeric, so ifelse(x,y,z) may differ from ifelse(x==TRUE,y,z).) -- 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 -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
On 30-03-2015, at 09:59, Stéphane Adamowicz stephane.adamow...@avignon.inra.fr wrote: Le 27 mars 2015 à 18:01, David Winsemius dwinsem...@comcast.net a écrit : On Mar 27, 2015, at 3:41 AM, Stéphane Adamowicz wrote: Well, it seems to work with me. No one is doubting that it worked for you in this instance. What Peter D. was criticizing was the construction : complete.cases(t(Y))==T ... and it was on two bases that it is wrong. The first is that `T` is not guaranteed to be TRUE. The second is that the test ==T (or similarly ==TRUE) is completely unnecessary because `complete.cases` returns a logical vector and so that expression is a waste of time. Indeed, You are right, the following code was enough : « Z - Y[, complete.cases(t(Y) ] » However, in order to help me understand, would you be so kind as to give me a matrix or data.frame example where « complete.cases(X)== T » or « complete.cases(X)== TRUE » would give some unwanted result ? T can be redefined. Try this in your example with airquality: T - hello Z - Y[,complete.cases(t(Y))==T] Z TRUE is a reserved word and cannot be changed. But why use ==TRUE if not necessary? All of this mentioned already by David Winsemius in a previous reply. Berend __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
On 30 Mar 2015, at 09:59 , Stéphane Adamowicz stephane.adamow...@avignon.inra.fr wrote: However, in order to help me understand, would you be so kind as to give me a matrix or data.frame example where « complete.cases(X)== T » or « complete.cases(X)== TRUE » would give some unwanted result ? The standard problem with T for TRUE is if T has been used for some other purpose, like a time variable. E.g., T - 0 ; complete.cases(X)==T. complete.cases()==TRUE is just silly, like (x==0)==TRUE or ((x==0)==TRUE)==TRUE). (However, notice that x==TRUE is different from as.logical(x) if x is numeric, so ifelse(x,y,z) may differ from ifelse(x==TRUE,y,z).) -- 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 -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
Le 27 mars 2015 � 18:01, David Winsemius dwinsem...@comcast.net a �crit : On Mar 27, 2015, at 3:41 AM, St�phane Adamowicz wrote: Well, it seems to work with me. No one is doubting that it worked for you in this instance. What Peter D. was criticizing was the construction : complete.cases(t(Y))==T ... and it was on two bases that it is wrong. The first is that `T` is not guaranteed to be TRUE. The second is that the test ==T (or similarly ==TRUE) is completely unnecessary because `complete.cases` returns a logical vector and so that expression is a waste of time. Indeed, You are right, the following code was enough : � Z - Y[, complete.cases(t(Y) ] � However, in order to help me understand, would you be so kind as to give me a matrix or data.frame example where � complete.cases(X)== T � or � complete.cases(X)== TRUE � would give some unwanted result ? St�phane [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
On 27 Mar 2015, at 09:58 , Stéphane Adamowicz stephane.adamow...@avignon.inra.fr wrote: data_no_NA - data[, complete.cases(t(data))==T] Ouch! logical == TRUE is bad, logical == T is worse: data[, complete.cases(t(data))] -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
Very, very, very bad solution. as.matrix can change silently your data to unwanted format, complete.cases()==T is silly as Peter already pointed out. I use head(airquality[ ,colSums(is.na(airquality))==0]) Wind Temp Month Day 1 7.4 67 5 1 2 8.0 72 5 2 3 12.6 74 5 3 4 11.5 62 5 4 5 14.3 56 5 5 6 14.9 66 5 6 if I want to get rid of columns with NA. Cheers Petr From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Stéphane Adamowicz Sent: Friday, March 27, 2015 11:42 AM To: peter dalgaard Cc: r-help@r-project.org Subject: Re: [R] matrix manipulation question Well, it seems to work with me. Y - as.matrix(airquality) head(Y, n=8) Ozone Solar.R Wind Temp Month Day [1,]41 190 7.4 67 5 1 [2,]36 118 8.0 72 5 2 [3,]12 149 12.6 74 5 3 [4,]18 313 11.5 62 5 4 [5,]NA NA 14.3 56 5 5 [6,]28 NA 14.9 66 5 6 [7,]23 299 8.6 65 5 7 [8,]19 99 13.8 59 5 8 Z - Y[,complete.cases(t(Y))==T] head(Z, n=8) Wind Temp Month Day [1,] 7.4 67 5 1 [2,] 8.0 72 5 2 [3,] 12.6 74 5 3 [4,] 11.5 62 5 4 [5,] 14.3 56 5 5 [6,] 14.9 66 5 6 [7,] 8.6 65 5 7 [8,] 13.8 59 5 8 The columns that contained NA were deleted. Le 27 mars 2015 � 10:38, peter dalgaard pda...@gmail.commailto:pda...@gmail.com a �crit : On 27 Mar 2015, at 09:58 , St�phane Adamowicz stephane.adamow...@avignon.inra.frmailto:stephane.adamow...@avignon.inra.fr wrote: data_no_NA - data[, complete.cases(t(data))==T] Ouch! logical == TRUE is bad, logical == T is worse: data[, complete.cases(t(data))] -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dkmailto:pd@cbs.dk Priv: pda...@gmail.commailto:pda...@gmail.com _ St�phane Adamowicz Inra, centre de recherche Paca, unit� PSH 228, route de l'a�rodrome CS 40509 domaine St Paul, site Agroparc 84914 Avignon, cedex 9 France stephane.adamow...@avignon.inra.frmailto:stephane.adamow...@avignon.inra.fr tel. +33 (0)4 32 72 24 35 fax. +33 (0)4 32 72 24 32 do not dial 0 when out of France web PSH : https://www6.paca.inra.fr/psh web Inra : http://www.inra.fr/ _ [[alternative HTML version deleted]] __ R-help@r-project.orgmailto:R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům. Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému. Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu. V případě, že je tento e-mail součástí obchodního jednání: - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu. - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou. - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech. - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without
Re: [R] matrix manipulation question
Well, it seems to work with me. Y - as.matrix(airquality) head(Y, n=8) Ozone Solar.R Wind Temp Month Day [1,]41 190 7.4 67 5 1 [2,]36 118 8.0 72 5 2 [3,]12 149 12.6 74 5 3 [4,]18 313 11.5 62 5 4 [5,]NA NA 14.3 56 5 5 [6,]28 NA 14.9 66 5 6 [7,]23 299 8.6 65 5 7 [8,]19 99 13.8 59 5 8 Z - Y[,complete.cases(t(Y))==T] head(Z, n=8) Wind Temp Month Day [1,] 7.4 67 5 1 [2,] 8.0 72 5 2 [3,] 12.6 74 5 3 [4,] 11.5 62 5 4 [5,] 14.3 56 5 5 [6,] 14.9 66 5 6 [7,] 8.6 65 5 7 [8,] 13.8 59 5 8 The columns that contained NA were deleted. Le 27 mars 2015 � 10:38, peter dalgaard pda...@gmail.com a �crit : On 27 Mar 2015, at 09:58 , St�phane Adamowicz stephane.adamow...@avignon.inra.fr wrote: data_no_NA - data[, complete.cases(t(data))==T] Ouch! logical == TRUE is bad, logical == T is worse: data[, complete.cases(t(data))] -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dk Priv: pda...@gmail.com _ St�phane Adamowicz Inra, centre de recherche Paca, unit� PSH 228, route de l'a�rodrome CS 40509 domaine St Paul, site Agroparc 84914 Avignon, cedex 9 France stephane.adamow...@avignon.inra.fr tel. +33 (0)4 32 72 24 35 fax. +33 (0)4 32 72 24 32 do not dial 0 when out of France web PSH : https://www6.paca.inra.fr/psh web Inra : http://www.inra.fr/ _ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
Why not use complete.cases() ? data_no_NA - data[, complete.cases(t(data))==T] Le 27 mars 2015 à 06:13, Jatin Kala jatin.kala...@gmail.com a écrit : Hi, I've got a rather large matrix of about 800 rows and 60 columns. Each column is a time-series 800 long. Out of these 60 time series, some have missing values (NA). I want to strip out all columns that have one or more NA values, i.e., only want full time series. This should do the trick: data_no_NA - data[,!apply(is.na(data), 2, any)] I now use data_no_NA as input to a function, which returns output as a matrix of the same size as data_no_NA The trick is that i now need to put these columns back into a new 800 by 60 empty matrix, at their original locations. Any suggestions on how to do that? hopefully without having to use loops. I'm using R/3.0.3 Cheers, Jatin. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
Le 27 mars 2015 à 12:34, PIKAL Petr petr.pi...@precheza.cz a écrit : Very, very, very bad solution. as.matrix can change silently your data to unwanted format, complete.cases()==T is silly as Peter already pointed out. Perhaps, but it happens that in the original message, the question dealt with a matrix not a dataframe, and thus I needed a matrix example. Furthermore in my example no unwanted format occurred. You can check easily that the final matrix contains only « numeric » data as in the original data frame. Stéphane _ St�phane Adamowicz Inra, centre de recherche Paca, unit� PSH 228, route de l'a�rodrome CS 40509 domaine St Paul, site Agroparc 84914 Avignon, cedex 9 France stephane.adamow...@avignon.inra.fr tel. +33 (0)4 32 72 24 35 fax. +33 (0)4 32 72 24 32 do not dial 0 when out of France web PSH : https://www6.paca.inra.fr/psh web Inra : http://www.inra.fr/ _ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům. Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému. Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu. V případě, že je tento e-mail součástí obchodního jednání: - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu. - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou. - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech. - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
Hi -Original Message- From: Stéphane Adamowicz [mailto:stephane.adamow...@avignon.inra.fr] Sent: Friday, March 27, 2015 1:26 PM To: PIKAL Petr Cc: peter dalgaard; r-help@r-project.org Subject: Re: [R] matrix manipulation question Le 27 mars 2015 à 12:34, PIKAL Petr petr.pi...@precheza.cz a écrit : Very, very, very bad solution. as.matrix can change silently your data to unwanted format, complete.cases()==T is silly as Peter already pointed out. Perhaps, but it happens that in the original message, the question I do not have original message. dealt with a matrix not a dataframe, and thus I needed a matrix But you made matrix from data frame. If one column was not numeric all resulting matrix would chnge to nonnumeric format. example. Furthermore in my example no unwanted format occurred. You can Yes because data.frame was (luckily) numeric. check easily that the final matrix contains only « numeric » data as in the original data frame. You want matrix? Here it is. head(as.matrix(airquality)[ ,colSums(is.na(airquality))==0]) Wind Temp Month Day [1,] 7.4 67 5 1 [2,] 8.0 72 5 2 [3,] 12.6 74 5 3 [4,] 11.5 62 5 4 [5,] 14.3 56 5 5 [6,] 14.9 66 5 6 Works same with matrix as with data frame without need to transform it. Cheers Petr Stéphane _ St phane Adamowicz Inra, centre de recherche Paca, unit PSH 228, route de l'a rodrome CS 40509 domaine St Paul, site Agroparc 84914 Avignon, cedex 9 France stephane.adamow...@avignon.inra.fr tel. +33 (0)4 32 72 24 35 fax. +33 (0)4 32 72 24 32 do not dial 0 when out of France web PSH : https://www6.paca.inra.fr/psh web Inra : http://www.inra.fr/ _ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům. Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému. Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat. Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu. V případě, že je tento e-mail součástí obchodního jednání: - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu. - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou. - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech. - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted
Re: [R] matrix manipulation question
example. Furthermore in my example no unwanted format occurred. You can Yes because data.frame was (luckily) numeric. Luck has nothing to do with this. I Chose this example on purpose … Stéphane __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
On Mar 27, 2015, at 3:41 AM, Stéphane Adamowicz wrote: Well, it seems to work with me. No one is doubting that it worked for you in this instance. What Peter D. was criticizing was the construction : complete.cases(t(Y))==T ... and it was on two bases that it is wrong. The first is that `T` is not guaranteed to be TRUE. The second is that the test ==T (or similarly ==TRUE) is completely unnecessary because `complete.cases` returns a logical vector and so that expression is a waste of time. (The issue of matrix versus dataframe was raised by someone else.) -- David. Y - as.matrix(airquality) head(Y, n=8) Ozone Solar.R Wind Temp Month Day [1,]41 190 7.4 67 5 1 [2,]36 118 8.0 72 5 2 [3,]12 149 12.6 74 5 3 [4,]18 313 11.5 62 5 4 [5,]NA NA 14.3 56 5 5 [6,]28 NA 14.9 66 5 6 [7,]23 299 8.6 65 5 7 [8,]19 99 13.8 59 5 8 Z - Y[,complete.cases(t(Y))==T] head(Z, n=8) Wind Temp Month Day [1,] 7.4 67 5 1 [2,] 8.0 72 5 2 [3,] 12.6 74 5 3 [4,] 11.5 62 5 4 [5,] 14.3 56 5 5 [6,] 14.9 66 5 6 [7,] 8.6 65 5 7 [8,] 13.8 59 5 8 The columns that contained NA were deleted. Le 27 mars 2015 à 10:38, peter dalgaard pda...@gmail.com a écrit : On 27 Mar 2015, at 09:58 , Stéphane Adamowicz stephane.adamow...@avignon.inra.fr wrote: data_no_NA - data[, complete.cases(t(data))==T] Ouch! logical == TRUE is bad, logical == T is worse: data[, complete.cases(t(data))] -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dk Priv: pda...@gmail.com _ Stéphane Adamowicz Inra, centre de recherche Paca, unité PSH 228, route de l'aérodrome CS 40509 domaine St Paul, site Agroparc 84914 Avignon, cedex 9 France stephane.adamow...@avignon.inra.fr tel. +33 (0)4 32 72 24 35 fax. +33 (0)4 32 72 24 32 do not dial 0 when out of France web PSH : https://www6.paca.inra.fr/psh web Inra : http://www.inra.fr/ _ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
On 2015-03-27 11:41, Stéphane Adamowicz wrote: Well, it seems to work with me. Y - as.matrix(airquality) head(Y, n=8) Ozone Solar.R Wind Temp Month Day [1,]41 190 7.4 67 5 1 [2,]36 118 8.0 72 5 2 [3,]12 149 12.6 74 5 3 [4,]18 313 11.5 62 5 4 [5,]NA NA 14.3 56 5 5 [6,]28 NA 14.9 66 5 6 [7,]23 299 8.6 65 5 7 [8,]19 99 13.8 59 5 8 Z - Y[,complete.cases(t(Y))==T] Peter's point, I guess, is that 1. complete.cases(t(Y)) is already a vector of logicals 2. T (and F) can be redefined, so what if T - FALSE? Henric Winell head(Z, n=8) Wind Temp Month Day [1,] 7.4 67 5 1 [2,] 8.0 72 5 2 [3,] 12.6 74 5 3 [4,] 11.5 62 5 4 [5,] 14.3 56 5 5 [6,] 14.9 66 5 6 [7,] 8.6 65 5 7 [8,] 13.8 59 5 8 The columns that contained NA were deleted. Le 27 mars 2015 � 10:38, peter dalgaard pda...@gmail.com a �crit : On 27 Mar 2015, at 09:58 , St�phane Adamowicz stephane.adamow...@avignon.inra.fr wrote: data_no_NA - data[, complete.cases(t(data))==T] Ouch! logical == TRUE is bad, logical == T is worse: data[, complete.cases(t(data))] -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dk Priv: pda...@gmail.com _ St�phane Adamowicz Inra, centre de recherche Paca, unit� PSH 228, route de l'a�rodrome CS 40509 domaine St Paul, site Agroparc 84914 Avignon, cedex 9 France stephane.adamow...@avignon.inra.fr tel. +33 (0)4 32 72 24 35 fax. +33 (0)4 32 72 24 32 do not dial 0 when out of France web PSH : https://www6.paca.inra.fr/psh web Inra : http://www.inra.fr/ _ [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
Thanks Richard, This works, rather obvious now that i think of it! =) On 27/03/2015 4:30 pm, Richard M. Heiberger wrote: just reverse what you did before. newdata - data newdata[] - NA newdata[,!apply(is.na(data), 2, any)] - myfunction(data_no_NA) On Fri, Mar 27, 2015 at 1:13 AM, Jatin Kala jatin.kala...@gmail.com wrote: Hi, I've got a rather large matrix of about 800 rows and 60 columns. Each column is a time-series 800 long. Out of these 60 time series, some have missing values (NA). I want to strip out all columns that have one or more NA values, i.e., only want full time series. This should do the trick: data_no_NA - data[,!apply(is.na(data), 2, any)] I now use data_no_NA as input to a function, which returns output as a matrix of the same size as data_no_NA The trick is that i now need to put these columns back into a new 800 by 60 empty matrix, at their original locations. Any suggestions on how to do that? hopefully without having to use loops. I'm using R/3.0.3 Cheers, Jatin. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] matrix manipulation question
Hi, I've got a rather large matrix of about 800 rows and 60 columns. Each column is a time-series 800 long. Out of these 60 time series, some have missing values (NA). I want to strip out all columns that have one or more NA values, i.e., only want full time series. This should do the trick: data_no_NA - data[,!apply(is.na(data), 2, any)] I now use data_no_NA as input to a function, which returns output as a matrix of the same size as data_no_NA The trick is that i now need to put these columns back into a new 800 by 60 empty matrix, at their original locations. Any suggestions on how to do that? hopefully without having to use loops. I'm using R/3.0.3 Cheers, Jatin. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 question
just reverse what you did before. newdata - data newdata[] - NA newdata[,!apply(is.na(data), 2, any)] - myfunction(data_no_NA) On Fri, Mar 27, 2015 at 1:13 AM, Jatin Kala jatin.kala...@gmail.com wrote: Hi, I've got a rather large matrix of about 800 rows and 60 columns. Each column is a time-series 800 long. Out of these 60 time series, some have missing values (NA). I want to strip out all columns that have one or more NA values, i.e., only want full time series. This should do the trick: data_no_NA - data[,!apply(is.na(data), 2, any)] I now use data_no_NA as input to a function, which returns output as a matrix of the same size as data_no_NA The trick is that i now need to put these columns back into a new 800 by 60 empty matrix, at their original locations. Any suggestions on how to do that? hopefully without having to use loops. I'm using R/3.0.3 Cheers, Jatin. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] matrix manipulation with its rows
Dear R users, I have a question about matrix manipulation with its rows. Plz see the simple example below sample - list(matrix(1:6, nr=2,nc=3), matrix(7:12, nr=2,nc=3), matrix(13:18,nr=2,nc=3)) sample [[1]] [,1] [,2] [,3] [1,]135 [2,]246 [[2]] [,1] [,2] [,3] [1,]79 11 [2,]8 10 12 [[3]] [,1] [,2] [,3] [1,] 13 15 17 [2,] 14 16 18 With this list, I'd like to create this below [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]135 000 [2,]000 246 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]79 11 000 [2,] 0008 10 12 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 13 15 17 000 [2,] 000 14 16 18 Any suggestion will be greatly appreciated. Regards, Kathryn Lord [[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] matrix manipulation with its rows
Hi with your specific example you can use remat-function(mat) { mt-t(mat) mt-c(mt[1:3], rep(0,6), mt[4:6]) matrix(mt, 2,6, byrow=TRUE) } lapply(sample, remat) Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Kathryn Lord Sent: Wednesday, January 16, 2013 9:00 AM To: r-help@r-project.org Subject: [R] matrix manipulation with its rows Dear R users, I have a question about matrix manipulation with its rows. Plz see the simple example below sample - list(matrix(1:6, nr=2,nc=3), matrix(7:12, nr=2,nc=3), matrix(13:18,nr=2,nc=3)) sample [[1]] [,1] [,2] [,3] [1,]135 [2,]246 [[2]] [,1] [,2] [,3] [1,]79 11 [2,]8 10 12 [[3]] [,1] [,2] [,3] [1,] 13 15 17 [2,] 14 16 18 With this list, I'd like to create this below [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]135 000 [2,]000 246 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]79 11 000 [2,] 0008 10 12 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 13 15 17 000 [2,] 000 14 16 18 Any suggestion will be greatly appreciated. Regards, Kathryn Lord [[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@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] matrix manipulation with its rows
Not a great solution, I don't think, but: kronecker(diag(2), matrix(1:6, 2, byrow=TRUE))[c(1,4),] [,1] [,2] [,3] [,4] [,5] [,6] [1,]123000 [2,]000456 So using a function that does this in 'lapply' should solve the problem you state. I'm guessing the real problem might be more complex. Pat On 16/01/2013 07:59, Kathryn Lord wrote: Dear R users, I have a question about matrix manipulation with its rows. Plz see the simple example below sample - list(matrix(1:6, nr=2,nc=3), matrix(7:12, nr=2,nc=3), matrix(13:18,nr=2,nc=3)) sample [[1]] [,1] [,2] [,3] [1,]135 [2,]246 [[2]] [,1] [,2] [,3] [1,]79 11 [2,]8 10 12 [[3]] [,1] [,2] [,3] [1,] 13 15 17 [2,] 14 16 18 With this list, I'd like to create this below [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]135 000 [2,]000 246 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]79 11 000 [2,] 0008 10 12 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 13 15 17 000 [2,] 000 14 16 18 Any suggestion will be greatly appreciated. Regards, Kathryn Lord [[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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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] matrix manipulation with its rows
HI, You could also do this: lapply(sample1,function(x) {mat1-cbind(matrix(0,nrow=2,ncol=3),x); mat1[cbind(rep(1,3),1:3)]- mat1[cbind(rep(1,3),4:6)]; mat1[cbind(rep(1,3),4:6)]-0; mat1}) A.K. - Original Message - From: Kathryn Lord kathryn.lord2...@gmail.com To: r-help@r-project.org Cc: Sent: Wednesday, January 16, 2013 2:59 AM Subject: [R] matrix manipulation with its rows Dear R users, I have a question about matrix manipulation with its rows. Plz see the simple example below sample - list(matrix(1:6, nr=2,nc=3), matrix(7:12, nr=2,nc=3), matrix(13:18,nr=2,nc=3)) sample [[1]] [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [[2]] [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 [[3]] [,1] [,2] [,3] [1,] 13 15 17 [2,] 14 16 18 With this list, I'd like to create this below [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 3 5 0 0 0 [2,] 0 0 0 2 4 6 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 7 9 11 0 0 0 [2,] 0 0 0 8 10 12 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 13 15 17 0 0 0 [2,] 0 0 0 14 16 18 Any suggestion will be greatly appreciated. Regards, Kathryn Lord [[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@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] matrix manipulation
Dear Rlisters, I'm writing to ask how to manipulate a matrix or dataframe in a specific way. To elaborate, let's consider an example. Assume we have the following 3 by 4 matrix A with elements either 0 or 1, 0 1 1 0 1 0 1 1 0 0 0 1 From the original matrix A, I'd like to generate a new matrix B satisfing 1) B is initially set to be equal to A, and then 2) Each row of B is a weighted sum of rows of A 3) The weight is given by the number of common 1 shared by the rows. For row 1: it has one common 1 with row 2, thus the weight is 1; it has zero common 1 with row 3, thus the weight is 0; For row 2: it has one common 1 with row 1, thus the weight is 1; it has one common 1 with row 3, thus the weight is 1; For row 3: it has zero common 1 with row 1, thus the weight is 0; it has one common 1 with row 2, thus the weight is 1; 4) In this way, the new matrix B is 1 1 2 1 1 1 2 2 1 0 1 2 To do this, I can use loops for each row. But I'm not sure how to get the weight for each row efficiently. Second, I'd like to have a method to achieve the goal without loops since loops is slow when data becomes big. Any idea? thanks, Cal __ 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] matrix manipulation
On Thu, Jun 14, 2012 at 01:11:45PM +, G. Dai wrote: Dear Rlisters, I'm writing to ask how to manipulate a matrix or dataframe in a specific way. To elaborate, let's consider an example. Assume we have the following 3 by 4 matrix A with elements either 0 or 1, 0 1 1 0 1 0 1 1 0 0 0 1 From the original matrix A, I'd like to generate a new matrix B satisfing 1) B is initially set to be equal to A, and then 2) Each row of B is a weighted sum of rows of A 3) The weight is given by the number of common 1 shared by the rows. For row 1: it has one common 1 with row 2, thus the weight is 1; it has zero common 1 with row 3, thus the weight is 0; For row 2: it has one common 1 with row 1, thus the weight is 1; it has one common 1 with row 3, thus the weight is 1; For row 3: it has zero common 1 with row 1, thus the weight is 0; it has one common 1 with row 2, thus the weight is 1; 4) In this way, the new matrix B is 1 1 2 1 1 1 2 2 1 0 1 2 Hi. If i understand correctly, each row of B is obtained from the corresponding row of A by adding mutliples of other rows using the weights as described. Try the following. a - rbind( c(0, 1, 1, 0), c(1, 0, 1, 1), c(0, 0, 0, 1)) w - a %*% t(a) diag(w) - 1 w %*% a [,1] [,2] [,3] [,4] [1,]1121 [2,]1122 [3,]1012 The entries of w are the weights except of the diagonal, which is all ones, since each row of A contributes to the same row of B with weight one. Is this, what you are looking for? Petr Savicky. __ 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] matrix manipulation
thank you, Petr. This is exactly what I'm looking for in my post. An related question can be how to get an arbitrary weight, say if row1 and row 2 have 1 common value 1, then assign a weight 10, if row 1 and row 2 have 2 common value 1, then assign a weight 12. I'm not so sure how to expand your method. Really need to refresh my linear algebra;-) regards, cal@cowbo On 6/14/12, Petr Savicky savi...@cs.cas.cz wrote: On Thu, Jun 14, 2012 at 01:11:45PM +, G. Dai wrote: Dear Rlisters, I'm writing to ask how to manipulate a matrix or dataframe in a specific way. To elaborate, let's consider an example. Assume we have the following 3 by 4 matrix A with elements either 0 or 1, 0 1 1 0 1 0 1 1 0 0 0 1 From the original matrix A, I'd like to generate a new matrix B satisfing 1) B is initially set to be equal to A, and then 2) Each row of B is a weighted sum of rows of A 3) The weight is given by the number of common 1 shared by the rows. For row 1: it has one common 1 with row 2, thus the weight is 1; it has zero common 1 with row 3, thus the weight is 0; For row 2: it has one common 1 with row 1, thus the weight is 1; it has one common 1 with row 3, thus the weight is 1; For row 3: it has zero common 1 with row 1, thus the weight is 0; it has one common 1 with row 2, thus the weight is 1; 4) In this way, the new matrix B is 1 1 2 1 1 1 2 2 1 0 1 2 Hi. If i understand correctly, each row of B is obtained from the corresponding row of A by adding mutliples of other rows using the weights as described. Try the following. a - rbind( c(0, 1, 1, 0), c(1, 0, 1, 1), c(0, 0, 0, 1)) w - a %*% t(a) diag(w) - 1 w %*% a [,1] [,2] [,3] [,4] [1,]1121 [2,]1122 [3,]1012 The entries of w are the weights except of the diagonal, which is all ones, since each row of A contributes to the same row of B with weight one. Is this, what you are looking for? Petr Savicky. __ 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] matrix manipulation
On Thu, Jun 14, 2012 at 02:24:20PM -0400, cowboy wrote: thank you, Petr. This is exactly what I'm looking for in my post. An related question can be how to get an arbitrary weight, say if row1 and row 2 have 1 common value 1, then assign a weight 10, if row 1 and row 2 have 2 common value 1, then assign a weight 12. I'm not so sure how to expand your method. Hi. The weight matrix may be transformed. Try the following. # modified example to have also intersection 2 a - rbind( c(0, 1, 1, 1), c(1, 0, 1, 1), c(0, 0, 0, 1)) w - a %*% t(a) diag(w) - 0 w [,1] [,2] [,3] [1,]021 [2,]201 [3,]110 trans - c(1, 10, 12) w[, ] - trans[w + 1] w [,1] [,2] [,3] [1,]1 12 10 [2,] 121 10 [3,] 10 101 Hope this helps. Petr Savicky. __ 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] matrix Manipulation...
Hello everyone, I have a 2 x 5 matrix: say 0.2 0.3 1 -1 3 0.2. 0.4 5 0.5 -1 I want to replace all the values greater than or equal to 1 with 1 and those less than or equal to 0 with 0. So I should end up with a mtrix looking like: 0.2 0.3 1 0 1 0.2. 0.4 1 0.5 0 Any ideas how to do this? -- Thanks, Jim. [[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] matrix Manipulation...
It's very easy to do in two steps: testmat - matrix(c(.2, .3, 1, -1, 3, .2, .4, 5, .5, -1), byrow=TRUE, nrow=2) testmat [,1] [,2] [,3] [,4] [,5] [1,] 0.2 0.31 -1.03 [2,] 0.2 0.45 0.5 -1 testmat[testmat = 1] - 1 testmat[testmat 0] - 0 testmat [,1] [,2] [,3] [,4] [,5] [1,] 0.2 0.31 0.01 [2,] 0.2 0.41 0.50 This is pretty basic. You might want to read one of the many excellent intro to R guides, especially the subsetting section. Sarah On Wed, May 25, 2011 at 2:51 PM, Jim Silverton jim.silver...@gmail.com wrote: Hello everyone, I have a 2 x 5 matrix: say 0.2 0.3 1 -1 3 0.2. 0.4 5 0.5 -1 I want to replace all the values greater than or equal to 1 with 1 and those less than or equal to 0 with 0. So I should end up with a mtrix looking like: 0.2 0.3 1 0 1 0.2. 0.4 1 0.5 0 Any ideas how to do this? -- Thanks, Jim. -- Sarah Goslee http://www.functionaldiversity.org __ 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] Matrix manipulation in for loop
Hi all, I have a problem with getting my code to do what I want! This is the code I have: create.means.one.size-function(nsample,var,nboot){ mat.x-matrix(0,nrow=nboot,ncol=nsample) for(i in 1:nboot){ mat.x[i,]-sample(var,nsample,replace=T) } mean.mat-rep(0,nboot) for(i in 1:nboot){ mean.mat[i]-mean(mat.x[i,]) } sd.mean-sd(mean.mat) return(mean.mat) } where nsample here is a scalar. Then this is where I have the problem create.all.means-function(nsample,var,nboot){ MEANS-matrix(0,nrow=nboot,ncol=length(nsample)) for(j in nsample){ MEANS[,]-create.means.one.size(j,var,nboot) } return(A=list(MEANS=MEANS,nsample=nsample,std.dev=sd(MEANS))) } here nsample is a vector of the different sample sizes I want. This function should first create an empty matrix MEANS then it loops through the possible values of nsample each time it calls create means.one.size() and puts the output into the appropriate column of means. this function outputs the matrix MEANS and the vector nsample - both as part of a list However, the vector nsample could be c(1,3,5) but I want the outputs to go into columns 1, 2 and 3 rather than 1,3,5 (which I'm not even sure would work anyway!) Any help would be great! If I leave the code as it is and use the vector data-c(1,2,3) Then create.all.means(c(1,2),data,5) gives $MEANS [,1] [,2] [1,] 2.5 2.5 [2,] 1.0 1.0 [3,] 2.0 2.0 [4,] 2.0 2.0 [5,] 2.0 2.0 $nsample [1] 1 2 $std.dev1] 0.5477226 0.5477226[ which obviously isn't correct :( -- View this message in context: http://r.789695.n4.nabble.com/Matrix-manipulation-in-for-loop-tp3525849p3525849.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] Matrix manipulation in for loop
hello I think if you try this: for(j in 1: length(nsample)){ MEANS[,]-create.means.one.size(j,var,nboot) } it will work -- View this message in context: http://r.789695.n4.nabble.com/Matrix-manipulation-in-for-loop-tp3525849p3525888.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] Matrix manipulation
HI, here is another solution: int - sample(1:20,10) int [1] 10 4 5 2 14 17 9 11 16 13 mat-matrix(11:30,ncol=4) mat [,1] [,2] [,3] [,4] [1,] 11 16 21 26 [2,] 12 17 22 27 [3,] 13 18 23 28 [4,] 14 19 24 29 [5,] 15 20 25 30 mat[apply(mat,1, function(x) any(int==x[1])),] [,1] [,2] [,3] [,4] [1,] 11 16 21 26 [2,] 13 18 23 28 [3,] 14 19 24 29 Andrija On Sat, Apr 2, 2011 at 7:08 AM, Joseph N. Paulson josephpaul...@gmail.comwrote: Hi all! I have a vector, let's say for example int - sample(1:20,10); for now: now I have a matrix... M = m x n where the first column is a feature column and most likely shares at least one of the int (interesting) numbers. I want to extract the rows where int[] = M[,1] I thought: rownames(int)-int; rownames(M)-M[,1]; M[rownames(int),] would work, but it doesn't... (I assume because I have rownames(int) that are not found in M[,1]. Neither does, rownames(M)==rownames(int)... Any help would be greatly appreciated! Thank you! [[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. [[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] Matrix manipulation
Hi all! I have a vector, let's say for example int - sample(1:20,10); for now: now I have a matrix... M = m x n where the first column is a feature column and most likely shares at least one of the int (interesting) numbers. I want to extract the rows where int[] = M[,1] I thought: rownames(int)-int; rownames(M)-M[,1]; M[rownames(int),] would work, but it doesn't... (I assume because I have rownames(int) that are not found in M[,1]. Neither does, rownames(M)==rownames(int)... Any help would be greatly appreciated! Thank you! [[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] Matrix manipulation
Hi: Here' s one approach: int - sample(1:20,10) m - matrix(sample(1:40, 20), nrow = 10) int [1] 7 12 4 6 1 19 17 20 15 5 m [,1] [,2] [1,]9 15 [2,] 23 32 [3,] 40 14 [4,] 19 38 [5,] 286 [6,] 26 18 [7,] 34 22 [8,]7 35 [9,] 213 [10,] 39 12 m[m[, 1] %in% int, ] [,1] [,2] [1,] 19 38 [2,]7 35 HTH, Dennis On Fri, Apr 1, 2011 at 10:08 PM, Joseph N. Paulson josephpaul...@gmail.comwrote: Hi all! I have a vector, let's say for example int - sample(1:20,10); for now: now I have a matrix... M = m x n where the first column is a feature column and most likely shares at least one of the int (interesting) numbers. I want to extract the rows where int[] = M[,1] I thought: rownames(int)-int; rownames(M)-M[,1]; M[rownames(int),] would work, but it doesn't... (I assume because I have rownames(int) that are not found in M[,1]. Neither does, rownames(M)==rownames(int)... Any help would be greatly appreciated! Thank you! [[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. [[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] Matrix Manipulation
Hi, Is there a quick way to go from this matrix: A [,1] [,2] [,3] [1,]111 [2,]222 [3,]333 [4,]444 [5,]5 NA5 [6,] NA NA6 [7,] NA NA NA to this matrix: B [,1] [,2] [,3] [1,]1 NA NA [2,]2 NA1 [3,]312 [4,]423 [5,]534 [6,] NA45 [7,] NA NA6 without using a loop? For example using a vector which describes how many NA's are required from the top of the matrix- so in this case it would be c(0,2,1). Many thanks Emma -- View this message in context: http://r.789695.n4.nabble.com/Matrix-Manipulation-tp3027266p3027266.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] Matrix Manipulation
try this: x V2 V3 V4 [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 [4,] 4 4 4 [5,] 5 NA 5 [6,] NA NA 6 [7,] NA NA NA offset - c(0,2,1) # add the control to the data and make two copies so we can offset x.new - rbind(offset, x, x) result - apply(x.new, 2, function(.col){ + .col[seq(nrow(x) - .col[1L] + 2L, length = nrow(x))] + }) result V2 V3 V4 1 NA NA 2 NA 1 3 1 2 4 2 3 5 3 4 NA 4 5 NA NA 6 On Thu, Nov 4, 2010 at 11:47 AM, emj83 stp08...@shef.ac.uk wrote: Hi, Is there a quick way to go from this matrix: A [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 [4,] 4 4 4 [5,] 5 NA 5 [6,] NA NA 6 [7,] NA NA NA to this matrix: B [,1] [,2] [,3] [1,] 1 NA NA [2,] 2 NA 1 [3,] 3 1 2 [4,] 4 2 3 [5,] 5 3 4 [6,] NA 4 5 [7,] NA NA 6 without using a loop? For example using a vector which describes how many NA's are required from the top of the matrix- so in this case it would be c(0,2,1). Many thanks Emma -- View this message in context: http://r.789695.n4.nabble.com/Matrix-Manipulation-tp3027266p3027266.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.
Re: [R] Matrix Manipulation
Many thanks-its worked a treat :-) Emma -- View this message in context: http://r.789695.n4.nabble.com/Matrix-Manipulation-tp3027266p3027307.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] matrix manipulation problem
Dear fellow R-users, Say we have a matrix x, defined as follows set.seed(50) x - matrix(rbinom(100*5,1, p=0.75),nrow=100, ncol=5) Now the interpretation of x is that each for of x is actually a sequence of length 5, and i would like to transform x in such a way that I can describe the frequencies of sequences observed among the 100. How can I do this efficiently? Thanks for any assistance! Greg __ [[elided Yahoo spam]] [[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] matrix manipulation problem
On Tue, 1 Sep 2009, Gregory Gentlemen wrote: Dear fellow R-users, Say we have a matrix x, defined as follows set.seed(50) x - matrix(rbinom(100*5,1, p=0.75),nrow=100, ncol=5) Now the interpretation of x is that each for of x is actually a sequence of length 5, and i would like to transform x in such a way that I can describe the frequencies of sequences observed among the 100. How can I do this efficiently? If I understood the previous para: as.data.frame( xtabs( ~. , as.data.frame( x ) ) ) HTH, Chuck Thanks for any assistance! Greg __ [[elided Yahoo spam]] [[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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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] Matrix manipulation
Hello everyone, I have a couple of fairly simple questions (I hope) the answers to which I cannot find through the documentation at the moment. 1. I would like to delete the a row from a matrix if a certain elimination criterion is met. I am familiar with x - x[-7,] (to remove row 7, for example). Are there any other means of removing an entire row? 2. Is there a single command that will rename the index of each row to match the row number once a row has been deleted. For example, when row 7 is deleted above, the old row 8 is now row 7, but the row name is still 8. I have figured out how to assign a sequence vector to the row names, but I am wondering if there is a built-in command that does the same thing. (I.e., change the name of row 7 to 7 from 8.) Thank you very much. Payam -- Payam Minoofar, Ph.D. Scientist Meissner Filtration Products 4181 Calle Tesoro Camarillo, CA 93012 USA +1 805 388 9911 +1 805 388 5948 fax payam.minoo...@meissner.com [[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] Matrix manipulation
Try this: For the first and the second question: transform(subset(d, row.names(d) != 2), row.names=NULL) On Thu, Jun 11, 2009 at 3:53 PM, Payam Minoofar payam.minoo...@meissner.com wrote: Hello everyone, I have a couple of fairly simple questions (I hope) the answers to which I cannot find through the documentation at the moment. 1. I would like to delete the a row from a matrix if a certain elimination criterion is met. I am familiar with x - x[-7,] (to remove row 7, for example). Are there any other means of removing an entire row? 2. Is there a single command that will rename the index of each row to match the row number once a row has been deleted. For example, when row 7 is deleted above, the old row 8 is now row 7, but the row name is still 8. I have figured out how to assign a sequence vector to the row names, but I am wondering if there is a built-in command that does the same thing. (I.e., change the name of row 7 to 7 from 8.) Thank you very much. Payam -- Payam Minoofar, Ph.D. Scientist Meissner Filtration Products 4181 Calle Tesoro Camarillo, CA 93012 USA +1 805 388 9911 +1 805 388 5948 fax payam.minoo...@meissner.com [[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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[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] Matrix manipulation
On Jun 11, 2009, at 2:53 PM, Payam Minoofar wrote: Hello everyone, I have a couple of fairly simple questions (I hope) the answers to which I cannot find through the documentation at the moment. 1. I would like to delete the a row from a matrix if a certain elimination criterion is met. I am familiar with x - x[-7,] (to remove row 7, for example). Are there any other means of removing an entire row? ?which # useful for converting logical vectors into argument for functions that require numerics M10 - matrix(1:100, nrow = 10) # find row with 63 which( sapply( 1:10, function(x) 63 %in% M10[x, ]) ) [1] 3 M10[-which( sapply( 1:10, function(x) 63 %in% M10[x, ]) ), ] # remove row with 63 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,]1 11 21 31 41 51 61 71 8191 [2,]2 12 22 32 42 52 62 72 8292 [3,]4 14 24 34 44 54 64 74 8494 [4,]5 15 25 35 45 55 65 75 8595 [5,]6 16 26 36 46 56 66 76 8696 [6,]7 17 27 37 47 57 67 77 8797 [7,]8 18 28 38 48 58 68 78 8898 [8,]9 19 29 39 49 59 69 79 8999 [9,] 10 20 30 40 50 60 70 80 90 100 2. Is there a single command that will rename the index of each row to match the row number once a row has been deleted. For example, when row 7 is deleted above, the old row 8 is now row 7, but the row name is still 8. I have figured out how to assign a sequence vector to the row names, but I am wondering if there is a built-in command that does the same thing. (I.e., change the name of row 7 to 7 from 8.) Thank you very much. Payam -- Payam Minoofar, Ph.D. Scientist Meissner Filtration Products 4181 Calle Tesoro Camarillo, CA 93012 USA +1 805 388 9911 +1 805 388 5948 fax payam.minoo...@meissner.com [[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. David Winsemius, MD Heritage Laboratories 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.
Re: [R] Matrix manipulation
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius Sent: Thursday, June 11, 2009 1:49 PM To: Payam Minoofar Cc: r-help@r-project.org Subject: Re: [R] Matrix manipulation On Jun 11, 2009, at 2:53 PM, Payam Minoofar wrote: Hello everyone, I have a couple of fairly simple questions (I hope) the answers to which I cannot find through the documentation at the moment. 1. I would like to delete the a row from a matrix if a certain elimination criterion is met. I am familiar with x - x[-7,] (to remove row 7, for example). Are there any other means of removing an entire row? ?which # useful for converting logical vectors into argument for functions that require numerics M10 - matrix(1:100, nrow = 10) # find row with 63 which( sapply( 1:10, function(x) 63 %in% M10[x, ]) ) [1] 3 M10[-which( sapply( 1:10, function(x) 63 %in% M10[x, ]) ), ] # remove row with 63 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,]1 11 21 31 41 51 61 71 8191 [2,]2 12 22 32 42 52 62 72 8292 [3,]4 14 24 34 44 54 64 74 8494 [4,]5 15 25 35 45 55 65 75 8595 [5,]6 16 26 36 46 56 66 76 8696 [6,]7 17 27 37 47 57 67 77 8797 [7,]8 18 28 38 48 58 68 78 8898 [8,]9 19 29 39 49 59 69 79 8999 [9,] 10 20 30 40 50 60 70 80 90 100 which() is dangerous here. E.g., if you wanted to use that idiom to delete any row containing 666 you would get a 0-row by 10-column matrix, not the expected copy of the input matrix M10[-which( sapply( 1:10, function(x) 666 %in% M10[x, ]) ), ] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] (This happens because -integer(0) is no different than integer(0): both are 0-long vectors.) I think you should use logical subscripts unless you are really pressed for space. M10[ !sapply(1:10, function(x)666 %in% M10[x,]), ] When you read '[', say 'such that'. Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com 2. Is there a single command that will rename the index of each row to match the row number once a row has been deleted. For example, when row 7 is deleted above, the old row 8 is now row 7, but the row name is still 8. I have figured out how to assign a sequence vector to the row names, but I am wondering if there is a built-in command that does the same thing. (I.e., change the name of row 7 to 7 from 8.) Thank you very much. Payam -- Payam Minoofar, Ph.D. Scientist Meissner Filtration Products 4181 Calle Tesoro Camarillo, CA 93012 USA +1 805 388 9911 +1 805 388 5948 fax payam.minoo...@meissner.com [[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. David Winsemius, MD Heritage Laboratories 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@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] Matrix manipulation
Or perhaps: M10[rowSums(M10 == 63) == 0, ] On Thu, Jun 11, 2009 at 4:49 PM, David Winsemiusdwinsem...@comcast.net wrote: On Jun 11, 2009, at 2:53 PM, Payam Minoofar wrote: Hello everyone, I have a couple of fairly simple questions (I hope) the answers to which I cannot find through the documentation at the moment. 1. I would like to delete the a row from a matrix if a certain elimination criterion is met. I am familiar with x - x[-7,] (to remove row 7, for example). Are there any other means of removing an entire row? ?which # useful for converting logical vectors into argument for functions that require numerics M10 - matrix(1:100, nrow = 10) # find row with 63 which( sapply( 1:10, function(x) 63 %in% M10[x, ]) ) [1] 3 M10[-which( sapply( 1:10, function(x) 63 %in% M10[x, ]) ), ] # remove row with 63 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 4 14 24 34 44 54 64 74 84 94 [4,] 5 15 25 35 45 55 65 75 85 95 [5,] 6 16 26 36 46 56 66 76 86 96 [6,] 7 17 27 37 47 57 67 77 87 97 [7,] 8 18 28 38 48 58 68 78 88 98 [8,] 9 19 29 39 49 59 69 79 89 99 [9,] 10 20 30 40 50 60 70 80 90 100 2. Is there a single command that will rename the index of each row to match the row number once a row has been deleted. For example, when row 7 is deleted above, the old row 8 is now row 7, but the row name is still 8. I have figured out how to assign a sequence vector to the row names, but I am wondering if there is a built-in command that does the same thing. (I.e., change the name of row 7 to 7 from 8.) Thank you very much. Payam -- Payam Minoofar, Ph.D. Scientist Meissner Filtration Products 4181 Calle Tesoro Camarillo, CA 93012 USA +1 805 388 9911 +1 805 388 5948 fax payam.minoo...@meissner.com [[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. David Winsemius, MD Heritage Laboratories 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@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] Matrix manipulation
From: metal_lical...@live.com To: r-help@r-project.org Subject: About Matrix Date: Tue, 28 Apr 2009 11:28:43 +0300 Hi, Dear R users, I have a question: I have A matrix which is 11519X14 and B matrix which is 1764X14, How do I get C matrix which is The remaining matrix after removing B from A? Thanks a lot. Kind regards, Tammy check out the rest of the Windows Live. More than mailWindows Live goes way beyond your inbox. More than messages _ Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! http://spaces.live.com/spacesapi.aspx?wx_action=createwx_url=/friends.aspxmkt=en-us [[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] Matrix manipulation
use the '-' feature. mat - matrix(rnorm(100), nrow = 10) #snip the second row mat[-2,] #snip the third column mat[,-3] #snip rows 5 and 7 mat[-c(5,7),] cheers tc On 10/23/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi everyone, suppose I have a 2D matrix, is there a command to snip out a specific row/column and then remerge the remaining columns/rows back into a contiguous matrix? I will need to repeat this operation quite a bit(reverse selection). Thanks for any insights you can offer. Yifei __ 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. -- Tim Calkins 0406 753 997 __ 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.