我正在尝试计算排序表的唯一行值。所以,如果我有一个像这样的表:
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
来获取唯一的名称,但是我无法将这些名称放在一起以获得最终的计数列。
答案 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])))))
lapply
函数weight < n
feed[weight < n]
unique
和length
的唯一身份。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]))