R代码:如何计算列表中超出(或低于)数字的次数

时间:2014-04-09 10:40:29

标签: r list

我有一份水表测量清单,为期2 - 4年,想要计算超过某个地下水位的次数(例如-0.22)或低于(第二步)。

我尝试过:

sum(abs(Mylist <= -0.22) < 1e-6)

但R回答'not senseful for factors'

解决此问题的正确方法是什么?

以下是数据链接:

https://www.dropbox.com/sh/oq9v7nbg7baadm8/QmDuVpTk2v

我会非常感谢提示!

1 个答案:

答案 0 :(得分:0)

library(data.table)
library(reshape2)
library(lubridate)

df = fread("/storage/Downloads/Watertable_fluctuations_All_Original.txt", 
     skip = 1, header = T) # dropbox file
df = df[, date := dmy(date)]

dfm = melt(df, id.vars = "date", value.name = "water.level")

wl.threshold = -0.22

dfm[water.level < wl.threshold,.N, keyby = variable] # count per measuring station

# bonus
library(openair)
timePlot(dfm[variable == "Yel342(2010)"],
     pollutant = "water.level", 
     smooth = T, 
     ref.y = wl.threshold)

enter image description here