T1 T2 T3 T4 T5 1 1 NA NA 1 NA NA 1 1 1 NA 1 NA NA NA NA NA NA NA 1
假設我的資料框是這樣的(請看下圖,抱歉我不知道如何在 stackoverflow 中復制這些資料)。T1代表第一個時期,T5代表最后一個時期。

對于每一行,我想找到一個最長的價差,使得這兩個索引之間沒有 NA 出現。
所以第一行的結果應該是從 T1 到 T2(索引 1 到索引 2)
第 2 行的結果應該是 T3 - T5 第 3 行的結果應該是 0,因為沒有這樣令人滿意的結果。
我希望所有結果都放入資料框中,因為我實際上有很多行,所以我希望我的最終資料集是這樣的:

太感謝了
uj5u.com熱心網友回復:
對不起,我沒有看到你改變了你的問題。這是基于 R 的簡單解決方案。首先,在串列中,我提取具有連續非 NA 值的列的名稱。從中我提取了第一個和最后一個組件。
df<-data.frame(T1=c(1,NA,NA,NA),
T2=c(1,NA,1,NA),
T3=c(NA,1,NA,NA),
T4=c(NA,1,NA,NA),
T5=c(1,1,NA,1))
lcon<-apply(df, MARGIN=1, function(x) {!na.contiguous(x)})
df$start_T <- lapply(lcon, function(x) (head(names(x), n=1)))
df$end_T <- lapply(lcon, function(x) (tail(names(x), n=1)))
然后,您可以將零分配給沒有連續值的序列,即 werestart_T和end_Tare 相等。但請記住,如果您遇到多個長度相同的序列,則只會提取其中的最后一個。例如,嘗試運行相同的代碼,但使用此資料集作為示例,我將第一行修改為具有兩個長度為 2 的序列。
df_m<-data.frame(T1=c(1,NA,NA,NA),
T2=c(1,NA,1,NA),
T3=c(NA,1,NA,NA),
T4=c(1,1,NA,NA),
T5=c(1,1,NA,1))
uj5u.com熱心網友回復:
通常對于索引,你從 0 而不是 1 開始計數,所以這個 python 腳本會這樣做。但是,如果您希望第一個元素為 1 而不是 0,則可以在第 48 和 49 行添加 1
# Take user input and make it a list
input = input().split(' ')
# Count the number of columns
columns = 0
for element in input:
if element.startswith('T'):
columns = 1
else:
break
# Determine the number of rows
rows = int(len(input) / columns) - 1
# Print headers of the table
print ("{:<8} {:<15} {:<10}".format("row", "start_T", "end_T"))
# For every row, loop trough that row
for i in range(rows):
# Determine the first and last index of the row
start = rows 1 i * columns
end = start columns
# Initialize a streak and record variables
# The low and high are the the 2 indicies we are looking for
streak = -1
streak_low = 0
streak_high = 0
record = 0
record_low = 0
record_high = 0
# Loop trough the row
for j, element in enumerate(input[start:end]):
#print(i, j, element)
# If the element is 1, update the streak
if element == "1":
streak = 1
streak_high = j
# Update lower streak boundary if this is the first in the streak
if streak == 0:
streak_low = j
# If the streak is greater then the current record, update the record
if streak > record:
record = streak
record_low = streak_low # 1 Normally with indicies you start to count at 0
record_high = streak_high # 1 if you want to start the count at 1 add the plus 1 here
# If element is not 1 (thus NA) set the streak back to -1
else:
streak = -1
streak_low = 0
streak_high = 0
# Print the row number and start / end index
print ("{:<8} {:<15} {:<10}".format(f"row_{i 1}", record_low, record_high))
uj5u.com熱心網友回復:
一種使用方法rle
outp <- setNames(data.frame(t(apply(d, 1, function(x){
rl = rle(unlist(x))$lengths
rbind(which.max(rl) * any(rl > 1), (which.max(rl) max(rl) - 1) * any(rl > 1))
}))), c("start_T", "end_T"))
cbind(row = paste0("row_", 1:nrow(outp)), outp)
row start_T end_T
1 row_1 1 2
2 row_2 3 5
3 row_3 0 0
4 row_4 0 0
資料
d <- structure(list(T1 = list(1L, NA, NA, NA), T2 = list(1L, NA, 1L,
NA), T3 = list(NA, 1L, NA, NA), T4 = list(NA, 1L, NA, NA),
T5 = list(1L, 1L, NA, 1L)), class = "data.frame", row.names = c(NA,
-4L))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/536375.html
標籤:r循环索引
