You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

曲线下面积分析:如何获取y取最小值时对应的x值?

Get Corresponding X-Value When Y is Minimum in R

Got it, let's sort this out for you! You're currently using min(data[,2]) to grab the smallest value from your y-column (column 2 of your data object), but what you actually need is the x-value paired with that minimum y. Here are straightforward, practical ways to do this in R:

Basic Approach (Returns All Matching X-Values)

If there could be multiple rows where y hits the minimum value, this code will return all corresponding x-values:

# Extract all x-values where y equals its minimum
data[data[, 2] == min(data[, 2]), 1]

Breakdown of how it works:

  • min(data[,2]) finds the smallest value in your y-column
  • data[,2] == min(data[,2]) creates a logical vector marking which rows have that minimum y
  • We use that vector to filter the data, then pull the first column (your x-values)

Get Only the First Matching X-Value

If you only care about the first occurrence of the minimum y, use which.min()—it returns the index of the first minimum value in the y-column:

# Extract the first x-value where y is minimum
data[which.min(data[,2]), 1]

Quick Test Example

Let's say your dataset looks like this:

data <- data.frame(x = c(1, 2, 3, 4, 5), y = c(5, 3, 1, 1, 4))
  • Running data[data[,2] == min(data[,2]), 1] will return 3 4 (both x-values where y is 1)
  • Running data[which.min(data[,2]), 1] will return 3 (the first x with the minimum y)

内容的提问来源于stack exchange,提问作者C.Morton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:33:52