如何在数据帧列表中使用 log() 转换并执行 aov()?

How to use log() transformation in a dataframe list and perform aov()?

希望有人能帮助我。我正在尝试转换我的 WeightHeight 数据以遵循正态分布并在每个数据框中使用它执行方差分析。但是,我无法使用转换后的值将新列附加到原始数据框。 PersonAge 因素在分析中很重要。

我有这个数据

df_list <- list(
  `1.3.A` = 
    tibble::tribble(
      ~Person, ~Height, ~Weight,  ~Age, 
      "Alex",    175L,     75L,    15,
      "Gerard",    180L,     85L,  17,
      "Clyde",    179L,     79L,   19,
      "Alex",    175L,     75L,    22,
      "Gerard",    180L,     85L,  21,
      "Clyde",    179L,     79L,    20

    ),
  `2.2.A` = 
    tibble::tribble(
      ~Person, ~Height, ~Weight,   ~Age,
      "Alex",    175L,     75L,     18,
      "Gerard",    180L,     85L,   18,
      "Clyde",    179L,     79L,    19,
       "Alex",    175L,     75L,    25,
      "Gerard",    180L,     85L,   27,
      "Clyde",    179L,     79L,    26
    ), 
  `1.1.B` = 
    tibble::tribble(
      ~Person, ~Height, ~Weight,    ~Age,
      "Alex",    175L,     75L,       22,
      "Gerard",    180L,     85L,     23,
      "Clyde",    179L,     79L,      25, 
      "Alex",    175L,     75L,       27,
      "Gerard",    180L,     85L,     18,
      "Clyde",    179L,     79L,      19   
    )
)

失败的尝试是:

russia <- lapply(df_list, (function(x) log10(Age)))
greece <- function(df){ 
  mutate(df, transformed_Age = log10(df$Age)) 
}

spain <- lapply(df_list, greece)
View(spain)
arkansas <- df_list
canada <- function(df){ 
    log10(as.numeric(df$Age))
}

arkansas <- lapply(df_list, canada)
View(arkansas)

我也想尝试其他的转换,所以我希望有人能告诉我我该怎么做。我尝试了 boxcox 转换但失败了。

你可以使用-

cols <- c('Height', 'Weight')
df_list <- lapply(df_list, function(x) {x[cols] <- log10(x[cols]);x})
df_list

#$`1.3.A`
# A tibble: 6 x 4
#  Person Height Weight   Age
#  <chr>   <dbl>  <dbl> <dbl>
#1 Alex     2.24   1.88    15
#2 Gerard   2.26   1.93    17
#3 Clyde    2.25   1.90    19
#4 Alex     2.24   1.88    22
#5 Gerard   2.26   1.93    21
#6 Clyde    2.25   1.90    20

#$`2.2.A`
# A tibble: 6 x 4
#  Person Height Weight   Age
#  <chr>   <dbl>  <dbl> <dbl>
#1 Alex     2.24   1.88    18
#2 Gerard   2.26   1.93    18
#3 Clyde    2.25   1.90    19
#4 Alex     2.24   1.88    25
#5 Gerard   2.26   1.93    27
#6 Clyde    2.25   1.90    26

#$`1.1.B`
# A tibble: 6 x 4
#  Person Height Weight   Age
#  <chr>   <dbl>  <dbl> <dbl>
#1 Alex     2.24   1.88    22
#2 Gerard   2.26   1.93    23
#3 Clyde    2.25   1.90    25
#4 Alex     2.24   1.88    27
#5 Gerard   2.26   1.93    18
#6 Clyde    2.25   1.90    19

如果您想保持原始列不变并创建具有转换值的新列

cols <- c('Height', 'Weight')
new_cols <- paste0('new_', cols)
df_list <- lapply(df_list, function(x) {x[new_cols] <- log10(x[cols]);x})
df_list