Map function to iterate t.test

Viewed 94

I have a database a little bit tidier than the original thanks to a StackOverflow colleague I'm really grateful

The data frame is composed of a patient ID which has several measurements along time (00 = basal, 66 = 6 months, 01 = 12 months). I know I put the data frame not chronologically ordered

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

I am trying to perform comparisons between the measurements between the different time points, NOT paired. For example weight at 00 vs weight at 66 / weight at 00 vs weight at 01 /weight at 66 vs weight at 01 I am looking for a data frame or data.table to export with the statistics (t, pvalue, mean..)

  • Create a column with the difference between the different time measurements for each patient. For example: patient ID: 6430 Weight_6months = Weight01 - Weight66 Weight_12months= Weight01 - Weight00

I am really trying to perform this with purrr::map functions but I'm not reaching the objective

1 Answers

If I understand the question, then a simple solution is pivot the data wider, then perform the differencing between years:

NB If there are lots of years, then using mutate() with across(), allows selection of the columns, without specifying them.

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

And the t.test on the columns

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 
Related