2017-02-21 25 views
0

我期待在我的數據的特定時間間隔內找到DF中的最小當前值。這是我到目前爲止所嘗試的,我期望得到第49行,但它只是返回第一行。如果我將第一個索引留在數據空白處,我會在我的整個數據集中得到最低的當前值。關於爲什麼開始的任何提示:結束不起作用?如何在DF的某一部分中查找具有最小值的行

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) 

回答

0

您在計算min_row時忘了添加start - 1。嘗試

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. 
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 
相關問題