<p>Loop through 3 columns by row using <em>apply</em>, then convert <em>toupper</em> case (as we want <code>a</code> same as <code>A</code>), get <em>unique</em>, and get <em>lengths</em>, if length is 1 then all 3 values are equal:</p>

<pre><code>df1[ lengths(
  apply(df1[, 1:3], 1, function(i) unique(toupper(i)))
  ) == 1, ]
</code></pre>

<p>This would scale better if we want to compare 4, 10, n(?) columns.</p>

CC BY-SA 4.0
2018-05-21T09:49:25.477Z
2018-05-22T10:36:30.467Z
2018-05-22T10:36:30.467Z
zx8754
<p>No need to use <em>dplyr</em>, we can do this using <em>base</em>, see below example: </p>

<pre><code># reproducible example data
df1 &lt;- data.frame(
  t(data.frame(
    one = c("A", "A", "A", "C", "C", "C"),
    two = c("A", "A", "A", "A", "A", "C"), 
    thr = c("A", "A", "A", "A", "A", "A"), 
    fou = c("C", "A", "A", "A", "A", "A"))), 
  stringsAsFactors = FALSE)

df1
#     X1 X2 X3 X4 X5 X6
# one  A  A  A  C  C  C
# two  A  A  A  A  A  C
# thr  A  A  A  A  A  A
# fou  C  A  A  A  A  A

# solution, subset using logical index:
selection &lt;- df1[df1[, 1] == df1[, 2] &amp; df1[, 1] == df1[, 3], ]

selection
#     X1 X2 X3 X4 X5 X6
# one  A  A  A  C  C  C
# two  A  A  A  A  A  C
# thr  A  A  A  A  A  A
</code></pre>

CC BY-SA 4.0
2018-05-18T17:00:13.813Z
To be fair, you don't really need dplyr for anything. I just think it's easier to wrap your head around `filter` then a command that repeats the name of the data frame 5 times, along with 5 sets of square brackets and 5 commas. Personal preference though I guess.
heathobrien
CC BY-SA 4.0
2018-05-18T17:08:37.570Z
@heathobrien, you are right dplyr is a great tool, and I think for some difficult tasks even necessary. For row selection with conditional statements I am used to select this (old fashion) way. But with dplyr would certainly be good as well.
benn
CC BY-SA 4.0
2018-05-22T10:14:18.230Z
`t(data.frame(…))` is an odd pattern, since `t` always converts data.frames to matrices, and assigning the result to a variable called `df` is actively misleading.
Konrad Rudolph
CC BY-SA 4.0
2018-05-22T10:16:20.013Z
@KonradRudolph, that was to make a workable example. I understand that you don't like my style (also from previous comments), feel free to put your own answer.
benn
CC BY-SA 4.0
2018-05-22T10:24:39.200Z
@b.nota Actually I have no problem with the style of this answer, I think it actually works well otherwise. But, to clarify, my previous comment *wasn’t about style*.
Konrad Rudolph
CC BY-SA 4.0
2018-05-22T10:51:28.400Z
@b.nota hope you don't mind the edit. Generally I avoid function names (`df`). And converted your matrix to a dataframe by wrapping it with another dataframe. Your solution works just fine.
zx8754
CC BY-SA 4.0
2018-05-18T16:44:49.110Z
2018-05-22T13:42:29.970Z
2018-05-22T13:42:29.970Z
benn
<p>I have this table: </p>

<p><a href="https://i.sstatic.net/2rkaG.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2rkaG.png" alt="enter image description here"></a></p>

<p>and I want to get the rows that are equal to the first three columns, like this: </p>

<p><a href="https://i.sstatic.net/1PBVw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1PBVw.png" alt="enter image description here"></a></p>

<p>I've tried these functions, but when I get the index of the lines, r doesn't give the output that I want:</p>

<pre><code>df$obj&lt;-sapply(c("sample1", "sample2", "sample3"), function (comparison) {
    df$sample1 == df[comparison]
})
</code></pre>

CC BY-SA 4.0
2018-05-18T16:40:16.973Z
following on from my `dplyr` suggestion on your previous question: `filter(df, sample1 == sample2 & sample1 == sample3)` Note that `a` and `A` are not the same. If you want a case-insensitive matching, use `filter(df, toupper(sample1) == toupper(sample2) & toupper(sample1) == toupper(sample3))`
heathobrien
CC BY-SA 4.0
2018-05-18T16:53:32.960Z
but in that way you only get comparison between two columns, you see? you can have the same result between sample 1 and sample 2 , but when you are going to compare sample 1 and sample 3, the result can be different than the previous one
Sofia
CC BY-SA 4.0
2018-05-18T17:05:02.810Z
I'm not sure what you mean. If sample1 == sample2 and sample1 == sample3, then sample2 has to be equal to sample3. Unless I'm misunderstanding your goal. My code will turn your example input into your desired output though
heathobrien
<r>
How to compare the contents of a column of the same data table
# How to compare the contents of a column of the same data table

(Asked by: username_0 on 2018-05-18)

I have this table: 

and I want to get the rows that are equal to the first three columns, like this: 

I've tried these functions, but when I get the index of the lines, r doesn't give the output that I want:
df$obj<-sapply(c("sample1", "sample2", "sample3"), function (comparison) {
    df$sample1 == df[comparison]
})

**Tags:** r
> **Comments:**
> **username_1**: following on from my `dplyr` suggestion on your previous question: `filter(df, sample1 == sample2 & sample1 == sample3)` Note that `a` and `A` are not the same. If you want a case-insensitive matching, use `filter(df, toupper(sample1) == toupper(sample2) & toupper(sample1) == toupper(sample3))`  
> **username_0**: but in that way you only get comparison between two columns, you see? you can have the same result between sample 1 and sample 2 , but when you are going to compare sample 1 and sample 3, the result can be different than the previous one  
> **username_1**: I'm not sure what you mean. If sample1 == sample2 and sample1 == sample3, then sample2 has to be equal to sample3. Unless I'm misunderstanding your goal. My code will turn your example input into your desired output though  

---

## 2 answers

### Answer by username_2 on 2018-05-21. Score: 4

Loop through 3 columns by row using apply, then convert toupper case (as we want a same as A), get unique, and get lengths, if length is 1 then all 3 values are equal:
df1[ lengths(
  apply(df1[, 1:3], 1, function(i) unique(toupper(i)))
  ) == 1, ]

This would scale better if we want to compare 4, 10, n(?) columns.


---

### Answer by username_3 on 2018-05-18. Score: 3

No need to use dplyr, we can do this using base, see below example: 
# reproducible example data
df1 <- data.frame(
  t(data.frame(
    one = c("A", "A", "A", "C", "C", "C"),
    two = c("A", "A", "A", "A", "A", "C"), 
    thr = c("A", "A", "A", "A", "A", "A"), 
    fou = c("C", "A", "A", "A", "A", "A"))), 
  stringsAsFactors = FALSE)

df1
#     X1 X2 X3 X4 X5 X6
# one  A  A  A  C  C  C
# two  A  A  A  A  A  C
# thr  A  A  A  A  A  A
# fou  C  A  A  A  A  A

# solution, subset using logical index:
selection <- df1[df1[, 1] == df1[, 2] & df1[, 1] == df1[, 3], ]

selection
#     X1 X2 X3 X4 X5 X6
# one  A  A  A  C  C  C
# two  A  A  A  A  A  C
# thr  A  A  A  A  A  A

> **Comments:**
> **username_1**: To be fair, you don't really need dplyr for anything. I just think it's easier to wrap your head around `filter` then a command that repeats the name of the data frame 5 times, along with 5 sets of square brackets and 5 commas. Personal preference though I guess.  
> **username_3**: @heathobrien, you are right dplyr is a great tool, and I think for some difficult tasks even necessary. For row selection with conditional statements I am used to select this (old fashion) way. But with dplyr would certainly be good as well.  
> **username_4**: `t(data.frame(…))` is an odd pattern, since `t` always converts data.frames to matrices, and assigning the result to a variable called `df` is actively misleading.  
> **username_3**: @KonradRudolph, that was to make a workable example. I understand that you don't like my style (also from previous comments), feel free to put your own answer.  
> **username_4**: @b.nota Actually I have no problem with the style of this answer, I think it actually works well otherwise. But, to clarify, my previous comment *wasn’t about style*.  
> **username_2**: @b.nota hope you don't mind the edit. Generally I avoid function names (`df`). And converted your matrix to a dataframe by wrapping it with another dataframe. Your solution works just fine.  

---

