【发布时间】:2020-06-02 21:09:49
【问题描述】:
我有一个complexity 向量,其评分为 1-9,每个复杂度评分的时间为 durations,我想将持续时间乘以该时期的相应复杂度分数,并将权重乘以持续时间,例如:
主题 S001,复制 1,刺激 S1,“软”属性:2*(0.99 - 0.77) / 0.99-0.77 = 2
那么在一个复杂度等级超过 1 个的例子中,
主题 S001,复制 2,刺激 S1,“软”属性:2*(0.32-0.14)+1*(0.99-0.32)/(0.32-0.14)+(0.99-0.32) = 1.21
structure(list(Replicate = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), Stimulus = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Subject = c("S001", "S001",
"S001", "S001", "S001", "S001", "S001", "S001", "S001", "S001",
"S001", "S001", "S001", "S001", "S001", "S001", "S001", "S001",
"S001", "S001", "S001", "S001", "S001", "S001", "S001", "S001",
"S001", "S001", "S001", "S001"), Attribute = c("Soft", "Soft",
"Soft", "Soft", "Soft", "Soft", "Soft", "Soft", "Soft", "Soft",
"Soft", "Soft", "Soft", "Soft", "Soft", "Soft", "Soft", "Soft",
"Soft", "Soft", "Soft", "Soft", "Soft", "Soft", "Soft", "Soft",
"Soft", "Soft", "Soft", "Soft"), Timepoint = c(0.77, 0.78, 0.79,
0.8, 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9,
0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 0.19, 0.2,
0.21, 0.22, 0.23, 0.24, 0.25), Dominant = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Complexity = c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L), start_time = c(0.77,
0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77,
0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77, 0.77,
0.19, 0.19, 0.19, 0.19, 0.19, 0.24, 0.24), end_time = c(0.99,
0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99,
0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99, 0.99,
0.23, 0.23, 0.23, 0.23, 0.23, 0.99, 0.99), duration = c(0.22,
0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22,
0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22,
0.04, 0.04, 0.04, 0.04, 0.04, 0.75, 0.75), complexity_x_duration = c(NA,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, NA, 0, 0, 0, 0, 0.75, 0)), row.names = c(NA, -30L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), groups = structure(list(Replicate = 1:2,
Stimulus = c(1L, 1L), Subject = c("S001", "S001"), Attribute = c("Soft",
"Soft"), .rows = list(1:23, 24:30)), row.names = c(NA, -2L
), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
到目前为止,我的最佳尝试如下,但它只返回零。我觉得我要绕道而行,也许可以使用forloop 来使它更容易。
tds_merged_duration4<-modified_tds_merged %>%
group_by(Replicate, Stimulus, Subject, Attribute) %>%
mutate(duration = end_time - start_time) %>%
mutate(complexity_x_duration = ifelse(Complexity!=lag(Complexity, 1), duration*Complexity,0))
这是另一个尝试:
for (i in 1:20614) {
if (tds_merged_duration3$Complexity==lag(tds_merged_duration3$Complexity,1)){
NA
}else{
(tds_merged_duration3$Complexity*tds_merged_duration3$duration)/tds_merged_duration3$duration
}
}
【问题讨论】:
-
你的代码没有问题;由于每行的
Complexity等于2,因此您最终将每行的Complexity != lag(Complexity, 1)等于FALSE,并且您的ifelse每次都会评估为0。Duration每行也有相同的值。因此,您在最后一个 mutate 中的表达式将始终计算为相同的值。您的数据中的值是否正确? -
值在整个数据集中发生变化,但仍返回零。
Complexity是介于 1 和 9 之间的评级。“持续时间”是从start_time到end_time的时间,因此它是基于stimulussubjectreplicate或attribute的复杂性评级的持续时间。 -
你能添加更多数据吗?
-
我可以使用什么代码来打印更多数据?
-
尝试
dput(head(data, n))并在此处复制结果。这将为我们提供数据对象的第一行n。 n 足够大以包含有趣的数据。例如,n = 25