选择具有特定值的行下方的行

时间:2018-04-23 21:06:23

标签: r select filter dplyr row

我有一个包含许多行和列的数据集,我想过滤我的数据集,只选择行下面的行,其值为“Loss”或“MissedWin”

我该怎么做?

例如,我有一个如下所示的数据集:

   ParticipantNumber Primary_Dx     Cue TrialCategory Target.RT
1              21054    Healthy Control       Neutral       203
2              21054    Healthy   Lose2   AvoidedLoss       186
3              21054    Healthy Control       Neutral       205
4              21054    Healthy    Win2           Win       222
5              21054    Healthy    Win2           Win       198
6              21054    Healthy    Win2           Win       271
7              21054    Healthy   Lose2   AvoidedLoss       259
8              21054    Healthy   Lose2   AvoidedLoss       223
9              21054    Healthy   Lose2   AvoidedLoss       257
10             21054    Healthy   Lose2   AvoidedLoss       204
11             21054    Healthy Control       Neutral       207
12             21054    Healthy   Lose2   AvoidedLoss       193
13             21054    Healthy Control       Neutral         0
14             21054    Healthy Control       Neutral       217
15             21054    Healthy    Win2           Win       208
16             21054    Healthy Control       Neutral       248
17             21054    Healthy    Win2           Win       184
18             21054    Healthy   Lose2   AvoidedLoss       217
19             21054    Healthy   Lose2          Loss       296
20             21054    Healthy Control       Neutral       296
21             21054    Healthy Control       Neutral         0
22             21054    Healthy Control       Neutral         0
23             21054    Healthy Control       Neutral       202
24             21054    Healthy Control       Neutral         0
25             21054    Healthy    Win2     MissedWin         0
26             21054    Healthy    Win2     MissedWin       207
27             21054    Healthy    Win2     MissedWin         0
28             21054    Healthy   Lose2          Loss         0
29             21054    Healthy Control       Neutral       201
30             21054    Healthy   Lose2          Loss         0
31             21054    Healthy    Win2     MissedWin         0
32             21054    Healthy   Lose2          Loss       233
33             21054    Healthy   Lose2          Loss       241
34             21054    Healthy    Win2           Win       223
35             21054    Healthy   Lose2          Loss         0
36             21054    Healthy   Lose2          Loss         0
37             21054    Healthy Control       Neutral       192
38             21054    Healthy    Win2           Win       211
39             21054    Healthy   Lose2   AvoidedLoss       208
40             21054    Healthy    Win2           Win       166
41             21054    Healthy    Win2     MissedWin         0
42             21054    Healthy    Win2           Win       191
43             21054    Healthy Control       Neutral       199
44             21054    Healthy    Win2           Win       205
45             21054    Healthy    Win2           Win       108
46             21054    Healthy Control       Neutral       174
47             21054    Healthy   Lose2   AvoidedLoss       218
48             21054    Healthy   Lose2   AvoidedLoss       227
49             21054    Healthy   Lose2   AvoidedLoss       236
50             21054    Healthy Control       Neutral       219

我希望它看起来像这样:

        ParticipantNumber Primary_Dx     Cue TrialCategory Target.RT

20             21054    Healthy Control       Neutral       296
29             21054    Healthy Control       Neutral       201
34             21054    Healthy    Win2           Win       223
37             21054    Healthy Control       Neutral       192
42             21054    Healthy    Win2           Win       191

1 个答案:

答案 0 :(得分:1)

这是一个刺:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

    <p id = "myString">Hello World! I am Julio!</p>

    <p id = "mRCharacter"></p>

    <script>
        var string = document.getElementById("myString").innerHTML;

        var mRCharater = mostRepetedCharacter(string);
        document.getElementById("mRCharacter").innerHTML = mRCharater;
        console.log(mRCharater);

        function mostRepetedCharacter(string){
            var mRCharater = "";
            var strLength = string.length;
            var i = 0;
            var count = 0;
            var max = 0;
            var rest = strLength - 1;
            while (i < strLength){
                var j = i + 1;
                while (j <= rest){
                    if (string[i] === string[j]){
                        count++;
                    }
                    if (count > max){
                        max = count;
                        mRCharater = string[i];
                    }
                    j++;
                }
                i++;
                count = 0;
            }
            return mRCharater;
        }

    </script>

</body>
</html>

    enter code here

让我们说Datsun基于其低HP而感兴趣,我想列出它下面的所有内容。

head(mtcars)
#                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

你可以看到字面意思:

library(dplyr)
head(mtcars) %>%
  filter(cumany(hp < 100))
#    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# 2 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# 3 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# 4 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

具体而言:

cumany(c(F,F,T,F,T,F,T))
# [1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE