【问题标题】:I want to apply a formula in R, multiply a value in one vector by that in another vector and so on我想在 R 中应用一个公式,将一个向量中的值乘以另一个向量中的值,依此类推
【发布时间】: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_timeend_time 的时间,因此它是基于 stimulus subject replicateattribute 的复杂性评级的持续时间。
  • 你能添加更多数据吗?
  • 我可以使用什么代码来打印更多数据?
  • 尝试dput(head(data, n)) 并在此处复制结果。这将为我们提供数据对象的第一行n。 n 足够大以包含有趣的数据。例如,n = 25

标签: r for-loop


【解决方案1】:

这是您的代码在我的 PC 上运行的结果,其中似乎是正确计算的列 complexity_x_duration

> tail(tds_merged_duration4,20)
# A tibble: 20 x 11
# Groups:   Replicate, Stimulus, Subject, Attribute [2]
   Replicate Stimulus Subject Attribute Timepoint Dominant Complexity start_time end_time duration complexity_x_duration
       <int>    <int> <chr>   <chr>         <dbl>    <int>      <int>      <dbl>    <dbl>    <dbl>                 <dbl>
 1         1        1 S001    Soft           0.87        1          2       0.77     0.99    0.220                 0.440
 2         1        1 S001    Soft           0.88        1          2       0.77     0.99    0.220                 0.440
 3         1        1 S001    Soft           0.89        1          2       0.77     0.99    0.220                 0.440
 4         1        1 S001    Soft           0.9         1          2       0.77     0.99    0.220                 0.440
 5         1        1 S001    Soft           0.91        1          2       0.77     0.99    0.220                 0.440
 6         1        1 S001    Soft           0.92        1          2       0.77     0.99    0.220                 0.440
 7         1        1 S001    Soft           0.93        1          2       0.77     0.99    0.220                 0.440
 8         1        1 S001    Soft           0.94        1          2       0.77     0.99    0.220                 0.440
 9         1        1 S001    Soft           0.95        1          2       0.77     0.99    0.220                 0.440
10         1        1 S001    Soft           0.96        1          2       0.77     0.99    0.220                 0.440
11         1        1 S001    Soft           0.97        1          2       0.77     0.99    0.220                 0.440
12         1        1 S001    Soft           0.98        1          2       0.77     0.99    0.220                 0.440
13         1        1 S001    Soft           0.99        1          2       0.77     0.99    0.220                 0.440
14         2        1 S001    Soft           0.19        1          2       0.19     0.23    0.04                 NA    
15         2        1 S001    Soft           0.2         1          2       0.19     0.23    0.04                  0.08 
16         2        1 S001    Soft           0.21        1          2       0.19     0.23    0.04                  0.08 
17         2        1 S001    Soft           0.22        1          2       0.19     0.23    0.04                  0.08 
18         2        1 S001    Soft           0.23        1          2       0.19     0.23    0.04                  0.08 
19         2        1 S001    Soft           0.24        1          1       0.24     0.99    0.75                  0    
20         2        1 S001    Soft           0.25        1          1       0.24     0.99    0.75                  0.75 

【讨论】:

  • 好的,我在想如果我将digit = 更改为complexity_x_duration,它会显示为您的,我只是想弄清楚如何做到这一点......
  • 我不知道如何更改complexity_x_duration 的小数点。当我使用formatC(tds_merged_duration4$complexity_x_duration, digits = 3) 时返回" " 你能建议吗?
  • formatC(tds_merged_duration4$complexity_x_duration, 3) 为我返回 " NA" "0.44" "0.44" "0.44" "0.44" "0.44"
  • formatC(tds_merged_duration4$complexity_x_duration, 3) 为我返回这个[1] " NA" " 0" " 0" " 0" " 0" " 0" " 0" " 0" " 0" [10] " 0" " 0" " 0" " 0" " 0" " 0" " 0" " 0" " 0" [19] " 0" " 0" " 0" " 0" " 0" " NA" " 0" " 0" " 0" [28] " 0" " 1" " 0" " 0" " 0" " 0" " 0" " 0" " 0" 我迷路了!
  • 我现在有 3 个小数位,但还有更多的零??[1] NA 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 [14] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 NA 0.00 0.00 [27] 0.00 0.00 0.75 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 [40] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 [53] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
【解决方案2】:

我通过删除ifelse 函数解决了这个问题

modified_tdc_merged<-tdc_merged.df %>% 
  as.data.frame() %>% 
  mutate(Timepoint = as.numeric(gsub("[a-zA-Z]+", "", Timepoint))) %>% 
  group_by(Replicate, Stimulus, Subject, Attribute, Complexity) %>% 
  summarise(
    start_time = min(Timepoint), 
    end_time = max(Timepoint),
    duration = end_time - start_time,
    n = n()
  ) %>% 
  ungroup()

tdc_cwd_score.df <- modified_tdc_merged %>% 
  group_by(Replicate, Stimulus, Subject, Attribute) %>% 
  summarise(
    avg = sum(Complexity * duration) / sum(duration)
  )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多