简体   繁体   English

如何计算数据框列中包含某些数字的单元格

[英]How to count cells containing certain digits in a column of a data frame

I need to find the number of elements of a vector containing two precise digits, let's say 00.我需要找到包含两个精确数字的向量的元素数,比如 00。

I thought about using logical indexing but the way R reacts makes me think it is looking for the exact value 00, while I want to know how many times 00 can be found INSIDE the numbers contained in the vector.我考虑过使用逻辑索引,但 R 的反应方式让我认为它正在寻找确切的值 00,而我想知道在向量中包含的数字中可以找到多少次 00。

Is there a function that does that or some kind of logical indexing I could use?是否有一个函数可以做到这一点,或者我可以使用某种逻辑索引?

If I understood well, one work around is to use str_count from stringr as follows:如果我理解得很好,一种解决方法是使用str_countstringr ,如下所示:

 df<-data.frame(A=c("00","89","00"))

Sums<-str_count(df$A,"00")
sum(Sums>0)

Using base R and adapting a little of nicola's answer:使用基础 R 并调整一些 nicola 的答案:

sum(grepl("00",df$A)==T)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM