如何找到包含大于或等于给定数字的值的R矩阵的列数?

发布于 2025-02-11 12:52:47 字数 101 浏览 3 评论 0原文

我有一个带有52列的矩阵和5,000行。 想找到包含一个小于或等于值的值的列数(例如,52个中的多少列包含小于或等于10的数字)

我 做这项工作的方法。

谢谢!

I have a matrix with 52 columns, and 5,000 rows. I want to find the number of columns that contain a value less than or equal to a value (for example, how many columns out of 52 contain a number less than or equal to 10)

I was trying rowSum but I cannot remember / find a way to make this work.

Thanks!

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

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

发布评论

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

评论(3

玩心态 2025-02-18 12:52:47

一个可能的解决方案:

m <- matrix(1:9, 3, 3)

sum(colSums(m <= 5) != 0)

#> [1] 2

A possible solution:

m <- matrix(1:9, 3, 3)

sum(colSums(m <= 5) != 0)

#> [1] 2
煮茶煮酒煮时光 2025-02-18 12:52:47

写自己的功能怎么样?

这是代码。

count_rows = function(df, val)
{
    checks = 0
    for (i in 1:ncol(df))
    {
       if(any(df[,i] > 0))
          checks = checks + 1
    }
    return (checks)
}

A = matrix(runif(100), 10, 10)

count_rows(A, 0.5)

How about writing your own function?

Here's the code.

count_rows = function(df, val)
{
    checks = 0
    for (i in 1:ncol(df))
    {
       if(any(df[,i] > 0))
          checks = checks + 1
    }
    return (checks)
}

A = matrix(runif(100), 10, 10)

count_rows(A, 0.5)
平定天下 2025-02-18 12:52:47

说尺寸5000x52的矩阵mat

set.seed(1234)
mat <- matrix(trunc(runif(5000*52)*1e5) , 5000 , 52)

dim(mat)

#> [1] 5000   52

,然后我们可以找到52列中有多少列包含一个小于或等于10的数字

sum(apply(mat , 2 , \(x) any(x <= 10)))

#> 24

Say the matrix mat of dimensions 5000x52

set.seed(1234)
mat <- matrix(trunc(runif(5000*52)*1e5) , 5000 , 52)

dim(mat)

#> [1] 5000   52

then we can find how many columns out of 52 contains a number less than or equal to 10 using

sum(apply(mat , 2 , \(x) any(x <= 10)))

#> 24
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文