曲线下面积分析:如何获取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-columndata[,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 return3 4(both x-values where y is 1) - Running
data[which.min(data[,2]), 1]will return3(the first x with the minimum y)
内容的提问来源于stack exchange,提问作者C.Morton
相关产品推荐
相关产品推荐

