How to return a logical value when the lines have identical values in the R software?

Asked

Viewed 70 times

2

I have a database that contains 3 variables: var1, var2 and var 3. I tried to use some functions in R that would return me a logical value if any of the lines had the same values. One of them was the function all, as below:

apply(meusdados,1,all)

However, I noticed that she returns TRUE only for values other than 0 if there is repetition.

[1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE

Aléḿ of this, this function does not work with vectors with characters, returning everything NA.

How to fix this so that if identical lines return the value Boolean TRUE?

My data with numerical variables are these:

structure(list(var1 = c(1, 0, 1, 0, 1, 0, 1, 0, 0, 1), var2 = c(0, 
0, 0, 1, 1, 1, 0, 0, 1, 1), var3 = c(1, 0, 1, 1, 1, 1, 0, 0, 
0, 0)), .Names = c("var1", "var2", "var3"), class = "data.frame",  row.names = c(NA, 
-10L))

With the strings, these are:

structure(list(var1 = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 1L, 2L), .Label = c("compras", "opfin"), class = "factor"), 
var2 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L
), .Label = c("compras", "opfin"), class = "factor"), var3 = structure(c(2L, 
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("compras", 
"opfin"), class = "factor")), .Names = c("var1", "var2", 
"var3"), row.names = c(NA, -10L), class = "data.frame")

2 answers

1


The following function solves the problem both for numerical data (dados1) as for non-numerical data, for example class character (dados2).

todosIguais <- function(DF){
  apply(DF, 1, function(x) length(unique(x)) == 1)
}

todosIguais(dados1)
#[1] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE

todosIguais(dados2)
#[1] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE

1

To check if the same element repeats in all columns use the condition > 1, if you want to check if an element repeats at least once in the columns use the condition == 3. In both cases the combination of functions dim() and table() account for the number of separate items per row.

chec_rep_rows = function(data){
  data = as.matrix(data)
  for (i in 1:nrow(data))
    print(ifelse(dim(table(data[i,])) > 1, FALSE, TRUE))
}

chec_rep_rows(data)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.