下面是我的示例数据
df <- data.frame('a'=c(0.1,0.2,0.3,0.4,1.0,1.1,1.2,3.1,3.2),'b'=c(0,0,0,0,0,0,0,0,100,120)))
我希望看到单个值3.1
作为我的输出
感谢任何帮助。
要在b
中第一个大于 0 的值之后获得相应的a
值,您可以这样做
df$a[which.max(df$b > 0)]
#[1] 3.1
或者只有which
df$a[which(df$b > 0)[1]]
数据
df <- data.frame(a=c(0.1,0.2,0.3,0.4,1.0,1.1,1.2,1.3,3.1,3.2),
b=c(0,0,0,0,0,0,0,0,100,120))