计算多列中 R 中大于 0 的值的数量 [英] counting the number of values greater than 0 in R in multiple columns

查看:19
本文介绍了计算多列中 R 中大于 0 的值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一个数据集 myDF,其中包含变量 L1、L2、L3、L4.如何获得L2、L3和L4区域大于0的观察次数?

I have a dataset myDF in R with a the variables L1,L2,L3,L4. How can I get the number of observations in L2, L3, and L4 that area greater than 0?

我想使用子集功能,我只是不知道如何谢谢!

I would like to use the subset function, I'm just not sure how Thanks!

L1     L2     L3    L4
1       1     0     2
2       1     4     1 
3       1     3     1
2       2     1     1

我希望能够创建一个函数,该函数能够计算 L2、L3 和 L4 列中大于 0 的行数.

I would like to be able to create a function that would be able to tally up the number of rows in columns L2, L3, and L4 greater than 0.

推荐答案

我们可以用

colSums(myDF[c("L2", "L3", "L4")] > 0)

这篇关于计算多列中 R 中大于 0 的值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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