csmeredith
2012-Sep-12 20:45 UTC
[R] how to create a substraction matrix (subtract a row of every column from the same row in other columns)
Hello I have data like this x1 x2 x3 x4 x5 I want to create a matrix similar to a correlation matrix, but with the difference between the two values, like this x1 x2 x3 x4 x5 x1 x2-x1 x3-x1 x4-x1 x5-x1 x2 x3-x2 x4-x2 x5-x2 x3 x4-x3 x5-x3 x4 x5-x4 x5 Then I want to convert it back to a data frame with a column that describes what columns were used in the calculation, but I would like to only include the comparisons that go forward in number. Basically, each number represents a year, and I want each comparison to only be listed once, the difference between the 1st year the sample was taken and then 2nd year the sample was taken. (This would probably entail just taking a portion of the matrix.) . This is what the result would be difftype value x1x2 x1x3 x1x4 x1x5 x2x3 x2x4 x2x5 x3x4 x3x5 x4x5 Thank you for any help you can give me. -- View this message in context: r.789695.n4.nabble.com/how-to-create-a-substraction-matrix-subtract-a-row-of-every-column-from-the-same-row-in-other-column-tp4642949.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2012-Sep-13 09:04 UTC
[R] how to create a substraction matrix (subtract a row of every column from the same row in other columns)
Hello, I'm not sure if this is what you want. dat <- data.frame(matrix(1:25 + rnorm(25), ncol=5, dimnames=list(NULL, paste0("x", 1:5)))) d2 <- apply(dat, 1, diff) nms <- t(outer(names(dat), names(dat), paste0)) res <- data.frame(difftype = nms[lower.tri(nms)], value = d2[lower.tri(d2, diag = TRUE)]) res Hope this helps, Rui Barradas Em 12-09-2012 21:45, csmeredith escreveu:> Hello > I have data like this > > x1 x2 x3 x4 x5 > > I want to create a matrix similar to a correlation matrix, but with the > difference between the two values, like this > > x1 x2 x3 x4 x5 > x1 x2-x1 x3-x1 x4-x1 x5-x1 > x2 x3-x2 x4-x2 x5-x2 > x3 x4-x3 x5-x3 > x4 x5-x4 > x5 > > Then I want to convert it back to a data frame with a column that describes > what columns were used in the calculation, but I would like to only include > the comparisons that go forward in number. Basically, each number represents > a year, and I want each comparison to only be listed once, the difference > between the 1st year the sample was taken and then 2nd year the sample was > taken. (This would probably entail just taking a portion of the matrix.) . > This is what the result would be > > difftype value > x1x2 > x1x3 > x1x4 > x1x5 > x2x3 > x2x4 > x2x5 > x3x4 > x3x5 > x4x5 > > Thank you for any help you can give me. > > > > > -- > View this message in context: r.789695.n4.nabble.com/how-to-create-a-substraction-matrix-subtract-a-row-of-every-column-from-the-same-row-in-other-column-tp4642949.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Ben Bolker
2012-Sep-13 12:20 UTC
[R] how to create a substraction matrix (subtract a row of every column from the same row in other columns)
csmeredith <csmeredith <at> fs.fed.us> writes:>[snip]> I want to create a matrix similar to a correlation matrix, but with the > difference between the two values, like this > > x1 x2 x3 x4 x5 > x1 x2-x1 x3-x1 x4-x1 x5-x1 > x2 x3-x2 x4-x2 x5-x2 > x3 x4-x3 x5-x3 > x4 x5-x4 > x5 > > Then I want to convert it back to a data frame with a column that describes > what columns were used in the calculation, but I would like to only include > the comparisons that go forward in number. Basically, each number represents > a year, and I want each comparison to only be listed once, the difference > between the 1st year the sample was taken and then 2nd year the sample was > taken. (This would probably entail just taking a portion of the matrix.) .[snip] You might want to start with (something like) library(reshape2); subset(melt(outer(x,x,"-")), Var1>Var2) and then remove redundant rows ... I haven't actually tested any of that, but it should get you started.
arun
2012-Sep-13 18:14 UTC
[R] how to create a substraction matrix (subtract a row of every column from the same row in other columns)
Hi, Try this: vec1<-1:5 library(MCMCpack) ?cbind(rep(0,4),xpnd(diff(combn(vec1,2)),4)) ##???? [,1] [,2] [,3] [,4] [,5] #[1,]??? 0??? 1??? 2??? 3??? 4 #[2,]??? 0??? 2??? 1??? 2??? 3 #[3,]??? 0??? 3??? 2??? 1??? 2 #[4,]??? 0??? 4??? 3??? 2??? 1 dat1<-data.frame(difftype= paste0("x",combn(vec1,2,paste,collapse="x")),value=as.vector(diff(combn(vec1,2)))) ?head(dat1) #? difftype value #1???? x1x2???? 1 #2???? x1x3???? 2 #3???? x1x4???? 3 #4???? x1x5???? 4 #5???? x2x3???? 1 #6???? x2x4???? 2 A.K. ----- Original Message ----- From: csmeredith <csmeredith at fs.fed.us> To: r-help at r-project.org Cc: Sent: Wednesday, September 12, 2012 4:45 PM Subject: [R] how to create a substraction matrix (subtract a row of every column from the same row in other columns) Hello I have data like this x1? x2? x3? x4? x5 I want to create a matrix similar to a correlation matrix, but with the difference between the two values, like this x1? x2? ? ? ? x3? ? ? ? x4? ? ? ? ? x5 x1? x2-x1? ? x3-x1? ? x4-x1? ? x5-x1 x2? ? ? ? ? ? ? x3-x2? ? x4-x2? ? x5-x2 x3? ? ? ? ? ? ? ? ? ? ? ? ? x4-x3? ? ? x5-x3 x4? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x5-x4 x5 Then I want to convert it back to a data frame with a column that describes what columns were used in the calculation, but I would like to only include the comparisons that go forward in number. Basically, each number represents a year, and I want each comparison to only be listed once, the difference between the 1st year the sample was taken and then 2nd year the sample was taken. (This would probably entail just taking a portion of the matrix.) . This is what the result would be difftype value x1x2? ? ? x1x3 x1x4 x1x5 x2x3 x2x4 x2x5 x3x4 x3x5 x4x5 Thank you for any help you can give me. -- View this message in context: r.789695.n4.nabble.com/how-to-create-a-substraction-matrix-subtract-a-row-of-every-column-from-the-same-row-in-other-column-tp4642949.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.