Home > other >  How to use R language to determine whether a line is the same character
How to use R language to determine whether a line is the same character

Time:11-04

I have a data.frame like the following:

  V1 V2 V3 V4 V5
1 a  a  b  a  a
2 a  a  a 
3 b  b  b  b
4 a  c  d

I want to keep the lines with the same character in one line (in my example, lines 2 and 3), is there any function that can help me achieve this requirement?

CodePudding user response:

Here is a base R option using apply

df[apply(df, 1, function(x) length(unique(x[x != ''])) == 1), ]
#V1 V2 V3 V4 V5
#2  a  a  a      
#3  b  b  b  b   

Explanation: length(unique(x[x != '')) == 1 checks if non-empty elements of a vector x contain only a single unique element. apply with MARGIN = 1 means that we loop through the rows of the data.frame.


Sample data

df <- read.table(text = "  V1 V2 V3 V4 V5
1 a  a  b  a  a
2 a  a  a  '' ''
3 b  b  b  b ''
4 a  c  d '' ''", header = T)
  •  Tags:  
  • r
  • Related