使用library(dplyr),filter()

时间:2018-10-02 22:53:28

标签: r

我正在尝试根据第一列中的两个条件来过滤data.frame。这是我的原始数据表:

df=rbind(l2a,l3a,l5b);df

给出以下输出:

line immci log10ci clutch flask num 1 L2a 1.9928984 0.299485154 g xiii 1 2 L2a 2.0844090 0.318982944 g xiii 2 3 L2a 1.5264288 0.183676540 g xiii 3 4 L2a 0.9567779 -0.019188860 g xiii 4 5 L2a 1.6149111 0.208148610 g xiii 5 6 L2a 1.8939134 0.277360108 g xiii 6 7 L2a 1.2506454 0.097134194 g xiii 7 8 L2a 1.9859786 0.297974563 g xiii 8 9 L2a 1.7384858 0.240171153 g xiii 9 10 L2a 1.4590863 0.164080984 g xiii 10 11 L2a 0.4699034 -0.327991426 h xv 11 12 L2a 1.5271860 0.183891935 h xv 12 13 L2a 0.6774155 -0.169144894 h xv 13 14 L2a 1.6782264 0.224850542 h xv 14 15 L2a 1.0197556 0.008496103 h xv 15 16 L2a 1.3849784 0.141443000 h xv 16 17 L2a 1.6152929 0.208251280 h xv 17 18 L2a 1.5214085 0.182245839 h xv 18 19 L2a 1.2306993 0.090151968 h xv 19 20 L2a 2.4760294 0.393755793 h xv 20 21 L2a 0.2660571 -0.575025122 i xvii 21 22 L2a 1.8511805 0.267448775 i xvii 22 23 L2a 0.9735272 -0.011651922 i xvii 23 24 L2a 1.3016773 0.114503326 i xvii 24 25 L2a 1.0381621 0.016265152 i xvii 25 26 L2a 2.2904514 0.359921077 i xvii 26 27 L2a 1.7535583 0.243920200 i xvii 27 28 L2a 1.9884998 0.298525551 i xvii 28 29 L2a 2.8867197 0.460404617 i xvii 29 30 L2a 1.1027367 0.042471840 i xvii 30 31 L3a 2.7755014 0.443341458 a i 1 32 L3a 3.3246253 0.521742701 a i 2 33 L3a 2.6471891 0.422784960 a i 3 34 L3a 3.1020086 0.491643000 a i 4 35 L3a 3.4601116 0.539090102 a i 5 36 L3a 2.2464903 0.351504556 a i 6 37 L3a 2.1724638 0.336952556 a i 7 38 L3a 2.2577228 0.353670618 a i 8 39 L3a 3.2253183 0.508572578 a i 9 40 L3a 3.0630622 0.486155818 a i 10 41 L3a 2.3777851 0.376172593 b ii 11 42 L3a 0.8454347 -0.072919938 b ii 12 43 L3a 1.3209917 0.120900088 b ii 13 44 L3a 0.8454347 -0.072919938 b ii 14 45 L3a 0.8454347 -0.072919938 b ii 15 46 L3a 1.0567934 0.023990075 b ii 16 47 L3a 1.4090578 0.148928811 b ii 17 48 L3a 1.7172892 0.234843440 b ii 18 49 L3a 6.3407601 0.802141325 b ii 19 50 L3a 1.0567934 0.023990075 b ii 20 51 L3a 1.3612015 0.133922403 c iii 21 52 L3a 2.0879623 0.319722648 c iii 22 53 L3a 1.6070421 0.206027251 c iii 23 54 L3a 1.6739557 0.223743962 c iii 24 55 L3a 2.7192951 0.434456346 c iii 25 56 L3a 2.0894176 0.320025246 c iii 26 57 L3a 1.6089163 0.206533449 c iii 27 58 L3a 2.3979239 0.379835395 c iii 28 59 L3a 1.5228053 0.182644373 c iii 29 60 L3a 1.2916652 0.111149956 c iii 30 61 L5b 4.8973579 0.689961841 g xiv 1 62 L5b 5.8659429 0.768337829 g xiv 2 63 L5b 1.3256006 0.122412700 g xiv 3 64 L5b 2.1995151 0.342326950 g xiv 4 65 L5b 3.4130407 0.533141466 g xiv 5 66 L5b 4.1081677 0.613648164 g xiv 6 67 L5b 4.4651811 0.649839073 g xiv 7 68 L5b 1.6466150 0.216592073 g xiv 8 69 L5b 1.7674675 0.247351437 g xiv 9 70 L5b 1.7054511 0.231839271 g xiv 10 71 L5b 17.3092190 1.238277472 h xvi 11 72 L5b 5.8925001 0.770299597 h xvi 12 73 L5b 7.6929862 0.886094954 h xvi 13 74 L5b 7.8566668 0.895238334 h xvi 14 75 L5b 11.1957502 1.049053198 h xvi 15 76 L5b 11.0975418 1.045226790 h xvi 16 77 L5b 11.6510797 1.066366173 h xvi 17 78 L5b 5.1068334 0.708151690 h xvi 18 79 L5b 6.1687110 0.790194426 h xvi 19 80 L5b 5.2295938 0.718467959 h xvi 20 81 L5b 4.9864112 0.697788089 i xviii 21 82 L5b 1.4543699 0.162674888 i xviii 22 83 L5b 4.8223845 0.683261836 i xviii 23 84 L5b 4.4953252 0.652761120 i xviii 24 85 L5b 11.8773545 1.074719717 i xviii 25 86 L5b 5.7265816 0.757895455 i xviii 26 87 L5b 3.7722720 0.576603002 i xviii 27 88 L5b 3.9038351 0.591491463 i xviii 28 89 L5b 2.7968653 0.446671544 i xviii 29 90 L5b 2.4542493 0.389918669 i xviii 30

我想产生一个仅包含“ L3a”和“ L5b”行的数据帧。到目前为止,我已经尝试了以下方法:

filter(imm,line==c('L3a','L5b'))

产生:

line immci log10ci clutch flask num 1 L3a 2.7755014 0.44334146 a i 1 2 L3a 2.6471891 0.42278496 a i 3 3 L3a 3.4601116 0.53909010 a i 5 4 L3a 2.1724638 0.33695256 a i 7 5 L3a 3.2253183 0.50857258 a i 9 6 L3a 2.3777851 0.37617259 b ii 11 7 L3a 1.3209917 0.12090009 b ii 13 8 L3a 0.8454347 -0.07291994 b ii 15 9 L3a 1.4090578 0.14892881 b ii 17 10 L3a 6.3407601 0.80214133 b ii 19 11 L3a 1.3612015 0.13392240 c iii 21 12 L3a 1.6070421 0.20602725 c iii 23 13 L3a 2.7192951 0.43445635 c iii 25 14 L3a 1.6089163 0.20653345 c iii 27 15 L3a 1.5228053 0.18264437 c iii 29 16 L5b 5.8659429 0.76833783 g xiv 2 17 L5b 2.1995151 0.34232695 g xiv 4 18 L5b 4.1081677 0.61364816 g xiv 6 19 L5b 1.6466150 0.21659207 g xiv 8 20 L5b 1.7054511 0.23183927 g xiv 10 21 L5b 5.8925001 0.77029960 h xvi 12 22 L5b 7.8566668 0.89523833 h xvi 14 23 L5b 11.0975418 1.04522679 h xvi 16 24 L5b 5.1068334 0.70815169 h xvi 18 25 L5b 5.2295938 0.71846796 h xvi 20 26 L5b 1.4543699 0.16267489 i xviii 22 27 L5b 4.4953252 0.65276112 i xviii 24 28 L5b 5.7265816 0.75789545 i xviii 26 29 L5b 3.9038351 0.59149146 i xviii 28 30 L5b 2.4542493 0.38991867 i xviii 30

如您所见,它仅捕获我真正想要的每隔一行数据,而我不知道为什么。我也尝试过使用all(),any(),&和|。谁能提供一些建议?谢谢

1 个答案:

答案 0 :(得分:0)

您不需要将filter用作

df[df$line %in% c("L3a", "L5b"), ]

可以解决问题。