我如何仅将具有列值的行放在R中的向量中?

时间:2015-12-21 20:56:25

标签: r

我有一个data.frame和一个向量。我想只输出数据框中的行,这些行具有与向量v相同的列中的值。

例如:

    private void GerarTorrent(string magnet)
    {
        MonoTorrent.Client.TorrentManager b = new MonoTorrent.Client.TorrentManager(new MonoTorrent.MagnetLink(magnet), "D:\\A", new MonoTorrent.Client.TorrentSettings(), "D:\\TorrentSave");
        MonoTorrent.Client.EngineSettings engineSettings = new MonoTorrent.Client.EngineSettings();
        MonoTorrent.Client.ClientEngine clientEngine = new MonoTorrent.Client.ClientEngine(engineSettings);
        clientEngine.Register(b);
        clientEngine.StartAll();
        b.Start();
    }

df =

    private void GerarTorrent(string hash)
    {
        MonoTorrent.Client.TorrentManager b = new MonoTorrent.Client.TorrentManager(MonoTorrent.InfoHash.FromHex(hash), "D:\\A", new MonoTorrent.Client.TorrentSettings(), "D:\\TorrentSave", new List<List<string>>());
        MonoTorrent.Client.EngineSettings engineSettings = new MonoTorrent.Client.EngineSettings();
        MonoTorrent.Client.ClientEngine clientEngine = new MonoTorrent.Client.ClientEngine(engineSettings);
        clientEngine.Register(b);
        clientEngine.StartAll();
        b.Start();
    }

我想要做的是,如果df $ b中有任何v值,则输出该行。基本上如果df $ b [i]不在v中,则删除i = 1的行:nrows(df)

输出应该是

v = (1,2,3,4,5)

因为2,4和1在v。

1 个答案:

答案 0 :(得分:3)

您应该使用%in%运算符。

v <- c(1, 2, 3, 4, 5)
df <- read.table(text =
"     A    B
1    a    2
2    b    6
3    c    4
4    d    1
5    e    8", header = TRUE)

out <- df[df$B %in% v, ]

这给出了:

  A B
1 a 2
3 c 4
4 d 1