使用具有多个时间测量值的变量进行迭代:purrr 和 map() 函数

Iteration with variables with several time measurements: purrr and map() functions

我有一个大型数据库要处理这种结构 我真实数据库的str是这样的 tibble [561 x 128] (S3: tbl_df/tbl/data.frame)

让我们在这个数据框中总结一下我需要做什么

paciente <- c(6430, 6494, 6165, 6278, 6188, 6447, 6207, 6463)
sexo_s1 <-  c("Hombre", "Mujer", "Mujer", "Mujer", "Hombre", "Hombre", "Mujer")
edad_s1 <- c(54, 68, 75, 85, 78, 80, 78, 90)
peso1_v00 <- c(115.2, 85, 98, 87, 85, 78, 84, 98)
cintura1_v00 <- c(115, 125, 110, 114, 120, 121 125, 110)
coltot_v00 <- c(215, 220, 210, 225, 215, 220, 230, 220)
peso1_v66 <- c(110.2, 80, 95, 87, 83, 78, 84, 98)
cintura1_v01 <- c(112, 125, 110, 110, 112, 121 120, 110)
coltot_v01 <- c(210, 210, 205, 215, 215, 210, 230,1 220)
peso1_v01 <- c(110.2, 80, 95, 87, 83, 78, 84, 98)
cintura1_v01 <- c(112, 125, 110, 110, 112, 121 120, 110)
coltot_v01 <- c(210, 210, 205, 215, 215, 210, 230,1 220)

我需要进行几个统计分析:

运行 数值变量(128 个变量中的 125 个)的正态性检验(shapiro.test 和箱线图)。我正在尝试使用 purrr::map 和类似的方法 (purrr:map_dfr)

iterative_example<-map_dfr(.x = quos(paciente, sexo_s1, edad_s1, peso1_v00, cintura1_v00, coltot_v00, peso1_v66, cintura1_v66, coltot_v66, peso1_v01, cintura1_v01, coltot_v01), .f = ~ shapiro.test, data = df_example)

error/rlang_error> 参数 1 必须是数据框或命名的原子向量。 回溯:

purrr::map_dfr(...) dplyr::bind_rows(res, .id = .id)t。 如果我将 map_dfr 与地图交换,我将获得一个无法导出或转换为 data.frame

的列表
iterative_example<-map(.x = quos(paciente, sexo_s1, edad_s1, peso1_v00, cintura1_v00, coltot_v00, peso1_v66, cintura1_v66, coltot_v66, peso1_v01, cintura1_v01, coltot_v01), .f = ~ shapiro.test, data = df_example)

9 人名单:

函数(x)

函数(x)

函数(x)

我暂时无法导出或取消嵌套列表来获取p值和t结果,但我会整理一下。但是我想要 data.frame.

与此操作类似,我必须 运行 在不同时间观察到的变量之间迭代 t.test,如果所采取的测量之间存在显着差异(我尝试了相同的地图功能,但我得到了 shapiro.test 的确切嵌套列表 例如

t.test(df_example$peso1_v00,  df_example$peso1_v66)
t.test(df_example$cintura1_v00,  df_example$cintura1_v66)

识别变量名称的语法: “i_variable1_v00”在特定时间“v00”并使用“i_variable1_v66”进行测试。我试过:starts_with() 但没有结果

我不确定如何执行此操作并导出输出

Welch 两样本 t 检验

数据:df_example$cintura1_v00 和 df_example$cintura1_v66 t = -0.051503,df = 10.399,p 值 = 0.9599 备择假设:均值的真实差异不等于 0 95% 置信区间: -5.504848 5.254848 样本估计: x 的平均值 y 的平均值 117.500 117.625

2 - 从 0、6 和 12 个月的值迭代创建新列。我已经创建了变量,但系统地重复了数据库中变量的行。我的数据库中有一个不同变量的例子。

我正在寻找 sthg 在不同时间点获取的变量之间迭代地在新列中创建变量:

d_peso1_v66: 相差 0- +6 个月 d_peso1_v01:差异 0 - 12 个月

没有迭代的 2 个变量示例:

df_example<-mutate(df_example, d_peso1_v66 = peso1_v66 - peso1_v00)
df_example<-mutate(df_example, d_coltot_v01 = coltot_v01 - coltot_v00)

d_variable1_v66 = i_variable1_v66 - i_variable1_v00 d_variable1_v01 = i_variable1_v01 - i_variable1_v00

d_variable2_v01 = i_variable2_v66 - i_variable2_v00 d_variable2_v01 = i_variable2_v01 - i_variable2_v00

df_example <-mutate(across(where(is.numeric)),
varname <- paste("varname01", if variable contains "01" )
df_example <- mutate(df, varname = Petal.Width * n)

不确定是否可以一步完成,或者是否有必要创建一个函数并使用映射函数通过数据库。 Sthg 像这样但有所作为 (difference_function)

meanofcol <- function(df, col) {
mutate(df, "Mean of {{col}}" := mean({{col}}))
}
meanofcol(iris, Petal.Width)`

然后用map函数

df_example2 <- map_dfr (.x = df_example, .f = ~ difference_function, data = df_example)

我一直在努力尝试不同的方法,如果我知道如何编写语法,这些方法所花费的时间比我认为应该花费的时间要多得多

试试这个。请注意,我删除了您数据中的重复行。

df <- data.frame(
  paciente = c(6430, 6494, 6165, 6278, 6188, 6447, 6207, 6463),
  sexo_s1 =  c("Hombre", "Mujer", "Mujer", "Mujer", "Hombre", "Hombre", "Mujer", "Hombre"),
  edad_s1 = c(54, 68, 75, 85, 78, 80, 78, 90),
  peso1_v00 = c(115.2, 85, 98, 87, 85, 78, 84, 98),
  cintura1_v00 = c(115, 125, 110, 114, 120, 121, 125, 110),
  coltot_v00 = c(215, 220, 210, 225, 215, 220, 230, 220),
  peso1_v66 = c(110.2, 80, 95, 87, 83, 78, 84, 98),
  cintura1_v01 = c(112, 125, 110, 110, 112, 121, 120, 110),
  peso1_v01 = c(110.2, 80, 95, 87, 83, 78, 84, 98),
  coltot_v01 = c(210, 210, 205, 215, 215, 210, 230, 220))

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
library(broom)

## First you need to convert non-numeric variables recorded as number into factor

df$paciente <- factor(df$paciente)

## Select numeric variables, pivot in long format, analyse

df |> 
  select(where(is.numeric)) |> 
  pivot_longer(everything()) |>
  group_by(name) |> 
  do(tidy(shapiro.test(.$value)))
#> # A tibble: 8 × 4
#> # Groups:   name [8]
#>   name         statistic p.value method                     
#>   <chr>            <dbl>   <dbl> <chr>                      
#> 1 cintura1_v00     0.897  0.270  Shapiro-Wilk normality test
#> 2 cintura1_v01     0.806  0.0330 Shapiro-Wilk normality test
#> 3 coltot_v00       0.958  0.792  Shapiro-Wilk normality test
#> 4 coltot_v01       0.896  0.269  Shapiro-Wilk normality test
#> 5 edad_s1          0.925  0.469  Shapiro-Wilk normality test
#> 6 peso1_v00        0.870  0.149  Shapiro-Wilk normality test
#> 7 peso1_v01        0.905  0.322  Shapiro-Wilk normality test
#> 8 peso1_v66        0.905  0.322  Shapiro-Wilk normality test

## Now select only *_v00 and *_v66, then pivot to longer and separate

df |> 
  select(paciente, matches("_v00|_v01|_v66")) |> 
  pivot_longer(-paciente) |> 
  separate(name, into=c("name", "time"), sep="_") |> 
  pivot_wider(names_from=time, values_from=value) |> 
  group_by(name) |> 
  do(tidy(t.test(.$v00, .$v01)))
#> # A tibble: 3 × 11
#> # Groups:   name [3]
#>   name     estimate estimate1 estimate2 statistic p.value parameter conf.low
#>   <chr>       <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>
#> 1 cintura1     2.5      118.      115       0.824   0.424      14.0    -4.01
#> 2 coltot       5        219.      214.      1.42    0.178      13.4    -2.58
#> 3 peso1        1.88      91.3      89.4     0.329   0.747      13.9   -10.4 
#> # … with 3 more variables: conf.high <dbl>, method <chr>, alternative <chr>

reprex package (v2.0.1)

于 2021-09-16 创建

您可以修改代码以获得其他比较(例如,00 与 66)。