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

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 rows had the same values. One of them was the all function, as below:

apply(meusdados,1,all)

However, I noticed that it returns% with% only for values other than% with% if there is repetition.

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

In addition, this function does not work with vectors with characters, returning all% with%.

How to correct this, so that if the identical lines return the value boolean TRUE ?

My data with the numeric 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")
    
asked by anonymous 27.10.2018 / 04:36

2 answers

1

The following function solves the problem for both numeric data ( dados1 ) and non-numeric 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
    
01.11.2018 / 23:14
1

To check if the same element is repeated in all columns, use the condition > 1 , if you want to check if an element repeats at least once in the columns use the == 3 condition. In both cases the combination of the dim() and table() functions will count the number of distinct elements per line.

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)
    
27.10.2018 / 18:45