Home > Blockchain >  How can I remove rows with same elements in all columns of a dataframe?
How can I remove rows with same elements in all columns of a dataframe?

Time:04-03

I have a dataframe with the following elements:

> x[3536:3540,]
     V1 V2
3536  2  6
3537 13  6
3538  9  6
3539  6  6
3540  2  2

I want to remove rows with the same elements in all columns. My desired result is as follows:

> x[3536:3540,]
     V1 V2
3536  2  6
3537 13  6
3538  9  6

I tried this:

x<-x[,1] != x[,2]

But I get only boolean values for each row, not matrix with rows with non-same elements in columns:

> x
   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [15] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [29] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [43] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [57] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [71] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [99] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [113] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE

Any help would be greatly appreciated.

CodePudding user response:

You need to subset/filter:

Base R:

x_new <- x[x$V1 != x$V2,]

dplyr:

library(dplyr)
x_new <- x %>%
           filter(V1 != V2)

Result:

x_new
  V1 V2
2  1  2
3  1  3

Data:

x <- data.frame(
  V1 = c(1,1,1,1),
  V2 = c(1,2,3,1)
)

CodePudding user response:

The below is assuming you want to subset within specific rows as per original post.

library(data.table)

setDT(df)

df <- df[3536:3540][V1 != V2]
  • Related