计算已排序表中先前行的唯一值

时间:2016-06-28 00:14:29

标签: r

我正在尝试计算排序表的唯一行值。所以,如果我有一个像这样的表:

 data('chickwts')
 chickwts[order(chickwts$weight),]

我希望能够检索前面行的唯一Feed的总数。因此,如果我想weight < 150作为我的截止点,我会得到feed count = 2。理想情况下,我可以生成一个列,该列也可以在整个行中跟踪此计数,并在此情况下将此数字与weight进行对比。

我知道我可以用grepl预先选择/子集:

chickwts$seed=ifelse(grepl("seed",chickwts$feed),TRUE,FALSE)*1
chickwts[order(chickwts$weight), ]

我知道我可以使用unique来获取唯一的名称,但是我无法将这些名称放在一起以获得最终的计数列。

2 个答案:

答案 0 :(得分:2)

data("chickwts")
chickwts <-  chickwts[order(chickwts[,"weight"]),]
chickwts[,"unique.feed"] <- unlist(lapply(chickwts[,"weight"], function(n) with(chickwts, length(unique(feed[weight < n])))))
  1. 使用lapply函数
  2. 中的所有权重
  3. 检查哪些权重小于当前行weight < n
  4. 的权重
  5. 获取相应的权重Feed少于当前行feed[weight < n]
  6. 的权重
  7. 获取唯一Feed,并计算uniquelength的唯一身份。
  8. unlist他们因为我们想要一个矢量。

答案 1 :(得分:1)

data("chickwts")    
chickwts <- chickwts[order(chickwts$weight),]

# Using < 150 as a cutoff
cat("if you meant 1 column giving the count to all rows, based on < 150")
chickwts$count_less_than_150 <- length(unique(chickwts$feed[chickwts$weight < 150]))

cat("if you meant 2 columns giving the count to all rows, based on < 150 or > 150")
chickwts$count_lt_150 <- length(unique(chickwts$feed[chickwts$weight < 150]))
chickwts$count_ge_150 <- length(unique(chickwts$feed[chickwts$weight >= 150]))

cat("if you meant 1 column giving the count to all rows, based on < 150 or >= 150")
chickwts$count <- NA
chickwts$count[chickwts$weight < 150]  <- length(unique(chickwts$feed[chickwts$weight < 150]))
chickwts$count[chickwts$weight >= 150] <- length(unique(chickwts$feed[chickwts$weight >= 150]))