如何根据R中另一列的值从数据框中的一列中提取值
How to extract values from one column in a data frame based on values in another in R
我想在p => 1之前的数据框中第一行提取c的值当 p =>1 时,作为数据框中第一行中 d 的值。
然后,当 p 再次下降 < 1。我很难知道如何做到这一点。如有任何建议,我们将不胜感激。
> dput(df)
structure(list(p = c(0.00032, 0.0045, 0.01, 0.34, 0.5576, 0.89,
1.00238, 1.2356, 4.76, 18.96, 23.56, 34.005, 26.778, 16.543,
12.3276, 5.674, 3.45, 1.987, 0.976, 0.5629, 0.34564, 0.034, 0.0123,
0.0023), c = c("23/10/2016 15:23:34", "23/10/2016 15:23:44",
"23/10/2016 15:23:54", "23/10/2016 15:24:04", "23/10/2016 15:24:14",
"23/10/2016 15:24:24", "23/10/2016 15:24:34", "23/10/2016 15:24:44",
"23/10/2016 15:24:54", "23/10/2016 15:25:04", "23/10/2016 15:25:14",
"23/10/2016 15:25:24", "23/10/2016 15:25:34", "23/10/2016 15:25:44",
"23/10/2016 15:25:54", "23/10/2016 15:26:04", "23/10/2016 15:26:14",
"23/10/2016 15:26:24", "23/10/2016 15:26:34", "23/10/2016 15:26:44",
"23/10/2016 15:26:54", "23/10/2016 15:27:04", "23/10/2016 15:27:14",
"23/10/2016 15:27:24"), d = c(324.5, 234.5, 109.5, 236.4, 86.5,
67.8, 126.4, 139.8, 139.8, 145.6, 257.6, 309, 19.4, 359.5, 299.5,
234.5, 134.5, 136.7, 135.7, 138.9, 223.1, 256.7, 295.6, 312.8
)), .Names = c("p", "c", "d"), class = "data.frame", row.names = c(NA,
-24L))
> df
p c d
1 0.00032 23/10/2016 15:23:34 324.5
2 0.00450 23/10/2016 15:23:44 234.5
3 0.01000 23/10/2016 15:23:54 109.5
4 0.34000 23/10/2016 15:24:04 236.4
5 0.55760 23/10/2016 15:24:14 86.5
6 0.89000 23/10/2016 15:24:24 67.8
7 1.00238 23/10/2016 15:24:34 126.4
8 1.23560 23/10/2016 15:24:44 139.8
9 4.76000 23/10/2016 15:24:54 139.8
10 18.96000 23/10/2016 15:25:04 145.6
11 23.56000 23/10/2016 15:25:14 257.6
12 34.00500 23/10/2016 15:25:24 309.0
13 26.77800 23/10/2016 15:25:34 19.4
14 16.54300 23/10/2016 15:25:44 359.5
15 12.32760 23/10/2016 15:25:54 299.5
16 5.67400 23/10/2016 15:26:04 234.5
17 3.45000 23/10/2016 15:26:14 134.5
18 1.98700 23/10/2016 15:26:24 136.7
19 0.97600 23/10/2016 15:26:34 135.7
20 0.56290 23/10/2016 15:26:44 138.9
21 0.34564 23/10/2016 15:26:54 223.1
22 0.03400 23/10/2016 15:27:04 256.7
23 0.01230 23/10/2016 15:27:14 295.6
24 0.00230 23/10/2016 15:27:24 312.8
期望的输出值:
c_1 d_1 c_2 d_2
23/10/2016 15:24:24 126.4 23/10/2016 15:26:34 135.7
不是最优雅的解决方案,但它完成了工作:
indices <- which(df$p >= 1)
c(c_1 = df[min(indices)-1,]$c,
d_1 = df[min(indices),]$d,
c_2 = df[max(indices)+1,]$c,
d_2=df[max(indices)+1,]$d)
我想在p => 1之前的数据框中第一行提取c的值当 p =>1 时,作为数据框中第一行中 d 的值。
然后,当 p 再次下降 < 1。我很难知道如何做到这一点。如有任何建议,我们将不胜感激。
> dput(df)
structure(list(p = c(0.00032, 0.0045, 0.01, 0.34, 0.5576, 0.89,
1.00238, 1.2356, 4.76, 18.96, 23.56, 34.005, 26.778, 16.543,
12.3276, 5.674, 3.45, 1.987, 0.976, 0.5629, 0.34564, 0.034, 0.0123,
0.0023), c = c("23/10/2016 15:23:34", "23/10/2016 15:23:44",
"23/10/2016 15:23:54", "23/10/2016 15:24:04", "23/10/2016 15:24:14",
"23/10/2016 15:24:24", "23/10/2016 15:24:34", "23/10/2016 15:24:44",
"23/10/2016 15:24:54", "23/10/2016 15:25:04", "23/10/2016 15:25:14",
"23/10/2016 15:25:24", "23/10/2016 15:25:34", "23/10/2016 15:25:44",
"23/10/2016 15:25:54", "23/10/2016 15:26:04", "23/10/2016 15:26:14",
"23/10/2016 15:26:24", "23/10/2016 15:26:34", "23/10/2016 15:26:44",
"23/10/2016 15:26:54", "23/10/2016 15:27:04", "23/10/2016 15:27:14",
"23/10/2016 15:27:24"), d = c(324.5, 234.5, 109.5, 236.4, 86.5,
67.8, 126.4, 139.8, 139.8, 145.6, 257.6, 309, 19.4, 359.5, 299.5,
234.5, 134.5, 136.7, 135.7, 138.9, 223.1, 256.7, 295.6, 312.8
)), .Names = c("p", "c", "d"), class = "data.frame", row.names = c(NA,
-24L))
> df
p c d
1 0.00032 23/10/2016 15:23:34 324.5
2 0.00450 23/10/2016 15:23:44 234.5
3 0.01000 23/10/2016 15:23:54 109.5
4 0.34000 23/10/2016 15:24:04 236.4
5 0.55760 23/10/2016 15:24:14 86.5
6 0.89000 23/10/2016 15:24:24 67.8
7 1.00238 23/10/2016 15:24:34 126.4
8 1.23560 23/10/2016 15:24:44 139.8
9 4.76000 23/10/2016 15:24:54 139.8
10 18.96000 23/10/2016 15:25:04 145.6
11 23.56000 23/10/2016 15:25:14 257.6
12 34.00500 23/10/2016 15:25:24 309.0
13 26.77800 23/10/2016 15:25:34 19.4
14 16.54300 23/10/2016 15:25:44 359.5
15 12.32760 23/10/2016 15:25:54 299.5
16 5.67400 23/10/2016 15:26:04 234.5
17 3.45000 23/10/2016 15:26:14 134.5
18 1.98700 23/10/2016 15:26:24 136.7
19 0.97600 23/10/2016 15:26:34 135.7
20 0.56290 23/10/2016 15:26:44 138.9
21 0.34564 23/10/2016 15:26:54 223.1
22 0.03400 23/10/2016 15:27:04 256.7
23 0.01230 23/10/2016 15:27:14 295.6
24 0.00230 23/10/2016 15:27:24 312.8
期望的输出值:
c_1 d_1 c_2 d_2
23/10/2016 15:24:24 126.4 23/10/2016 15:26:34 135.7
不是最优雅的解决方案,但它完成了工作:
indices <- which(df$p >= 1)
c(c_1 = df[min(indices)-1,]$c,
d_1 = df[min(indices),]$d,
c_2 = df[max(indices)+1,]$c,
d_2=df[max(indices)+1,]$d)