R - 从同一数据帧中排除具有特定数量的零的行

时间:2017-03-25 03:29:21

标签: r dataframe

假设我有一个如下所示的数据框:

private void MoveItem(List<Page> source, int fromOrder, int toOrder)
{
  if (fromOrder == toOrder)
  { return; }

  if (fromOrder < toOrder)
  {
    foreach(Page page in source)
    {
      bool match = page.Order == fromOrder;
      bool between = fromOrder < page.Order && page.Order <= toOrder;
      page.Order = match ? toOrder :
         between ? page.Order - 1 :
         page.Order;
    }
  }
  else
  {
    foreach(Page page in source)
    {
      bool match = page.Order == fromOrder;
      bool between = toOrder <= page.Order && page.Order < fromOrder;
      page.Order = match ? toOrder :
         between ? page.Order + 1 :
         page.Order;
    }
  }
}

首先,我想评估哪些样本( A1 A2 A3 B1 B2 B3 C1 C2 C3 X 00 10 00 10 00 11 00 00 00 Y 00 00 00 20 00 00 00 00 00 Z 00 00 00 29 30 00 50 42 38 )有多个不同于零的列。例如,对于X,A将为假,B将为真,C将为假。

然后,我想保留至少有一个样本(A, B, C)的行为早期代码保留TRUE。这意味着我的新数据框只有X行(样本B为真)和Z(样本B和C为真)。

如果可能,这可以单独完成,也可以在一行代码中完成。只要最终结果相同(Y排除的行)

1 个答案:

答案 0 :(得分:0)

这会将所有以相似字母开头并以数字尾随的列分组。

# data set used
df1 <- data.frame(A1=rep(0,3), A2=c(10,0,0), A3=rep(0,3), B1=c(10,20,29), B2=c(0,0,30), B3=c(11,0,0), C1=c(0,0,50), C2=c(0,0,42), C3=c(0,0,38))
rownames(df1) <- c("X", "Y", "Z")

# define column groups and count values greater than zero
col.samp <- unique(gsub("[0-9]", "", colnames(df1)))
m1 <- sapply(col.samp, function(x) rowSums(df1[grep(paste0("^", x), colnames(df1))] > 0))

df1[rowSums(m1 > 0) > 1, ]

  A1 A2 A3 B1 B2 B3 C1 C2 C3
X  0 10  0 10  0 11  0  0  0
Z  0  0  0 29 30  0 50 42 38