计算2列之一中存在空白的实例数+ r

发布于 2025-02-01 00:28:39 字数 272 浏览 3 评论 0原文

我需要测试一个数据框以完成记录 - 要完成记录,必须在两个列中进行条目。在示例df中,您将看到9个条目中的2个中包含一个行中的空白。

df <- data.frame(a = c(1,2,"",4,5,6,7,"",8,9),
                 b = c(9,5,2,7,5,"",3,"",6,8))

所需的输出将是2或7识别完整或相反不完整的记录数量的计数。两者都是空白的实例。

I need to test a dataframe for completion of records - for the records to be complete an entry must be made in both columns. In in the example df below you will see that 2 of the 9 entries contain blanks in one of the rows.

df <- data.frame(a = c(1,2,"",4,5,6,7,"",8,9),
                 b = c(9,5,2,7,5,"",3,"",6,8))

The desired output would be a count of 2 or 7 identifying the number of records that are complete or conversely incomplete. Instances where both are blanks would not be considered in the count.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2025-02-08 00:28:39

它可以是逻辑矩阵sum - 将'df'转换为逻辑矩阵(df!=''),获取> rowsums并检查sum(true - &gt; 1和false - &gt; 0)等于列的列数,以返回n1 n1 < /code>并从排行的总数中减去n2

n1 <- sum(rowSums(df != '') == ncol(df))
n1
[1] 7
n2 <- nrow(df) - n1
# or if there are some other cases with `NA` etc
n2 <- sum(rowSums(df =='') == 1, na.rm = TRUE)

It could be sum of logical matrix - convert the 'df' to logical matrix (df != ''), get the rowSums and check whether the sum (TRUE -> 1 and FALSE -> 0) is equal to number of columns to return n1 and subtract from the total number of rows to get n2

n1 <- sum(rowSums(df != '') == ncol(df))
n1
[1] 7
n2 <- nrow(df) - n1
# or if there are some other cases with `NA` etc
n2 <- sum(rowSums(df =='') == 1, na.rm = TRUE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文