【问题标题】:How to sort & extract values with multiple conditions in R?如何在R中对具有多个条件的值进行排序和提取?
【发布时间】:2019-01-20 17:32:37
【问题描述】:

我有一个基本的条件数据提取问题。我已经用 Python 编写了代码。我正在学习 R;我想在 R 中复制相同的代码。

我尝试使用 which 来放置条件参数,但这似乎不起作用。我还没有完全精通 R 语法。

我有一个包含 2 列的数据框:x 和 y 我们的想法是提取最大 5 个 x 值乘以 2 的列表,对应于最大 y 值,条件是我们将仅选择那些 y 值至少是峰值 y 值的 0.45 倍。

所以,算法将有以下步骤:

  1. 我们找到y的峰值:max_y

  2. 我们定义阈值 = 0.45 * max_y

  3. 我们应用过滤器,以获取所有大于阈值的 y 值的列表:y_filt

  4. 我们得到与步骤 3 中的 y 值对应的 x 值列表:x_filt

  5. 如果 x_filt 中的值的个数小于或等于 5,那么我们的结果将是 x_filt 中的值乘以 2

  6. 如果 x_filt 的值超过 5 个,我们只选择列表中 5 个最大 y 值对应的 5 个值。然后我们乘以 2 得到我们的结果

Python 代码

max_y = max(y)
max_x = x[y.argmax()]
print (max_x, max_y)

threshold = 0.45 * max_y
y_filt = y [y > threshold]
x_filt = x [y > threshold]


if len(y_filt) > 4:
    n_highest = 5
else:
    n_highest = len(y_filt)

y_filt_highest = y_filt.argsort()[-n_highest:][::-1]        
result = [x_filt[i]*2 for i in range(len(x_filt)) if i in y_filt_highest]

例如数据集

x           y
1          20
2           7
3           5
4          11
5           0  
6           8
7           3
8          10
9           2
10          6
11         15
12         18
13          0
14          1
15         12

上面的代码会给出如下结果

max_y = 20
max_x = 1
threshold = 9
y_filt = [20, 11, 10, 15, 18, 12]
x_filt = [1, 4, 8, 11, 12, 15]
n_highest = 5
y_filt_highest = [20, 11, 15, 18, 12]
result = [2, 8, 22, 24, 30]

我希望在 R 中做同样的事情。

【问题讨论】:

  • 所以通常在 SO 中,我们希望您做的不仅仅是问“我该怎么做?”特别是通过显示“我已经尝试过的东西”。大概您已经知道如何获得 y 的最大值?以及如何获取阈值?
  • 你是对的,当然。我刚从 R 开始,我认为展示我尝试过的东西会很愚蠢,因为我确信会有一些基本错误。因此,我用一个可重现的示例共享了我的 python 代码。是的,我只能进入算法中的第 2 步(阈值);然后我开始收到错误。

标签: python r dataframe


【解决方案1】:

R 如此强大/易于用于统计工作的原因之一是内置的 data.frame 是基础。在这里使用一个可以简化事情:

# Create a dataframe with the toy data
df <- data.frame(x = 1:10, y = c(20, 7, 5, 11, 0, 8, 3, 10, 2, 6))

# Refer to columns with the $ notation
max_y <- max(df$y)
max_x <- df$x[which(df$y == max_y)]

# If you want to print both values, you need to create a list with c()
print(c(max_x, max_y))
# But you could also just call the values directly, as in python
max_x
max_y

# Calculate a threshold and then create a filtered data.frame
threshold <- 0.45 * max_y
df_filt <- df[which(df$y > threshold), ]
df_filt <- df_filt[order(-df_filt$y), ]
if(nrow(df_filt) > 5){
  df_filt <- df_filt[1:5, ]
}

# Calculate the result
result <- df_filt$x * 2
# Alternatively, you may want the result to be part of your data.frame
df_filt$result <- df_filt$x*2

# Should show identical results
max_y
max_x
threshold
df_filt # Probably don't want to print a df if it is large
result

当然,如果您真的需要 y_filtx_filt 的单独向量,您可以在事后轻松创建它们:

y_filt <- df_filt$y
x_filt <- df_filt$x

请注意,如果您的最大值不是唯一的,则与 numpy.argmaxwhich(df$y == max(y)) 一样,将返回多个值。

【讨论】:

  • 感谢您的回答。您能否就我的问题的最后一部分提出建议? x_filt 的值不能超过 5 个。例如,如果它有 10 个值;我只需要选择对应于最大 y 值的 5 个值。
  • 啊,好吧,我已经相应地编辑了代码。我建议您只需按 y 值降序对数据框进行排序,然后测试结果维度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 2019-09-16
相关资源
最近更新 更多