从其他列的分组中减去特定行的值。

huangapple go评论99阅读模式
英文:

Subtracting values from specific rows based on groupings in other columns

问题

我有一个数据框:

  1. example <- data.frame(
  2. Subject = c(101,101,101,101,101,101,
  3. 102,102,102),
  4. Event = c('Negative_other_6',
  5. 'Negative_other_6',
  6. 'Negative_self_1',
  7. 'Negative_other_6',
  8. 'Negative_self_1',
  9. 'Negative_self_1',
  10. 'Negative_other_5',
  11. 'Negative_other_5',
  12. 'Negative_other_5'
  13. ),
  14. TrialType = c("ReframeNeg", "ImmerseNeu", "ImmerseNeu",
  15. "ImmerseNeg", "ReframeNeg", "ImmerseNeg",
  16. "ReframeNeg", "ImmerseNeu", "ImmerseNeg"),
  17. Rating = c(3,1,1,4,3,5,3,1,5)
  18. )

对于每个相同的事件,我想要从ReframeNeg的评分中减去ImmerseNeg的评分。输出应该是一个新的数据框,按SubjectEvent组织。这应该是示例输出:

  1. solution <- data.frame(
  2. Subject = c(101,101, 102),
  3. Event = c('Negative_other_6',
  4. 'Negative_self_1',
  5. 'Negative_other_5'),
  6. NegAffDiff = c(1, 2, 2)
  7. )

有人知道我怎么才能实现这个吗?非常感谢!

英文:

I have a dataframe:

  1. example &lt;- data.frame(
  2. Subject = c(101,101,101,101,101,101,
  3. 102,102,102),
  4. Event = c(&#39;Negative_other_6&#39;,
  5. &#39;Negative_other_6&#39;,
  6. &#39;Negative_self_1&#39;,
  7. &#39;Negative_other_6&#39;,
  8. &#39;Negative_self_1&#39;,
  9. &#39;Negative_self_1&#39;,
  10. &#39;Negative_other_5&#39;,
  11. &#39;Negative_other_5&#39;,
  12. &#39;Negative_other_5&#39;
  13. ),
  14. TrialType = c(&quot;ReframeNeg&quot;, &quot;ImmerseNeu&quot;, &quot;ImmerseNeu&quot;,
  15. &quot;ImmerseNeg&quot;, &quot;ReframeNeg&quot;, &quot;ImmerseNeg&quot;,
  16. &quot;ReframeNeg&quot;, &quot;ImmerseNeu&quot;, &quot;ImmerseNeg&quot;),
  17. Rating = c(3,1,1,4,3,5,3,1,5)
  18. )

For each of the same event, I want to subtract the rating ReframeNeg from ImmerseNeg. The output should be a new dataframe that's organized by Subject and Event. This should be the example output:

  1. solution &lt;- data.frame(
  2. Subject = c(101,101, 102),
  3. Event = c(&#39;Negative_other_6&#39;,
  4. &#39;Negative_self_1&#39;,
  5. &#39;Negative_other_5&#39;),
  6. NegAffDiff = c(1, 2, 2)
  7. )

Does anyone know how I can achieve this? Thanks so much!

答案1

得分: 0

以下是使用您的示例数据的一种选项。您可以使用pivot_wider来为每个TrialType创建一个列,为每个Subject/Event/TrialType组合分配Rating。然后找到ReframeNeg和ImmerseNeg之间的差异。

  1. library(dplyr)
  2. library(tidyr)
  3. example %>%
  4. pivot_wider(names_from = c("TrialType"), values_from = "Rating") %>%
  5. mutate(NegAffDiff = ImmerseNeg - ReframeNeg) %>%
  6. select(Subject, Event, NegAffDiff)

一个数据表: 3 × 3

Subject Event NegAffDiff
1 101 Negative_other_6 1
2 101 Negative_self_1 2
3 102 Negative_other_5 2

  1. <details>
  2. <summary>英文:</summary>
  3. Here&#39;s one option using your sample data. You can use pivot wider to create a column for each TrialType which assigns the Rating for each combo of Subject/Event/TrialType. Then find the difference between ReframeNeg and ImmerseNeg.

library(dplyr)
library(tidyr)

example %>%
pivot_wider(names_from = c("TrialType"), values_from = "Rating") %>%
mutate(NegAffDiff = ImmerseNeg - ReframeNeg) %>%
select(Subject, Event, NegAffDiff)

A tibble: 3 × 3

Subject Event NegAffDiff
<dbl> <fct> <dbl>
1 101 Negative_other_6 1
2 101 Negative_self_1 2
3 102 Negative_other_5 2

  1. </details>

huangapple
  • 本文由 发表于 2023年7月7日 00:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定