0

If na.rm = TRUE removes participants with incomplete responses, is there a way to remove participants who "straight-line" their responses (e.g. in a Likert scale)?

Example:

How to remove participant data with responses like Participant A?

Participant A responses: 1, 1, 1, 1, 1, 1, 1

Participant B responses: 1, 6, 5, 5, 2, 3, 2

Thanks for your help :)

Edit: My data columns include Participant, A_1, A_2, A_3, A_4, B_1, B_2, B_3. Would it be possible to get unique responses across a row (a participant). The "A_1" and others are the specific questions a participant can score 1-7.

Alexander
  • 75
  • 8
  • If the data is set to numeric, you can detect cases if their variance is 0: `var(rep(1, 10))` – Phil Nov 12 '21 at 15:04

1 Answers1

2

One possible solution to your task within the tidyverse (in this case only dplyr) can be this:

library(dplyr)

# your dummy data as one df
df <- data.frame(participant = c("A", "A","A","A","A","A","A", "B", "B","B","B","B","B","B"),
                 responses = c(1, 1, 1, 1, 1, 1, 1, 1, 6, 5, 5, 2, 3, 2)) 

df %>%
    # build groupings by participant
    dplyr::group_by(participant) %>%
    # filter for those groups (participants) that have more than one unique value in the responses column
    dplyr::filter(length(unique(responses)) != 1) %>%
    # always ungroup grouped dfs/tibbles to prevent unwanted behaviour down stream
    dplyr::ungroup()

# A tibble: 7 x 2
  participant responses
  <chr>           <dbl>
1 B                   1
2 B                   6
3 B                   5
4 B                   5
5 B                   2
6 B                   3
7 B                   2

EDIT

If I understand you correctly, this solution works with the prior code, we just have to transform the data from wide, also called pivoting and implemented nicely within the package tidyr. We need some new dummy data for which the package data.table has a nice function:

# just a quick way to read tables from text to df 
df <- data.table::fread("participant, A_1, A_2, A_3, A_4, B_1, B_2, B_3
A, 1, 1, 1, 1, 1, 1, 1
B, 1, 6, 5, 5, 2, 3, 2")

# transform to long format
wdf <- df %>%  
    tidyr::pivot_longer(-participant, names_to = "questions", values_to = "responses")

wdf %>%
    # build groupings by participant
    dplyr::group_by(participant) %>%
    # filter for those groups (participants) that have more than one unique value in the responses column
    dplyr::filter(length(unique(responses)) != 1) %>%
    # always ungroup grouped dfs/tibbles to prevent unwanted behaviour down stream
    dplyr::ungroup()

# A tibble: 7 x 3
  participant questions responses
  <chr>       <chr>         <int>
1 B           A_1               1
2 B           A_2               6
3 B           A_3               5
4 B           A_4               5
5 B           B_1               2
6 B           B_2               3
7 B           B_3               2

another option is to perform row wise operations, for which we use the same dummy data as before and keep it within dplyr after that:

df <- data.table::fread("participant, A_1, A_2, A_3, A_4, B_1, B_2, B_3
A, 1, 1, 1, 1, 1, 1, 1
B, 1, 6, 5, 5, 2, 3, 2")

df %>% 
    # change the formula aplication direction from rows to columns
    dplyr::rowwise(participant) %>%
    # pperfrom the filtering similar to the prior step
    dplyr::filter(length(unique(A_1:B_3)) != 1) %>%
    # release the inverted working direction (safer to prevent from unwanted behaviour)
    dplyr::ungroup()

# A tibble: 1 x 8
  participant   A_1   A_2   A_3   A_4   B_1   B_2   B_3
  <chr>       <int> <int> <int> <int> <int> <int> <int>
1 B               1     6     5     5     2     3     2
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DPH
  • 4,244
  • 1
  • 8
  • 18
  • Thank you for your reply! :) My data columns include Participant, A_1, A_2, A_3, A_4, B_1, B_2, B_3. Would it be possible to get unique responses across a row (a participant). I'm still very new to RStudio, hopefully that makes sense. – Alexander Nov 12 '21 at 16:58
  • The "A_1" and others are the specific questions a participant can score 1-7. – Alexander Nov 12 '21 at 17:04
  • hi @Alexander please make a repoducable example that represents your data correctly (otherwise we might missunderstand eachother). Have a look here on how to do it: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – DPH Nov 12 '21 at 18:16
  • @Alexander have a look at the altered answer - possibly that is what you are looking for – DPH Nov 12 '21 at 23:51
  • Your answer worked as intended, I learned about row wise, and about filtering for more than one unique value. Your answer was clear to follow and I'm slowly learning more about r. Thank you so much for your time and help!! :) – Alexander Nov 13 '21 at 16:25