- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个数据框。第一个 df
总结了超时(DateTime
)一个鱼类的几个个体(ID
)的检测。例如:
options("digits.secs" = 3)
df<- data.frame(DateTime=c("2017-08-05 14:03:55.300","2017-08-05 16:18:12.100","2017-08-05 20:34:31.540","2017-08-05 16:18:14.355","2017-08-05 20:34:33.605"),
ID= c("A","B","C","B","C"))
df
DateTime ID
1 2017-08-05 14:03:55.300 A
2 2017-08-05 16:18:12.100 B
3 2017-08-05 20:34:31.540 C
4 2017-08-05 16:18:14.355 B
5 2017-08-05 20:34:33.605 C
另一个数据框 Activity
包含这些人随时间的事件信息。该数据具有高时间分辨率。也就是说,它是每秒 11 个数据(11 赫兹)。作为一个可重现的例子:
set.seed(100)
fmt <- "%Y-%m-%d %H:%M:%OS"
DateTime = seq(from=as.POSIXct("2017-08-05 14:03:55.100", format=fmt, tz="UTC"), by=1/11, length.out=80)
ID = rep("A", each=80)
x= sample(seq(from = -1, to = 1, by = 0.01), size = 80, replace = TRUE)
y= sample(seq(from = -1, to = 1, by = 0.01), size = 80, replace = TRUE)
z= sample(seq(from = -1, to = 1, by = 0.01), size = 80, replace = TRUE)
Activity1<- data.frame(DateTime,ID, x, y, z)
DateTime = seq(from=as.POSIXct("2017-08-05 16:18:11.900", format=fmt, tz="UTC"),by=1/5, length.out=40)
ID = rep("B", each=40)
x= sample(seq(from = -1, to = 1, by = 0.01), size = 40, replace = TRUE)
y= sample(seq(from = -1, to = 1, by = 0.01), size = 40, replace = TRUE)
z= sample(seq(from = -1, to = 1, by = 0.01), size = 40, replace = TRUE)
Activity2<- data.frame(DateTime,ID, x, y, z)
DateTime = seq(from=as.POSIXct("2017-08-05 16:18:19.703", format=fmt, tz="UTC"),by=1/11, length.out=40)
ID = rep("B", each=40)
x= sample(seq(from = -1, to = 1, by = 0.01), size = 40, replace = TRUE)
y= sample(seq(from = -1, to = 1, by = 0.01), size = 40, replace = TRUE)
z= sample(seq(from = -1, to = 1, by = 0.01), size = 40, replace = TRUE)
Activity3<- data.frame(DateTime,ID, x, y, z)
DateTime = seq(from=as.POSIXct("2017-08-05 20:34:31.240", format=fmt, tz="UTC"),by=1/11, length.out=80)
ID = rep("C", each=80)
x= sample(seq(from = -1, to = 1, by = 0.01), size = 80, replace = TRUE)
y= sample(seq(from = -1, to = 1, by = 0.01), size = 80, replace = TRUE)
z= sample(seq(from = -1, to = 1, by = 0.01), size = 80, replace = TRUE)
Activity4<- data.frame(DateTime,ID, x, y, z)
Activity<- rbind(Activity1,Activity2,Activity3,Activity4)
head(Activity)
DateTime ID x y z
1 2017-08-05 14:03:55.099 A 0.01 -0.16 -1.00
2 2017-08-05 14:03:55.190 A 0.11 0.55 -0.69
3 2017-08-05 14:03:55.281 A 0.50 0.79 1.00
4 2017-08-05 14:03:55.372 A 0.97 -0.76 0.24
5 2017-08-05 14:03:55.463 A -0.97 -0.59 0.20
6 2017-08-05 14:03:55.554 A -0.46 0.42 -0.88
我正在使用下面的代码来计算 df
中的变量 VeDBA
和 RMS
。我使用来自 Activity
的数据来计算它们,并将这些变量添加到数据框 df
中。作为总结,对于每一行 df
数据,我使用 2 秒的数据(这是 22 行,因为我有数据帧 Activity
中每秒 11 个数据)来自 Activity
数据帧并使用数据帧 df
的 DateTime
> 作为开始时间。
library(data.table)
setDT(df)[, DateTime := as.POSIXct(DateTime, format=fmt, tz="UTC")][,
c("start", "end") := .(DateTime, DateTime+2)]
setDT(Activity)[, DateTime := as.POSIXct(DateTime, format=fmt, tz="UTC")]
df<- Activity[df, on=.(ID, DateTime>=start, DateTime<=end),
by=.EACHI, .(
DateTime=i.DateTime,
ID=i.ID,
VeDBA=sum(sqrt(x^2 + y^2 + z^2)) / .N,
RMS=sqrt((sum(x^2) + sum(y^2) + sum(z^2)) / .N))][,
(1L:3L) := NULL][]
问题是有时我每秒有 5 个数据而不是数据帧 Activity
中的 11 个。因此,我想包含某种代码来指示比,例如,当我在提到的 2 秒期间内的数据少于 14 个时,我想为 VeDBA
显示 NA
> 和 df
中的 RMS
。
到目前为止,使用上述代码我得到了这个:
df
DateTime ID VeDBA RMS
1: 2017-08-05 14:03:55.299 A 0.9919576 1.0264458
2: 2017-08-05 16:18:12.099 B 0.9375138 0.9573975
3: 2017-08-05 20:34:31.539 C 0.9294209 0.9764383
4: 2017-08-05 16:18:14.355 B 0.7542922 0.7886634
5: 2017-08-05 20:34:33.605 C 1.0041628 1.0395891
我想得到这个:
df
DateTime ID VeDBA RMS
1: 2017-08-05 14:03:55.299 A 0.9919576 1.0264458
2: 2017-08-05 16:18:12.099 B NA NA # Between 16:18:12.099 and 16:18:14.099 there is only 10 data instead of 22
3: 2017-08-05 20:34:31.539 C 0.9294209 0.9764383
4: 2017-08-05 16:18:14.355 B NA NA
5: 2017-08-05 20:34:33.605 C 1.0041628 1.0395891
有谁知道如何修改我使用 data.table
的代码来获取那些 NA
?
最佳答案
如果 .N
低于给定的阈值 n_min
,下面的修改将返回 NA
:
n_min <- 14L
Activity[df, on = .(ID, DateTime >= start, DateTime <= end),
by = .EACHI, .(
DateTime = i.DateTime,
ID = i.ID,
.N, # inserted just to verify the result, to be omitted in production code
VeDBA = if (.N < n_min) NA_real_ else sum(sqrt(x ^ 2 + y ^ 2 + z ^ 2)) / .N,
RMS = if (.N < n_min) NA_real_ else sqrt((sum(x ^ 2) + sum(y ^ 2) + sum(z ^ 2)) / .N)
)][,
(1L:3L) := NULL][]
DateTime ID N VeDBA RMS
1: 2017-08-05 14:03:55.299 A 22 0.8777660 0.9181305
2: 2017-08-05 16:18:12.099 B 10 NA NA
3: 2017-08-05 20:34:31.539 C 22 0.8807835 0.9383084
4: 2017-08-05 16:18:14.355 B 10 NA NA
5: 2017-08-05 20:34:33.605 C 22 1.0587765 1.1023549
请注意,插入 N
列只是为了验证结果。另请注意,尽管 set.seed(100)
用于创建数据,但输出与 OP 的预期结果不同。
if()
可以在这里使用,因为每个 .EACHI
组只有一个 .N
值。
关于r - 如何在 `data.table`中加入有最小数据量的条件来计算一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59480858/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!