Please use dput() to post your example matrix. Rambler1 wrote> > I have run into a problem in my code. What I want to accomplish is this: > I have a user input stock symbols into a list and from there I run the > quantmod package to get historical data. I compute the correlation matrix > and then turn that matrix into a simple matrix with 1's or 0's depending > on how strong the correlation is. From here I would like to go into the > matrix and take all the 1's and match them with their row/column titles. > So for each 1 I have two stock symblos. For example: > MMM ACE ABT ANF HD PEP K GOOG BIDU JNJ > MMM 0 1 0 0 0 1 0 0 0 > 0 > ACE 1 0 0 0 0 0 0 0 > 0 1 > ABT 0 0 0 0 0 1 0 0 > 0 1 > ANF 0 0 0 0 0 0 0 0 > 0 0 > HD 0 0 0 0 0 0 0 0 > 0 0 > PEP 1 0 1 0 0 0 0 0 > 0 1 > K 0 0 0 0 0 0 0 0 > 0 0 > GOOG 0 0 0 0 0 0 0 0 0 > 0 > BIDU 0 0 0 0 0 0 0 0 > 0 0 > JNJ 0 1 1 0 0 1 0 0 > 0 0 > > Say I have this matrix computed. I would like to locate every 1 and the > corresponding two names that go with it. (eg MMM/ACE, MMM/PEP and so on.) > Thank you for your help! >-- View this message in context: http://r.789695.n4.nabble.com/Grabbing-Column-and-Row-titles-tp4332136p4333698.html Sent from the R help mailing list archive at Nabble.com.
This is true with regard to all things you don't understand in R... use question mark (?) # this will show you the manual, or help page ?dput also, make sure you hit the "quote" button when you reply on this forum so that people know what you are replying to. I used dput() to create the following (see previous post): x <- structure(list(a = c(0L, 1L, 0L), b = c(1L, 0L, 1L), c = c(0L, 1L, 0L)), .Names = c("a", "b", "c"), class = "data.frame", row.names c("a", "b", "c")) now see what "x" is:> xa b c a 0 1 0 b 1 0 1 c 0 1 0 now use dput():> dput(x)structure(list(a = c(0L, 1L, 0L), b = c(1L, 0L, 1L), c = c(0L, 1L, 0L)), .Names = c("a", "b", "c"), class = "data.frame", row.names c("a", "b", "c")) Now if you paste this in your post, people can easily "play" around with your data and try to help. Good luck with your endeavors. Rambler1 wrote> > Thank you very much I will try this and see how it goes. Also what do you > mean by using dput() to post? I'm new to the blog. Than you again. >-- View this message in context: http://r.789695.n4.nabble.com/Grabbing-Column-and-Row-titles-tp4332136p4334377.html Sent from the R help mailing list archive at Nabble.com.
I would suggest reading some introductory manuals on R; specifically (with regards to your question) how to construct a function. Basically, "dat" is a variable input to the function. Everywhere you see "dat" is replaced by whatever you put in; for example: f(x) puts the data.frame "x" in for "dat" throughout the function. If you plan of expanding this function, I think you need to read a little bit first (perhaps a lot actually) so you know the basic mechanics. happy reading. Rambler1 wrote> > in this code: > > f <- function(dat, ...) { > dat[upper.tri(dat, TRUE)] <- NA > i <- which(dat == 1, arr.ind = TRUE) > data.frame(matrix(colnames(dat)[as.vector(i)], ncol = 2)) > } > > I am planning to make this part of a larger function is there anyway I can > extract the code without the "function(dat,...){}" and have it run in a > larger function? > Also what is the context of the command "dat" R is very new and confusing > to me and sometimes ?# doesn't fully explain it for me. Thank you! > > > > chuck.01 wrote >> >> This is true with regard to all things you don't understand in R... use >> question mark (?) # this will show you the manual, or help page >> >> ?dput >> >> also, make sure you hit the "quote" button when you reply on this forum >> so that people know what you are replying to. >> >> I used dput() to create the following (see previous post): >> >> x <- structure(list(a = c(0L, 1L, 0L), b = c(1L, 0L, 1L), c = c(0L, >> 1L, 0L)), .Names = c("a", "b", "c"), class = "data.frame", row.names >> c("a", >> "b", "c")) >> >> now see what "x" is: >> >>> x >> a b c >> a 0 1 0 >> b 1 0 1 >> c 0 1 0 >> >> >> now use dput(): >> >>> dput(x) >> structure(list(a = c(0L, 1L, 0L), b = c(1L, 0L, 1L), c = c(0L, >> 1L, 0L)), .Names = c("a", "b", "c"), class = "data.frame", row.names >> c("a", >> "b", "c")) >> >> Now if you paste this in your post, people can easily "play" around with >> your data and try to help. >> >> Good luck with your endeavors. >> >> >> >> >> Rambler1 wrote >>> >>> Thank you very much I will try this and see how it goes. Also what do >>> you mean by using dput() to post? I'm new to the blog. Than you again. >>> >> >-- View this message in context: http://r.789695.n4.nabble.com/Grabbing-Column-and-Row-titles-tp4332136p4334714.html Sent from the R help mailing list archive at Nabble.com.
Hello, Please check function which().> > m <- matrix(1:12,3,4) > which(m==5, arr.ind=T) row col[1,] 2 2> which(m==9, arr.ind=T) row col [1,] 3 3>Regards, Carlos Ortega www.qualityexcellence.es 2012/1/27 chuck.01 <CharlieTheBrown77@gmail.com>> Please use dput() to post your example matrix. > > > > > Rambler1 wrote > > > > I have run into a problem in my code. What I want to accomplish is this: > > I have a user input stock symbols into a list and from there I run the > > quantmod package to get historical data. I compute the correlation matrix > > and then turn that matrix into a simple matrix with 1's or 0's depending > > on how strong the correlation is. From here I would like to go into the > > matrix and take all the 1's and match them with their row/column titles. > > So for each 1 I have two stock symblos. For example: > > MMM ACE ABT ANF HD PEP K GOOG BIDU JNJ > > MMM 0 1 0 0 0 1 0 0 0 > > 0 > > ACE 1 0 0 0 0 0 0 0 > > 0 1 > > ABT 0 0 0 0 0 1 0 0 > > 0 1 > > ANF 0 0 0 0 0 0 0 0 > > 0 0 > > HD 0 0 0 0 0 0 0 0 > > 0 0 > > PEP 1 0 1 0 0 0 0 0 > > 0 1 > > K 0 0 0 0 0 0 0 0 > > 0 0 > > GOOG 0 0 0 0 0 0 0 0 0 > > 0 > > BIDU 0 0 0 0 0 0 0 0 > > 0 0 > > JNJ 0 1 1 0 0 1 0 0 > > 0 0 > > > > Say I have this matrix computed. I would like to locate every 1 and the > > corresponding two names that go with it. (eg MMM/ACE, MMM/PEP and so on.) > > Thank you for your help! > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Grabbing-Column-and-Row-titles-tp4332136p4333698.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. >[[alternative HTML version deleted]]