[R] How to compare X1 = X2 = ... = Xn?

Gabor Grothendieck ggrothendieck at myway.com
Mon Jul 19 20:32:44 CEST 2004


F Duan <f.duan <at> yale.edu> writes:

> I have a data frame with n columns: X1, X2, ., Xn. Now I want to create a
> new column: if X1 = X2 = . = Xn, the value is 1; Otherwise, the value is 0.

Here is one possibility if your data frame is all numeric:

   x$new <- (sd(t(x))==0)+0

x is the data frame, new is the new column, sd(t(x)) is TRUE for
rows with zero standard deviation (which occurs iff the entries
are all zero) and FALSE otherwise and +0 converts that to 1 or 0.




More information about the R-help mailing list