如何仅隔离R数据框架的最小级级值

发布于 2025-01-25 02:57:05 字数 572 浏览 2 评论 0 原文

我的问题

我有以下(简化的)dataFrame df

        A    B    C
[1,]    2   -5   20
[2,]  -10   -1   10
[3,]   10  -10    0 

我想仅隔离最小幅度的值:

[ 2, -1, 0 ]

我该怎么做?

我尝试过的

到目前为止,我刚刚向我展示了最小值 absolute 值是每行:

  MagMin <- vector()
  
  for(i in 1:nrow(df)){ 
    sub <- df[i,]
    MagMin[i] <- min(abs(df[i,]))
    }

它给我 [2,1, 0] ,但显然,我丢失了最小值的标牌。

I found a nice answer to the question in

My Problem

I have the following (simplified) dataframe df :

        A    B    C
[1,]    2   -5   20
[2,]  -10   -1   10
[3,]   10  -10    0 

I want to isolate just the values of the smallest magnitude:

[ 2, -1, 0 ]

How would I do that?

What I've Tried

So far I've just got it to show me what the minimum absolute value is per row:

  MagMin <- vector()
  
  for(i in 1:nrow(df)){ 
    sub <- df[i,]
    MagMin[i] <- min(abs(df[i,]))
    }

Which gives me [2, 1, 0], but obviously, I've lost the signage for what way the smallest value goes.

I found a nice answer to the question in python, but I can't think how to apply it here!

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

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

发布评论

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

评论(1

请远离我 2025-02-01 02:57:06
apply(mat, 1, function(z) z[which.min(abs(z))])
# [1,] [2,] [3,] 
#    2   -1    0 

演练:

  • 当您想要Min- 幅度时, min(abs(val))将返回其阳性,您知道... < /p>

      val&lt;  -  c(-10L,-1L,10L)
    最小(ABS(val))
    #[1] 1
     
  • 我们可以使用哪个min(abs(val))要确定向量中的哪个是min- 幅度,它返回向量上的索引:

      wher.min(abs(val))
    #[1] 2
     


  • 使用它,我们可以基于最小数字提取特定值(POS或neg):

      val [wher.min(abs(val))]
    #[1] -1
     
  • 重复每行操作,我们使用 apply(mat,1,...) 1 margin = 1 ,意思是“按行”,第三个参数( fun = )是一个函数,它恰好采用一个函数争论并用它做点什么;在这种情况下,第一次被称为 z 有效 mat [1,] ,带有值 c(2,-5,20);第二次,它有效地是 mat [2,] ,带有值 c(-10,-1,10);等。



数据

mat <- structure(list(A = c(2L, -10L, 10L), B = c(-5L, -1L, -10L), C = c(20L, 10L, 0L)), class = "data.frame", row.names = c("[1,]", "[2,]", "[3,]"))
apply(mat, 1, function(z) z[which.min(abs(z))])
# [1,] [2,] [3,] 
#    2   -1    0 

Walk-through:

  • When you want the min-magnitude, min(abs(val)) will return the positive of that, which you know ...

    val <- c(-10L, -1L, 10L)
    min(abs(val))
    # [1] 1
    
  • We can use which.min(abs(val)) to determine which in the vector is the min-magnitude, which returns an index on the vector:

    which.min(abs(val))
    # [1] 2
    
  • Using that, we can extract the specific value (pos or neg) based on the min-magnitude:

    val[which.min(abs(val))]
    # [1] -1
    
  • to repeat the operation for each row, we use apply(mat, 1, ...). The 1 is for MARGIN=1 which means "by row", and the third argument (FUN=) is a function that takes exactly one argument and does something with it; in this case, the first time it's called, z is effectively mat[1,], with the values c(2, -5, 20); the second time, it's effectively mat[2,] with values c(-10, -1, 10); etc.


Data

mat <- structure(list(A = c(2L, -10L, 10L), B = c(-5L, -1L, -10L), C = c(20L, 10L, 0L)), class = "data.frame", row.names = c("[1,]", "[2,]", "[3,]"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文