【问题标题】:ddply skip if nrow = 1如果 nrow = 1,则 ddply 跳过
【发布时间】:2014-01-30 19:34:42
【问题描述】:

我有一个ddply,它遍历 IP 列表并对每个 IP 应用一个乐趣。我希望只有在nrow(ip.data) > 1 的情况下才能返回一个值。否则,我希望ddply 跳过该 IP 并继续。我该怎么做?

例如:

pd.outs <- ddply(server_ips, .(ip), function(x) get.ip.outs(x$ip, data))

nrow(ip.data) 将提供(数据)子集中的行数长度。

【问题讨论】:

    标签: r plyr skip


    【解决方案1】:

    一种方法是只返回NULL where nrow(x)==1:

    pd.outs <- ddply(server_ips, .(ip), function(x) {
      if (nrow(x) == 1) {
        return(NULL)
      }
      get.ip.outs(x$ip, data)
    })
    

    【讨论】:

    • 嗨佩顿,谢谢!你的建议奏效了。我将 return 语句添加到我的主要乐趣中,一切都很好。再次感谢。
    • 或者直接做function(x) if (nrow(x) &gt; 1) get.ip.outs(x$ip, data)
    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 2017-02-15
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多