Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a column contains only identical elements in R?

Tags:

r

Example data:

x <- matrix(c("Stack","Stack","Stack",
              "Overflow","Overflow","wolfrevO"),
            nrow=3,ncol=2)

How can I check whether x[,1] contains entirely identical elements?

If x contains NAs, does this method still apply?

Thanks

like image 752
Unstack Avatar asked Aug 12 '15 14:08

Unstack


People also ask

How do I check if a column is unique in R?

To find unique values in a column in a data frame, use the unique() function in R. In Exploratory Data Analysis, the unique() function is crucial since it detects and eliminates duplicate values in the data.

How do you check if values are the same in R?

setequal() function in R Language is used to check if two objects are equal. This function takes two objects like Vectors, dataframes, etc. as arguments and results in TRUE or FALSE, if the Objects are equal or not.

How do you check if all elements in a vector are equal R?

Method 2: Using length() and unique() function By using unique function if all the elements are the same then the length is 1 so by this way if the length is 1, we can say all elements in a vector are equal.

What is unique function in R?

The unique() function in R is used to eliminate or delete the duplicate values or the rows present in the vector, data frame, or matrix as well. The unique() function found its importance in the EDA (Exploratory Data Analysis) as it directly identifies and eliminates the duplicate values in the data.


2 Answers

You count the unique elements of the column:

length(unique(x[,1]))==1

works even if there are NA's in your data.

For checking every column use:

apply(x, 2, function(a) length(unique(a))==1)
like image 188
MarkusN Avatar answered Oct 04 '22 02:10

MarkusN


You can compare the vector's first value to the rest of the vector.

all(x[-1, 1] == x[1, 1])
# [1] TRUE

If NA values are present, then this exact method does not still apply. However, it can be easily rectified by using na.omit(). For example -

## create a vector with an NA value
x2 <- c(x[, 1], NA)

## standard check returns NA
all(x2 == x2[1])
# [1] NA

## call na.omit() to remove, then compare
all(na.omit(x2) == x2[1])
# [1] TRUE

So then, with your matrix x, this last line would become

all(na.omit(x[-1, 1]) == x[1, 1])
like image 36
Rich Scriven Avatar answered Oct 04 '22 02:10

Rich Scriven