Dear all, I apologize if my question is quite simple. I have a dataset (20 columns & 1000 rows) which some of columns have the same value and the others have different values. Here are some piece of my dataset: obj <- cbind(c(1,1,1,4,0,0,1,4,-1), c(0,1,1,4,1,0,1,4,-1), c(1,1,1,4,2,0,1,4,-1), c(1,1,1,4,3,0,1,4,-1), c(1,1,1,4,6,0,1,5,-1), c(1,1,1,4,6,0,1,6,-1), c(1,1,1,4,6,0,1,7,-1), c(1,1,1,4,6,0,1,8,-1)) obj.tr <- t(obj) obj.tr> obj.tr[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 1 1 4 0 0 1 4 -1 [2,] 0 1 1 4 1 0 1 4 -1 [3,] 1 1 1 4 2 0 1 4 -1 [4,] 1 1 1 4 3 0 1 4 -1 [5,] 1 1 1 4 6 0 1 5 -1 [6,] 1 1 1 4 6 0 1 6 -1 [7,] 1 1 1 4 6 0 1 7 -1 [8,] 1 1 1 4 6 0 1 8 -1>How can I do to check columns 2,3,4,6,7 and 9 have the same value, and columns 1,5 and 8 have different values. Best, Muhammad Subianto
On 8/24/06, Muhammad Subianto <msubianto at gmail.com> wrote:> I have a dataset (20 columns & 1000 rows) which > some of columns have the same value and the others > have different values. > Here are some piece of my dataset: > obj <- cbind(c(1,1,1,4,0,0,1,4,-1), > c(0,1,1,4,1,0,1,4,-1), > c(1,1,1,4,2,0,1,4,-1), > c(1,1,1,4,3,0,1,4,-1), > c(1,1,1,4,6,0,1,5,-1), > c(1,1,1,4,6,0,1,6,-1), > c(1,1,1,4,6,0,1,7,-1), > c(1,1,1,4,6,0,1,8,-1)) > obj.tr <- t(obj) > obj.tr > > obj.tr > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] > [1,] 1 1 1 4 0 0 1 4 -1 > [2,] 0 1 1 4 1 0 1 4 -1 > [3,] 1 1 1 4 2 0 1 4 -1 > [4,] 1 1 1 4 3 0 1 4 -1 > [5,] 1 1 1 4 6 0 1 5 -1 > [6,] 1 1 1 4 6 0 1 6 -1 > [7,] 1 1 1 4 6 0 1 7 -1 > [8,] 1 1 1 4 6 0 1 8 -1 > > > > How can I do to check columns 2,3,4,6,7 and 9 have > the same value, and columns 1,5 and 8 have different values.Just try all(obj.tr[,2]==obj.tr[1,2]) and so on for the other columns. See ? all. Paul
--- Muhammad Subianto <msubianto at gmail.com> wrote:> Dear all, > I apologize if my question is quite simple. > I have a dataset (20 columns & 1000 rows) which > some of columns have the same value and the others > have different values. > Here are some piece of my dataset: > obj <- cbind(c(1,1,1,4,0,0,1,4,-1), > c(0,1,1,4,1,0,1,4,-1), > c(1,1,1,4,2,0,1,4,-1), > c(1,1,1,4,3,0,1,4,-1), > c(1,1,1,4,6,0,1,5,-1), > c(1,1,1,4,6,0,1,6,-1), > c(1,1,1,4,6,0,1,7,-1), > c(1,1,1,4,6,0,1,8,-1)) > obj.tr <- t(obj) > obj.tr > > obj.tr > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] > [1,] 1 1 1 4 0 0 1 4 -1 > [2,] 0 1 1 4 1 0 1 4 -1 > [3,] 1 1 1 4 2 0 1 4 -1 > [4,] 1 1 1 4 3 0 1 4 -1 > [5,] 1 1 1 4 6 0 1 5 -1 > [6,] 1 1 1 4 6 0 1 6 -1 > [7,] 1 1 1 4 6 0 1 7 -1 > [8,] 1 1 1 4 6 0 1 8 -1 > > > > How can I do to check columns 2,3,4,6,7 and 9 have > the same value, and columns 1,5 and 8 have different > values. > > Best, Muhammad SubiantoThere has to be a better way but this will let you check visually since you only have 20 columns for(i in 1:8) { tt <-table(obj.tr[,i]) print( i) print (tt) }
Try sd(obj.tr) which will give a vector of standard deviations, one per column. A column's entry will be zero if and only if all values in the column are the same. On 8/24/06, Muhammad Subianto <msubianto at gmail.com> wrote:> Dear all, > I apologize if my question is quite simple. > I have a dataset (20 columns & 1000 rows) which > some of columns have the same value and the others > have different values. > Here are some piece of my dataset: > obj <- cbind(c(1,1,1,4,0,0,1,4,-1), > c(0,1,1,4,1,0,1,4,-1), > c(1,1,1,4,2,0,1,4,-1), > c(1,1,1,4,3,0,1,4,-1), > c(1,1,1,4,6,0,1,5,-1), > c(1,1,1,4,6,0,1,6,-1), > c(1,1,1,4,6,0,1,7,-1), > c(1,1,1,4,6,0,1,8,-1)) > obj.tr <- t(obj) > obj.tr > > obj.tr > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] > [1,] 1 1 1 4 0 0 1 4 -1 > [2,] 0 1 1 4 1 0 1 4 -1 > [3,] 1 1 1 4 2 0 1 4 -1 > [4,] 1 1 1 4 3 0 1 4 -1 > [5,] 1 1 1 4 6 0 1 5 -1 > [6,] 1 1 1 4 6 0 1 6 -1 > [7,] 1 1 1 4 6 0 1 7 -1 > [8,] 1 1 1 4 6 0 1 8 -1 > > > > How can I do to check columns 2,3,4,6,7 and 9 have > the same value, and columns 1,5 and 8 have different values. > > Best, Muhammad Subianto > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
As a minor footnote to both of these, I would add that both assume that all the columns of the dataset are numeric. It doesn't cost much to generalize it to cover any matrix structure, of any mode: constantColmuns <- function(Xmat) which(apply(Xmat, 2, function(z) length(unique(z)) == 1))> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Berton Gunter> Sent: Friday, 25 August 2006 9:37 AM > To: 'Gabor Grothendieck'; 'Muhammad Subianto' > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] Check values in colums matrix > > Absolutely. But do note that if the values in obj are the product of > numerical computations then columns of equal values may turn out to beonly> **nearly** equal and so the sd may turn out to be **nearly** 0 and not > exactly 0. This is a standard issue in numerical computation, ofcourse, and> has been commented on in this list at least dozens of times, but it'sstill> a gotcha for the unwary (so now dozens +1). > > -- Bert Gunter > Genentech Non-Clinical Statistics > South San Francisco, CA > > > > > -----Original Message----- > > From: r-help-bounces at stat.math.ethz.ch > > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gabor > > Grothendieck > > Sent: Thursday, August 24, 2006 4:28 PM > > To: Muhammad Subianto > > Cc: r-help at stat.math.ethz.ch > > Subject: Re: [R] Check values in colums matrix > > > > Try sd(obj.tr) which will give a vector of standard > > deviations, one per column. > > A column's entry will be zero if and only if all values in thecolumn> > are the same. > > > > On 8/24/06, Muhammad Subianto <msubianto at gmail.com> wrote: > > > Dear all, > > > I apologize if my question is quite simple. > > > I have a dataset (20 columns & 1000 rows) which > > > some of columns have the same value and the others > > > have different values. > > > Here are some piece of my dataset: > > > obj <- cbind(c(1,1,1,4,0,0,1,4,-1), > > > c(0,1,1,4,1,0,1,4,-1), > > > c(1,1,1,4,2,0,1,4,-1), > > > c(1,1,1,4,3,0,1,4,-1), > > > c(1,1,1,4,6,0,1,5,-1), > > > c(1,1,1,4,6,0,1,6,-1), > > > c(1,1,1,4,6,0,1,7,-1), > > > c(1,1,1,4,6,0,1,8,-1)) > > > obj.tr <- t(obj) > > > obj.tr > > > > obj.tr > > > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] > > > [1,] 1 1 1 4 0 0 1 4 -1 > > > [2,] 0 1 1 4 1 0 1 4 -1 > > > [3,] 1 1 1 4 2 0 1 4 -1 > > > [4,] 1 1 1 4 3 0 1 4 -1 > > > [5,] 1 1 1 4 6 0 1 5 -1 > > > [6,] 1 1 1 4 6 0 1 6 -1 > > > [7,] 1 1 1 4 6 0 1 7 -1 > > > [8,] 1 1 1 4 6 0 1 8 -1 > > > > > > > > > > How can I do to check columns 2,3,4,6,7 and 9 have > > > the same value, and columns 1,5 and 8 have different values. > > > > > > Best, Muhammad Subianto
Dear all, I would like to thank everybody who replied for their useful suggestions. Maybe, I am going through the book statistics to teach (fresh) myself. Wish you have a nice weekend. Regards, Muhammad Subianto On this day 24/08/2006 18:59, Muhammad Subianto wrote:> Dear all, > I apologize if my question is quite simple. > I have a dataset (20 columns & 1000 rows) which > some of columns have the same value and the others > have different values. > Here are some piece of my dataset: > obj <- cbind(c(1,1,1,4,0,0,1,4,-1), > c(0,1,1,4,1,0,1,4,-1), > c(1,1,1,4,2,0,1,4,-1), > c(1,1,1,4,3,0,1,4,-1), > c(1,1,1,4,6,0,1,5,-1), > c(1,1,1,4,6,0,1,6,-1), > c(1,1,1,4,6,0,1,7,-1), > c(1,1,1,4,6,0,1,8,-1)) > obj.tr <- t(obj) > obj.tr >> obj.tr > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] > [1,] 1 1 1 4 0 0 1 4 -1 > [2,] 0 1 1 4 1 0 1 4 -1 > [3,] 1 1 1 4 2 0 1 4 -1 > [4,] 1 1 1 4 3 0 1 4 -1 > [5,] 1 1 1 4 6 0 1 5 -1 > [6,] 1 1 1 4 6 0 1 6 -1 > [7,] 1 1 1 4 6 0 1 7 -1 > [8,] 1 1 1 4 6 0 1 8 -1 > > How can I do to check columns 2,3,4,6,7 and 9 have > the same value, and columns 1,5 and 8 have different values. > > Best, Muhammad Subianto > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >