R找到曲线急剧变化的x和y值
R find the x and y value where the curve changes sharply
我有一个包含两列 x, y 的数据集,如下所示
x y
0.5789474 0.0011382324
1.0000000 0.0024540588
0.8000000 0.0017039382
0.7272727 0.0014921618
0.8421053 0.0018399977
0.8611111 0.0019049152
0.3750000 0.0007843210
0.7837838 0.0016542579
0.7222222 0.0014784711
0.7619048 0.0015895130
0.7435897 0.0015372644
0.4375000 0.0008791528
0.8750000 0.0019537960
0.6666667 0.0013359048
0.8750000 0.0019537960
0.8571429 0.0018911749
0.6896552 0.0013931524
1.0000000 0.0024540588
0.9285714 0.0021543502
0.9523810 0.0022499579
绘制这些值生成如下图
我的目标是找到曲线中斜率或变化率最大的 x 和 y 值。弯曲或曲线最尖锐的地方。我试过了,
(lead(y)-y)/(lead(x)-x)
这是行不通的。非常感谢任何建议或意见。提前致谢。
很高兴见到你,野牛
在我看来,
您的代码在排序后运行良好。
我不知道你为什么说 (lead(y)-y)/(lead(x)-x)
不起作用。
请参阅以下代码:
x <- sort(c(
0.5789474,1.0000000,
0.8000000,0.7272727,
0.8421053,0.8611111,
0.3750000,0.7837838,
0.7222222,0.7619048,
0.7435897,0.4375000,
0.8750000,0.6666667,
0.8750000,0.8571429,
0.6896552,1.0000000,
0.9285714,0.9523810
))
y <- sort(c(
0.0011382324,0.0024540588,
0.0017039382,0.0014921618,
0.0018399977,0.0019049152,
0.0007843210,0.0016542579,
0.0014784711,0.0015895130,
0.0015372644,0.0008791528,
0.0019537960,0.0013359048,
0.0019537960,0.0018911749,
0.0013931524,0.0024540588,
0.0021543502,0.0022499579
))
> (lead(y)-y)/(lead(x)-x)
[1] 0.001517309 0.001831632 0.002253465 0.002490271 0.002619790
[6] 0.002710761 0.002764148 0.002852761 0.002959226 0.003063622
[11] 0.003231410 0.003403282 0.003462603 0.003519415 NaN
[16] 0.003743680 0.004015511 0.004286123 NaN NA
x[which.max((lead(y)-y)/(lead(x)-x))]
y[which.max((lead(y)-y)/(lead(x)-x))]
我在指点:
df = read.table(text="
0.5789474 0.0011382324
1.0000000 0.0024540588
0.8000000 0.0017039382
0.7272727 0.0014921618
0.8421053 0.0018399977
0.8611111 0.0019049152
0.3750000 0.0007843210
0.7837838 0.0016542579
0.7222222 0.0014784711
0.7619048 0.0015895130
0.7435897 0.0015372644
0.4375000 0.0008791528
0.8750000 0.0019537960
0.6666667 0.0013359048
0.8750000 0.0019537960
0.8571429 0.0018911749
0.6896552 0.0013931524
1.0000000 0.0024540588
0.9285714 0.0021543502
0.9523810 0.0022499579",header=FALSE)
现在计算坡度:
slope= diff(df$V2)/diff(df$V1)
从最大斜率中检索对应的 x,y 点:
df[which.max(slope),]
输出:
V1 V2
18 1 0.002454059
欢迎指正!
我有一个包含两列 x, y 的数据集,如下所示
x y
0.5789474 0.0011382324
1.0000000 0.0024540588
0.8000000 0.0017039382
0.7272727 0.0014921618
0.8421053 0.0018399977
0.8611111 0.0019049152
0.3750000 0.0007843210
0.7837838 0.0016542579
0.7222222 0.0014784711
0.7619048 0.0015895130
0.7435897 0.0015372644
0.4375000 0.0008791528
0.8750000 0.0019537960
0.6666667 0.0013359048
0.8750000 0.0019537960
0.8571429 0.0018911749
0.6896552 0.0013931524
1.0000000 0.0024540588
0.9285714 0.0021543502
0.9523810 0.0022499579
绘制这些值生成如下图
我的目标是找到曲线中斜率或变化率最大的 x 和 y 值。弯曲或曲线最尖锐的地方。我试过了,
(lead(y)-y)/(lead(x)-x)
这是行不通的。非常感谢任何建议或意见。提前致谢。
很高兴见到你,野牛
在我看来, 您的代码在排序后运行良好。
我不知道你为什么说 (lead(y)-y)/(lead(x)-x)
不起作用。
请参阅以下代码:
x <- sort(c(
0.5789474,1.0000000,
0.8000000,0.7272727,
0.8421053,0.8611111,
0.3750000,0.7837838,
0.7222222,0.7619048,
0.7435897,0.4375000,
0.8750000,0.6666667,
0.8750000,0.8571429,
0.6896552,1.0000000,
0.9285714,0.9523810
))
y <- sort(c(
0.0011382324,0.0024540588,
0.0017039382,0.0014921618,
0.0018399977,0.0019049152,
0.0007843210,0.0016542579,
0.0014784711,0.0015895130,
0.0015372644,0.0008791528,
0.0019537960,0.0013359048,
0.0019537960,0.0018911749,
0.0013931524,0.0024540588,
0.0021543502,0.0022499579
))
> (lead(y)-y)/(lead(x)-x)
[1] 0.001517309 0.001831632 0.002253465 0.002490271 0.002619790
[6] 0.002710761 0.002764148 0.002852761 0.002959226 0.003063622
[11] 0.003231410 0.003403282 0.003462603 0.003519415 NaN
[16] 0.003743680 0.004015511 0.004286123 NaN NA
x[which.max((lead(y)-y)/(lead(x)-x))]
y[which.max((lead(y)-y)/(lead(x)-x))]
我在指点:
df = read.table(text="
0.5789474 0.0011382324
1.0000000 0.0024540588
0.8000000 0.0017039382
0.7272727 0.0014921618
0.8421053 0.0018399977
0.8611111 0.0019049152
0.3750000 0.0007843210
0.7837838 0.0016542579
0.7222222 0.0014784711
0.7619048 0.0015895130
0.7435897 0.0015372644
0.4375000 0.0008791528
0.8750000 0.0019537960
0.6666667 0.0013359048
0.8750000 0.0019537960
0.8571429 0.0018911749
0.6896552 0.0013931524
1.0000000 0.0024540588
0.9285714 0.0021543502
0.9523810 0.0022499579",header=FALSE)
现在计算坡度:
slope= diff(df$V2)/diff(df$V1)
从最大斜率中检索对应的 x,y 点:
df[which.max(slope),]
输出:
V1 V2
18 1 0.002454059
欢迎指正!