删除R中某个值之上和之下的所有行

问题描述 投票:0回答:1

我们有公民科学家使用Insitu Aqua troll 600仪器为我们记录数据。它类似于CTD,但不相似。数据格式略有不同。差异很大,以至于我无法使用R中OCE包中的CTD修剪。我需要在浸泡时间(它们开始放低仪器之前在水中的时间)中删除所有数据行,并从数据中删除数据。那就是达到最大深度后的所有行。所以我只需要数据框的中心部分。我的数据

Date Time   Salinity (ppt) (672441) Chlorophyll-a Fluorescence (RFU) (671721)   RDO Concentration (mg/L) (672144)   Temperature (°C) (676121)   Depth (ft) (671051)
16:29.0 0   0.01089297  7.257619    31.91303    0.008220486
16:31.0 0   0.01765913  7.246986    31.93175    0.1499496
16:33.0 0   0.0130412   7.258863    31.93253    0.5387784
16:35.0 0   0.01299242  7.274049    31.93806    0.6187978
16:37.0 0   0.01429801  7.26965 31.94401    0.6640261
16:39.0 0   0.01342988  7.271608    31.93595    0.681709
16:41.0 0   0.01337719  7.271549    31.93503    0.684597
16:43.0 7.087267    0.007094439 6.98015 31.89018    1.598019
16:45.0 28.3442 0.007111916 6.268753    31.83806    1.687673
16:47.0 31.06357    0.007945394 6.197834    31.77821    1.418773
16:49.0 32.07076    0.0080788   6.166986    31.76881    1.382685
16:51.0 31.95504    0.004382414 6.191305    31.72906    1.358556
16:53.0 36.21165    0.01983912  5.732656    29.3942 123.4148
16:55.0 36.37849    0.02243886  5.626586    28.82502    125.2927
16:57.0 36.43061    0.02416219  5.450325    28.23787    126.7997
16:59.0 36.44484    0.02441683  5.421676    28.14037    127.0321
17:01.0 36.46815    4.510316    5.318929    28.09501    127.2064
17:03.0 36.41381    4.012657    5.241654    28.14595    127.2227
17:05.0 36.42724    0.7891375   5.174401    28.20383    127.2019
17:07.0 36.41064    0.4351442   5.120181    28.18592    127.197
17:09.0 36.38155    0.2253969   5.033384    28.21021    127.1895
17:11.0 36.37671    0.2089337   5.019629    28.21222    127.1885
17:13.0 36.43813    0.08728585  4.981099    28.17526    127.2223
17:15.0 36.47644    0.904435    4.951878    28.13579    127.2108
17:17.0 36.54742    0.1230291   4.93056 28.06166    127.2307
17:19.0 36.60466    10.04291    4.908442    27.9397 126.6003
17:21.0 36.61511    11.33922    4.904828    27.92038    126.5161
17:23.0 36.68179    0.6680982   4.87018 27.78319    123.707
17:25.0 36.74612    0.06539913  4.848994    27.72977    119.906
17:27.0 36.75729    0.02414635  4.826871    27.72545    114.9537
17:29.0 37.1578 0.01556828  4.804105    27.81129    113.3405



> depthmax<- max(WS$`Depth (ft) (671051)`, na.rm = TRUE)
> output <- WS[WS$"Depth (ft) (671051)" < depthmax,]
> Output2 <- output[output$"Depth (ft) (671051)" > 1,]

我尝试了这些,并使output2正常工作,但无法缝制以使输出正常工作。有没有更优雅的方法可以做到这一点?回顾一下,我需要在depthmax(127.2307)之后的所有行和深度在其开始降低仪器(〜2.41)之前的所有行中删除。

r rows
1个回答
0
投票

您的代码会删除最大深度,但不会删除达到最大深度后的行。您想要找到最大深度的行索引,然后删除该行以及之后的行:

start <- tail(which(na.omit(WS$`Depth (ft) (671051)`) < 2.41), 1) + 1
end<- which.max(na.omit(WS$`Depth (ft) (671051)`)) - 1
output <- WS[start:end, ]

第一行找到小于2.41的最后一行的索引,并加1以获得起始行。第二行找到最大深度的索引,然后减去1以获得之前的行。

© www.soinside.com 2019 - 2024. All rights reserved.