R 根据特定标准删除参与者

R 根据特定标准删除参与者,r,filtering,subset,data-manipulation,R,Filtering,Subset,Data Manipulation,我有一个实验,有很多参与者和他们的选择。 为简单起见,我们假设如下: part<-c(1,1,1,2,2,2,3,3,3) choice<-c(6,2,9,2,3,18,3,6,8) study<-cbind(part,choice) part choice [1,] 1 6 [2,] 1 2 [3,] 1 9 [4,] 2 2 [5,] 2 3 [6,] 2

我有一个实验,有很多参与者和他们的选择。 为简单起见,我们假设如下:

part<-c(1,1,1,2,2,2,3,3,3)
choice<-c(6,2,9,2,3,18,3,6,8)
study<-cbind(part,choice)

       part choice
 [1,]    1      6
 [2,]    1      2
 [3,]    1      9
 [4,]    2      2
 [5,]    2      3
 [6,]    2     18
 [7,]    3      3
 [8,]    3      6
 [9,]    3      8
我怎么做

谢谢

库(dplyr)
library(dplyr)
 study %>% 
   group_by(part) %>% 
   filter(max(choice)<10)
# A tibble: 6 x 2
# Groups:   part [2]
   part choice
  <dbl>  <dbl>
1     1      6
2     1      2
3     1      9
4     3      3
5     3      6
6     3      8
研究%>% 按(部分)分组%>% 过滤器(最大值(可选)
使用此代码,您甚至不需要安装任何软件包。

使用R base,无需加载软件包。为了更好地了解解决方案,该示例使用变量名而不是位置

# Create object to be used in dataframe.
part   <- c(1,1,1,2,2,2,3,3,3)
choice <- c(6,2,9,2,3,18,3,6,8)
# Create dataframe.
study  <- data.frame(part, choice)

# Find rows in column [study$choice]
find_rows <- which(study$choice > 10)
# Find participant that matches [find_rows]
participant_to_be_deleted <- study[find_rows,1]

# Remove all rows that has found participant in [study$part].
result <- study[study$part!=participant_to_be_deleted,]
#创建要在数据帧中使用的对象。

使用
dplyr
您可以执行
study%%>%groupby(part)%%>%filter(all(choice<10))
removed = which(study[ , 2]>10);
study = study[!(study[ , 1] %in% study[removed, 1]), ];

study
     part choice
[1,]    1      6
[2,]    1      2
[3,]    1      9
[4,]    3      3
[5,]    3      6
[6,]    3      8
# Create object to be used in dataframe.
part   <- c(1,1,1,2,2,2,3,3,3)
choice <- c(6,2,9,2,3,18,3,6,8)
# Create dataframe.
study  <- data.frame(part, choice)

# Find rows in column [study$choice]
find_rows <- which(study$choice > 10)
# Find participant that matches [find_rows]
participant_to_be_deleted <- study[find_rows,1]

# Remove all rows that has found participant in [study$part].
result <- study[study$part!=participant_to_be_deleted,]