簡體   English   中英

根據特定條件刪除參與者

[英]Remove participants based on a certain criteria

我有許多參與者及其選擇的實驗。 為簡單起見,我們假設以下內容:

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

現在,我想完全刪除某些參與者。 例如,那些在10以上做出至少一項選擇的人。因此,在上面的示例中,由於參與者2在10以上做出了一項選擇,所以我將其完全刪除:最終數據應如下所示:

      part choice
[1,]    1      6
[2,]    1      2
[3,]    1      9
[4,]    3      3
[5,]    3      6
[6,]    3      8

我該怎么做?

謝謝!

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
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

使用此代碼,您甚至不需要安裝任何軟件包。

使用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,]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM