计算数据帧中每行的 NA [英] Count NAs per row in dataframe

查看:18
本文介绍了计算数据帧中每行的 NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含批次 ID 的数据框以及对每个批次执行的六次测试的结果.数据如下所示:

I've got dataframe that has batch ID and the results of six tests performed on each batch. The data looks like this:

batch_id  test1  test2  test3  test4  test5  test6
001       0.121     NA  0.340  0.877  0.417  0.662
002       0.229  0.108     NA  0.638     NA  0.574

(这个数据框中有几百行,每个batch_id只有一行)

(there are a few hundred rows in this dataframe, only one row per batch_id)

我正在寻找一种方法来计算每个 batch_id(对于每一行)有多少个 NA.我觉得这最多应该可以用几行 R 代码来实现,但是我在实际编码时遇到了麻烦.有什么想法吗?

I'm looking for a way to count how many NAs there are for each batch_id (for each row). I feel like this should be do-able with a few lines of R code at the most, but I'm having trouble actually coding it. Any ideas?

推荐答案

您可以向数据框中添加一个新列,其中包含每个 batch_idNA 值的数量:

You could add a new column to your data frame containing the number of NA values per batch_id:

df$na_count <- apply(df, 1, function(x) sum(is.na(x)))

这篇关于计算数据帧中每行的 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆