Calculating differences with specific values in data frame in R

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

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的方法:

library(dplyr)

df <- data.frame(subject = c(rep(1, 6), rep(2, 3)),
             var = "SMSPAg",
             session = c("T1", "T1", "T2", "T2", "T3", "T3","T1", "T1","T2"),
             timepoint = c("a", "b", "a", "b","a", "b","a", "b", "a"),
             value = c(50, 48, 52, 65, 51, 61, 53, 50, 54)
             )    

df %>%
  summarise(diff = last(value) - first(value), .by = c(subject, session))
  subject session diff
1       1      T1   -2
2       1      T2   13
3       1      T3   10
4       2      T1   -3
5       2      T2    0
英文:

Here is a dplyr way:

library(dplyr)

df &lt;- data.frame(subject = c(rep(1, 6), rep(2, 3)),
             var = &quot;SMSPAg&quot;,
             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;),
             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;),
             value = c(50, 48, 52, 65, 51, 61, 53, 50, 54)
             )    

df %&gt;% 
  summarise(diff = last(value) - first(value), .by = c(subject, session))
  subject session diff
1       1      T1   -2
2       1      T2   13
3       1      T3   10
4       2      T1   -3
5       2      T2    0

答案2

得分: 2

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

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

library(tidyverse)

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

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

其中

df &lt;- tribble(
  ~subject,     ~var, ~session, ~timepoint, ~value,
        1L, &quot;SMSPAg&quot;,     &quot;T1&quot;,        &quot;a&quot;,    50L,
        1L, &quot;SMSPAg&quot;,     &quot;T1&quot;,        &quot;b&quot;,    48L,
        1L, &quot;SMSPAg&quot;,     &quot;T2&quot;,        &quot;a&quot;,    52L,
        1L, &quot;SMSPAg&quot;,     &quot;T2&quot;,        &quot;b&quot;,    65L,
        1L, &quot;SMSPAg&quot;,     &quot;T3&quot;,        &quot;a&quot;,    51L,
        1L, &quot;SMSPAg&quot;,     &quot;T3&quot;,        &quot;b&quot;,    61L,
        2L, &quot;SMSPAg&quot;,     &quot;T1&quot;,        &quot;a&quot;,    53L,
        2L, &quot;SMSPAg&quot;,     &quot;T1&quot;,        &quot;b&quot;,    50L,
        2L, &quot;SMSPAg&quot;,     &quot;T2&quot;,        &quot;a&quot;,    54L
  )
英文:

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.

library(tidyverse)

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

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

where

df &lt;- tribble(
  ~subject,     ~var, ~session, ~timepoint, ~value,
        1L, &quot;SMSPAg&quot;,     &quot;T1&quot;,        &quot;a&quot;,    50L,
        1L, &quot;SMSPAg&quot;,     &quot;T1&quot;,        &quot;b&quot;,    48L,
        1L, &quot;SMSPAg&quot;,     &quot;T2&quot;,        &quot;a&quot;,    52L,
        1L, &quot;SMSPAg&quot;,     &quot;T2&quot;,        &quot;b&quot;,    65L,
        1L, &quot;SMSPAg&quot;,     &quot;T3&quot;,        &quot;a&quot;,    51L,
        1L, &quot;SMSPAg&quot;,     &quot;T3&quot;,        &quot;b&quot;,    61L,
        2L, &quot;SMSPAg&quot;,     &quot;T1&quot;,        &quot;a&quot;,    53L,
        2L, &quot;SMSPAg&quot;,     &quot;T1&quot;,        &quot;b&quot;,    50L,
        2L, &quot;SMSPAg&quot;,     &quot;T2&quot;,        &quot;a&quot;,    54L
  )

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:

确定