如何根据R中另一列中的值从数据框中的一列中提取值

时间:2016-11-28 21:00:18

标签: r extract rule

我想在 p => 1之前的数据框的第一行中提取 c 的值,以及的值当 p => 1时,数据框第一行中的

p 掉落时,我想确定下一行中 c d 的值。再次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

1 个答案:

答案 0 :(得分:1)

不是最优雅的解决方案,但它可以胜任:

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)