mauede at alice.it
2010-Apr-17 18:25 UTC
[R] how to remove one row at a time from a matrix keeping its nrow consistent
After some headache with debugging my script, I finally isolated the problem taht I am going to illustrate in the following example. I expected matrix nrow to decrease consistently till 1. Instead, when the matrix is left with one row only, its nrow jumps to 2 because the matrix gets transposed. How come ? Thank you, Maura> B <- c(1,2) > B <- rbind(B,c(3,4)) > B <- rbind(B,c(5,6)) > B[,1] [,2] B 1 2 3 4 5 6> dim(B)[1] 3 2> nrow(B)[1] 3> > #REMOVE ROW-1 OUT OF 3 > B <- as.matrix(B[-1,]) > B[,1] [,2] 3 4 5 6> dim(B)[1] 2 2> nrow(B)[1] 2> > #REMOVE ROW-2 OUT OF 3 > B <- as.matrix(B[-1,]) > B[,1] [1,] 5 [2,] 6> dim(B)[1] 2 1> nrow(B)[1] 2 tutti i telefonini TIM! [[alternative HTML version deleted]]
Ista Zahn
2010-Apr-17 18:43 UTC
[R] how to remove one row at a time from a matrix keeping its nrow consistent
On Sat, Apr 17, 2010 at 7:25 PM, <mauede at alice.it> wrote:> After some headache with debugging my script, I finally isolated the problem taht I am going to illustrate in the following example. > I expected matrix nrow to decrease consistently till 1. Instead, when the matrix is left with one row only, its nrow jumps to 2 because the matrix > gets transposed. How come ?Because the one-row matrix gets reduced to a vector:> B <- matrix(1:4, nrow = 2, byrow = TRUE) > class(B)[1] "matrix"> B1 <- B[-1,] > class(B1)[1] "integer" and as.matrix applied to a vector produces a one-column matrix (see ?as.matrix).> Thank you, > Maura > >> B <- c(1,2) >> ?B <- rbind(B,c(3,4)) >> ?B <- rbind(B,c(5,6)) >> ?B > ?[,1] [,2] > B ? ?1 ? ?2 > ? ? 3 ? ?4 > ? ? 5 ? ?6 >> dim(B) > [1] 3 2 >> nrow(B) > [1] 3 >> >> #REMOVE ROW-1 OUT OF 3 >> ?B <- as.matrix(B[-1,]) >> ?B > ?[,1] [,2] > ? ?3 ? ?4 > ? ?5 ? ?6 >> dim(B) > [1] 2 2 >> nrow(B) > [1] 2 >> >> #REMOVE ROW-2 OUT OF 3 >> ?B <- as.matrix(B[-1,]) >> ?B > ? ? [,1] > [1,] ? ?5 > [2,] ? ?6 >> dim(B) > [1] 2 1 >> nrow(B) > [1] 2 > > > > tutti i telefonini TIM! > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Henrique Dallazuanna
2010-Apr-17 18:53 UTC
[R] how to remove one row at a time from a matrix keeping its nrow consistent
Try this indeed of as.matrix(...) : B <- B[-1,, drop = FALSE] On Sat, Apr 17, 2010 at 3:25 PM, <mauede@alice.it> wrote:> After some headache with debugging my script, I finally isolated theproblem taht I am going to illustrate in the following example.> I expected matrix nrow to decrease consistently till 1. Instead, when thematrix is left with one row only, its nrow jumps to 2 because the matrix> gets transposed. How come ? > Thank you, > Maura > >> B <- c(1,2) >> B <- rbind(B,c(3,4)) >> B <- rbind(B,c(5,6)) >> B > [,1] [,2] > B 1 2 > 3 4 > 5 6 >> dim(B) > [1] 3 2 >> nrow(B) > [1] 3 >> >> #REMOVE ROW-1 OUT OF 3 >> B <- as.matrix(B[-1,]) >> B > [,1] [,2] > 3 4 > 5 6 >> dim(B) > [1] 2 2 >> nrow(B) > [1] 2 >> >> #REMOVE ROW-2 OUT OF 3 >> B <- as.matrix(B[-1,]) >> B > [,1] > [1,] 5 > [2,] 6 >> dim(B) > [1] 2 1 >> nrow(B) > [1] 2 > > > > tutti i telefonini TIM! > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://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]]
David Winsemius
2010-Apr-17 18:53 UTC
[R] how to remove one row at a time from a matrix keeping its nrow consistent
On Apr 17, 2010, at 2:25 PM, <mauede at alice.it> wrote:> After some headache with debugging my script, I finally isolated the > problem taht I am going to illustrate in the following example. > I expected matrix nrow to decrease consistently till 1. Instead, > when the matrix is left with one row only, its nrow jumps to 2 > because the matrix > gets transposed. How come ?Because you did not use drop=FALSE? (And I think it's a FAQ.) > #REMOVE ROW-1 OUT OF 3 > B <- as.matrix(B[-1,,drop=FALSE]) > B [,1] [,2] 3 4 5 6 > #REMOVE ROW-2 OUT OF 3 > B <- as.matrix(B[-1, ,drop=FALSE]) > B [,1] [,2] 5 6 > nrow(B) [1] 1> Thank you, > Maura > >> B <- c(1,2) >> B <- rbind(B,c(3,4)) >> B <- rbind(B,c(5,6)) >> B > [,1] [,2] > B 1 2 > 3 4 > 5 6 >> dim(B) > [1] 3 2 >> nrow(B) > [1] 3 >> >> #REMOVE ROW-1 OUT OF 3 >> B <- as.matrix(B[-1,]) >> B > [,1] [,2] > 3 4 > 5 6 >> dim(B) > [1] 2 2 >> nrow(B) > [1] 2 >> >> #REMOVE ROW-2 OUT OF 3 >> B <- as.matrix(B[-1,]) >> B > [,1] > [1,] 5 > [2,] 6 >> dim(B) > [1] 2 1 >> nrow(B) > [1] 2 > > > > tutti i telefonini TIM! > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 West Hartford, CT