Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-21T09:49:25.477Z
Answers.LastActivityDate: 2018-05-22T10:36:30.467Z
Answers.LastEditDate: 2018-05-22T10:36:30.467Z
Answers.UserDisplayName: zx8754
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-18T17:00:13.813Z
Answers.Text: 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.
Answers.UserDisplayName: heathobrien
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-18T17:08:37.570Z
Answers.Text: @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.
Answers.UserDisplayName: benn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-22T10:14:18.230Z
Answers.Text: `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.
Answers.UserDisplayName: Konrad Rudolph
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-22T10:16:20.013Z
Answers.Text: @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.
Answers.UserDisplayName: benn
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-22T10:24:39.200Z
Answers.Text: @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*.
Answers.UserDisplayName: Konrad Rudolph
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-22T10:51:28.400Z
Answers.Text: @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.
Answers.UserDisplayName: zx8754
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-18T16:44:49.110Z
Answers.LastActivityDate: 2018-05-22T13:42:29.970Z
Answers.LastEditDate: 2018-05-22T13:42:29.970Z
Answers.UserDisplayName: benn
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-18T16:40:16.973Z
Comments.Text: 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))`
Comments.UserDisplayName: heathobrien
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-18T16:53:32.960Z
Comments.Text: 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
Comments.UserDisplayName: Sofia
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-18T17:05:02.810Z
Comments.Text: 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
Comments.UserDisplayName: heathobrien
Tags: <r>
Title: How to compare the contents of a column of the same data table
ThreadText: # 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:
  
  [![enter image description here](https://i.sstatic.net/2rkaG.png)](https://i.sstatic.net/2rkaG.png)
  
  and I want to get the rows that are equal to the first three columns, like this:
  
  [![enter image description here](https://i.sstatic.net/1PBVw.png)](https://i.sstatic.net/1PBVw.png)
  
  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**: @username_1, 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**: @username_4, 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.  
  
  ---
  
  