【问题标题】:Map function to iterate t.test映射函数以迭代 t.test
【发布时间】:2021-11-22 02:30:31
【问题描述】:

感谢 StackOverflow 的一位同事,我的数据库比原来的更整洁了,我真的很感激

数据框由一个患者 ID 组成,该 ID 具有多个随时间推移的测量值(00 = 基础,66 = 6 个月,01 = 12 个月)。我知道我把数据框没有按时间顺序排列

df1<-data.frame(pacient<- c(6430, 6430, 6430, 6494, 6494, 6494, 6165, 6165, 6165),
                time<- c(00, 01, 66, 00, 01, 66, 00, 01, 66),
                weight <- c(115, 112, 110, 98, 95, 94, 88, 87, 86),
                waist <- c(123, NA, 112, 115, 112, 113, 112, 110, NA),
                p14_total<- c(7, NA, 4, 12, 5, NA, 15, 12, 13))
  • t.test

我正在尝试在不同时间点之间的测量值之间进行比较,而不是配对。例如,00 岁时体重与 66 岁时体重/00 岁时体重与 01 岁时体重/66 岁时体重与 01 岁时体重 我正在寻找一个数据框或 data.table 来导出统计数据(t、pvalue、mean..)

  • 创建一个列,其中包含每位患者的不同时间测量值之间的差异。 例如:患者 ID:6430 Weight_6months = Weight01 - Weight66 Weight_12months= Weight01 - Weight00

我真的想用 purrr::map 函数来执行此操作,但我没有达到目标

【问题讨论】:

    标签: r iteration purrr


    【解决方案1】:

    如果我理解了这个问题,那么一个简单的解决方案是更广泛地旋转数据,然后执行年份之间的差异:

    注意如果有很多年,则使用 mutate() 和 across(),允许选择列,而无需指定它们。

    library(tidyverse)
    
    df1<-data.frame(pacient = c(6430, 6430, 6430, 6494, 6494, 6494, 6165, 6165, 6165),
                    time = c(00, 01, 66, 00, 01, 66, 00, 01, 66),
                    weight = c(115, 112, 110, 98, 95, 94, 88, 87, 86),
                    waist = c(123, NA, 112, 115, 112, 113, 112, 110, NA),
                    p14_total = c(7, NA, 4, 12, 5, NA, 15, 12, 13)) %>% 
      as_tibble()
    
    df2 <- df1 %>% 
      group_by(pacient) %>% 
      pivot_wider(names_from = time, values_from = c(weight, waist, p14_total)) %>% 
      rowwise() %>% 
      mutate(weight_diff_1 = weight_66 - weight_1, weight_diff_2 = weight_1 - weight_0)
    
    # A tibble: 3 x 12
    # Rowwise:  pacient
      pacient weight_0 weight_1 weight_66 waist_0 waist_1 waist_66 p14_total_0 p14_total_1 p14_total_66 weight_diff_1 weight_diff_2
        <dbl>    <dbl>    <dbl>     <dbl>   <dbl>   <dbl>    <dbl>       <dbl>       <dbl>        <dbl>         <dbl>         <dbl>
    1    6430      115      112       110     123      NA      112           7          NA            4            -2            -3
    2    6494       98       95        94     115     112      113          12           5           NA            -1            -3
    3    6165       88       87        86     112     110       NA          15          12           13            -1            -1
    

    以及列上的 t.test

    t.test(df2$waist_0, df2$weight_1)   
    
    Welch Two Sample t-test
    
    data:  df2$waist_0 and df2$weight_1
    t = 2.3133, df = 2.7634, p-value = 0.1112
    alternative hypothesis: true difference in means is not equal to 0
    95 percent confidence interval:
     -8.302625 45.635958
    sample estimates:
    mean of x mean of y 
     116.6667   98.0000 
    

    【讨论】:

    • 感谢您的帮助,但这不是我想要的。实际上,数据已经按照您刚才的方式进行了整理。我想迭代在不同时间比较的变量,并在新列中进行减法。为每个变量单独编码是我所做的,但我不打算重复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2018-08-29
    • 2021-11-10
    • 2023-01-23
    • 1970-01-01
    • 2021-08-30
    相关资源
    最近更新 更多