簡體   English   中英

在R中,如何提取最接近某個值的數組中某個行的列號?

[英]In R, how do I extract the column number of a certain row in an array that's closest to a certain value?

鑒於:

data(veteran)
library(survival)
veteran$prognostic_indicator <- 0
veteran$prognostic_indicator[veteran$karno<50] <- 1
model <- coxph(Surv(time,status)~age+prognostic_indicator,data=veteran)
library(obsSens)
object <- obsSensSCC(model, which = "prognostic_indicator", g0 = seq(1,10,0.01),p0 = c(0.05,0.1,0.2,0.3,0.4), p1 = seq(0, 1, 0.05), logHaz = FALSE, method = "approx")

我可以提取向量:

object$lcl[21,1,1:901]

按降序排列。 我想提取最接近1但高於它的數字的“名稱”。 在那種情況下,我想提取名稱“ 2.69”或位置170,因為相應的數字是1.0001292。 位置2.70的數字為0.9968844,因此過低。

如何在數字最接近值1.0但更高的遞減值向量中提取位置(或名稱)?

如果使用該值創建一個新向量,則確定滿足條件的第一個元素,然后在序列中向后移動一個。

obj <- object$lcl[21,1,1:901] 
obj[which(obj< 1)[1] -1]

#    2.69 
#1.000129 

另一種方法是對反向矢量進行處理。 然后,您無需回溯:

> rev(obj)[which(rev(obj) > 1)[1] ]
    2.69 
1.000129 

這是DWin的清潔方法之外的另一種方法。

which.min(subset(object$lcl[21,1,1:901], object$lcl[21,1,1:901] > 1) - 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM