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