Calculating differences with specific values in data frame in R

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

Calculating differences with specific values in data frame in R

问题

我有以下的数据框在RStudio中:
screenshot from my dataframe

时间点a和b是前后的数值,我想计算两者之间的差值,即b-a。

我想要为每个受试者和每个会话单独进行计算,也就是说,对于受试者1,我想要计算T1、T2和T3的差值。

非常感谢任何帮助!

我考虑过使用Tidyverse进行过滤和子集选择,但这似乎非常复杂,我想肯定有更简单的方法。

英文:

I have the following dataframe in RStudio:
screenshot from my dataframe

Timepoint a and b are pre- and post values and I want to calculate the difference between the two i.e. b-a

I want to do this for each subject and each session seperately meaning for subject 1 I want to calculate the difference for T1, T2 and T3.

I greatly appreciate any help!

I thought about filtering and subsetting with Tidyverse but this seems very complicated and I gues there must be an easier way.

答案1

得分: 2

这是一个使用dplyr的方法:

  1. library(dplyr)
  2. df <- data.frame(subject = c(rep(1, 6), rep(2, 3)),
  3. var = "SMSPAg",
  4. session = c("T1", "T1", "T2", "T2", "T3", "T3","T1", "T1","T2"),
  5. timepoint = c("a", "b", "a", "b","a", "b","a", "b", "a"),
  6. value = c(50, 48, 52, 65, 51, 61, 53, 50, 54)
  7. )
  8. df %>%
  9. summarise(diff = last(value) - first(value), .by = c(subject, session))
  1. subject session diff
  2. 1 1 T1 -2
  3. 2 1 T2 13
  4. 3 1 T3 10
  5. 4 2 T1 -3
  6. 5 2 T2 0
英文:

Here is a dplyr way:

  1. library(dplyr)
  2. df &lt;- data.frame(subject = c(rep(1, 6), rep(2, 3)),
  3. var = &quot;SMSPAg&quot;,
  4. session = c(&quot;T1&quot;, &quot;T1&quot;, &quot;T2&quot;, &quot;T2&quot;, &quot;T3&quot;, &quot;T3&quot;,&quot;T1&quot;, &quot;T1&quot;,&quot;T2&quot;),
  5. timepoint = c(&quot;a&quot;, &quot;b&quot;, &quot;a&quot;, &quot;b&quot;,&quot;a&quot;, &quot;b&quot;,&quot;a&quot;, &quot;b&quot;, &quot;a&quot;),
  6. value = c(50, 48, 52, 65, 51, 61, 53, 50, 54)
  7. )
  8. df %&gt;%
  9. summarise(diff = last(value) - first(value), .by = c(subject, session))
  1. subject session diff
  2. 1 1 T1 -2
  3. 2 1 T2 13
  4. 3 1 T3 10
  5. 4 2 T1 -3
  6. 5 2 T2 0

答案2

得分: 2

你可以选择将数据变宽,然后按常规计算两个变量之间的差异,或者保持数据在长格式中,并按照TarJae的建议从每个向量中提取特定元素(按组拆分)。

变宽重塑(如下所示)的优点是不需要ab按正确顺序排列。

  1. library(tidyverse)
  2. df |&gt;
  3. pivot_wider(
  4. names_from = timepoint,
  5. values_from = value
  6. ) |&gt;
  7. mutate(difference = b - a)
  8. #&gt; # A tibble: 5 &#215; 6
  9. #&gt; subject var session a b difference
  10. #&gt; &lt;int&gt; &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  11. #&gt; 1 1 SMSPAg T1 50 48 -2
  12. #&gt; 2 1 SMSPAg T2 52 65 13
  13. #&gt; 3 1 SMSPAg T3 51 61 10
  14. #&gt; 4 2 SMSPAg T1 53 50 -3
  15. #&gt; 5 2 SMSPAg T2 54 NA NA

<sup>Created on 2023-04-13 with reprex v2.0.2</sup>

其中

  1. df &lt;- tribble(
  2. ~subject, ~var, ~session, ~timepoint, ~value,
  3. 1L, &quot;SMSPAg&quot;, &quot;T1&quot;, &quot;a&quot;, 50L,
  4. 1L, &quot;SMSPAg&quot;, &quot;T1&quot;, &quot;b&quot;, 48L,
  5. 1L, &quot;SMSPAg&quot;, &quot;T2&quot;, &quot;a&quot;, 52L,
  6. 1L, &quot;SMSPAg&quot;, &quot;T2&quot;, &quot;b&quot;, 65L,
  7. 1L, &quot;SMSPAg&quot;, &quot;T3&quot;, &quot;a&quot;, 51L,
  8. 1L, &quot;SMSPAg&quot;, &quot;T3&quot;, &quot;b&quot;, 61L,
  9. 2L, &quot;SMSPAg&quot;, &quot;T1&quot;, &quot;a&quot;, 53L,
  10. 2L, &quot;SMSPAg&quot;, &quot;T1&quot;, &quot;b&quot;, 50L,
  11. 2L, &quot;SMSPAg&quot;, &quot;T2&quot;, &quot;a&quot;, 54L
  12. )
英文:

You can either reshape the data wider and then compute the difference between two variables as normal, or you can keep the data in long format and extract a specific element from each vector (broken down by group) as suggested by TarJae.

Reshaping wider (as shown below) has the advantage that it does not require a and b to be in the correct order.

  1. library(tidyverse)
  2. df |&gt;
  3. pivot_wider(
  4. names_from = timepoint,
  5. values_from = value
  6. ) |&gt;
  7. mutate(difference = b - a)
  8. #&gt; # A tibble: 5 &#215; 6
  9. #&gt; subject var session a b difference
  10. #&gt; &lt;int&gt; &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  11. #&gt; 1 1 SMSPAg T1 50 48 -2
  12. #&gt; 2 1 SMSPAg T2 52 65 13
  13. #&gt; 3 1 SMSPAg T3 51 61 10
  14. #&gt; 4 2 SMSPAg T1 53 50 -3
  15. #&gt; 5 2 SMSPAg T2 54 NA NA

<sup>Created on 2023-04-13 with reprex v2.0.2</sup>

where

  1. df &lt;- tribble(
  2. ~subject, ~var, ~session, ~timepoint, ~value,
  3. 1L, &quot;SMSPAg&quot;, &quot;T1&quot;, &quot;a&quot;, 50L,
  4. 1L, &quot;SMSPAg&quot;, &quot;T1&quot;, &quot;b&quot;, 48L,
  5. 1L, &quot;SMSPAg&quot;, &quot;T2&quot;, &quot;a&quot;, 52L,
  6. 1L, &quot;SMSPAg&quot;, &quot;T2&quot;, &quot;b&quot;, 65L,
  7. 1L, &quot;SMSPAg&quot;, &quot;T3&quot;, &quot;a&quot;, 51L,
  8. 1L, &quot;SMSPAg&quot;, &quot;T3&quot;, &quot;b&quot;, 61L,
  9. 2L, &quot;SMSPAg&quot;, &quot;T1&quot;, &quot;a&quot;, 53L,
  10. 2L, &quot;SMSPAg&quot;, &quot;T1&quot;, &quot;b&quot;, 50L,
  11. 2L, &quot;SMSPAg&quot;, &quot;T2&quot;, &quot;a&quot;, 54L
  12. )

huangapple
  • 本文由 发表于 2023年4月13日 17:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004033.html
匿名

发表评论

匿名网友

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

确定