如何在DF的一部分中查找具有最小值的行

时间:2017-02-21 19:31:35

标签: r search dataframe min

我希望在一定的数据间隔内找到DF中的最小电流值。这就是我到目前为止所尝试的内容,我期待获得第49行,但它只返回第一行。如果我将数据中的第一个索引留空,我将得到整个数据集中的最低当前值。关于为什么开始:结束的任何提示都不起作用?

start = ceiling(0.4*nrow(data))
end = ceiling(0.8*nrow(data))
min_row = which.min(data[start:end,"current"]) #returns 1 

min_row = which.min(data[,"current"]) #returns 44 (searches all data) 

2 个答案:

答案 0 :(得分:0)

您在计算start - 1时忘记添加min_row。尝试

min_row = which.min(data[start:end,"current"]) + start - 1
#The first value is the index in the list from 'start' to 'end'.
#You need to add 'start - 1' to the value to get the index from beginning of the data.

答案 1 :(得分:0)

首先,您至少应该为我们提供数据框的链接或编写代码来创建数据框。请检查下面的代码,看看它是否有效,评论应该清楚一切。 :)

set.seed(10) # setting seed for reproducability

data <- data.frame(bla=runif(100), current=runif(100))
head(data)

start = ceiling(0.4*nrow(data))
end = ceiling(0.8*nrow(data))

data[start:end, "current"] # <- this is the interval you are looking for the minimum value, its not the whole dat
min_row = which.min(data[start:end, "current"]) ## you get the minimum value of the interval provided. 

# you need to add all of indexes which you have dropped from the start

TRUE_min_row <- min_row + start - 1 

# the -1 part comes from the fact that youre a searching between numbers WITH start[1] included

min_row = which.min(data[ ,"current"]) 

TRUE_min_row == min_row
# as you can see these match