1

This is the loop that is generating the error.

Error: ! Can't subset columns with df_numeric[[column]]. ✖ Can't convert from df_numeric[[column]] to due to loss of precision.

df_numeric <- df[, sapply(df, is.numeric)]

for (column in names(df_numeric)) {
  res.aov <- anova_test(data = df, dv= df_numeric[[column]], wid = `Subject`, within = `Timepoint`, between = `Genotype`)
  get_anova_table(res.aov)
}  

But when I pull out the code for the anova and specifically input the column from my dataframe it generates the proper anova table results.

res.aov <- anova_test(data = df, dv=  `Tregs CD127lo CD25+`, wid = `Subject`, within = `Timepoint`, between = `Genotype`)
get_anova_table(res.aov)

I have tried using df_numeric$column.

Dataframe

library(rstatix)

 dput(df_numeric)
structure(list(`Tregs CD127lo CD25+` = c(2702, 2175, 2651, 1672.8, 
3762, 4264, 1975, 3208, 3285, 3457, 3383, 2619.9, 11872, 16101, 
13443, 3935, 1894, 2297, 7385, 8901, 9522, 7100, 8789, 9309, 
371, 379, 514), `Monocytes % of Live by Size` = c(1.38, 2.66, 
4.74, 5.83, 3.9, 5.06, 6.36, 3.45, 2.64, 6.33, 10.7, 9.41, 3.42, 
3.46, 2.73, 2.38, 3.12, 4.44, 5.31, 3.59, 4.91, 1.53, 6.54, 4.85, 
6.87, 3.66, 5.07), `NK cells` = c(90.62, 153.6, 159.8, 88, 118, 
159, 74, 82, 64, 30, 344, 73, 29, 198, 79, 145, 258, 307, 30, 
74.4, 0, 47.3, 32, 0, 52.6, 95.3, 51.7)), row.names = c(NA, -27L
), class = c("tbl_df", "tbl", "data.frame"))


> dput(df)
structure(list(Subject = c("ASCVD002", "ASCVD002", "ASCVD002", 
"ASCVD003", "ASCVD003", "ASCVD003", "ASCVD004", "ASCVD004", "ASCVD004", 
"ASCVD005", "ASCVD005", "ASCVD005", "ASCVD006", "ASCVD006", "ASCVD006", 
"ASCVD008", "ASCVD008", "ASCVD008", "ASCVD009", "ASCVD009", "ASCVD009", 
"ASCVD010", "ASCVD010", "ASCVD010", "ASCVD011", "ASCVD011", "ASCVD011"
), Timepoint = c("0", "0.25", "0.5", "0", "0.25", "0.5", "0", 
"0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", 
"0.5", "0", "0.25", "0.5", "0", "0.25", "0.5", "0", "0.25", "0.5"
), Genotype = c("Heterozygote", "Heterozygote", "Heterozygote", 
"Heterozygote", "Heterozygote", "Heterozygote", "Heterozygote", 
"Heterozygote", "Heterozygote", "GG", "GG", "GG", "AA", "AA", 
"AA", "GG", "GG", "GG", "AA", "AA", "AA", "AA", "AA", "AA", "GG", 
"GG", "GG"), `Tregs CD127lo CD25+` = c(2702, 2175, 2651, 1672.8, 
3762, 4264, 1975, 3208, 3285, 3457, 3383, 2619.9, 11872, 16101, 
13443, 3935, 1894, 2297, 7385, 8901, 9522, 7100, 8789, 9309, 
371, 379, 514), `Monocytes % of Live by Size` = c(1.38, 2.66, 
4.74, 5.83, 3.9, 5.06, 6.36, 3.45, 2.64, 6.33, 10.7, 9.41, 3.42, 
3.46, 2.73, 2.38, 3.12, 4.44, 5.31, 3.59, 4.91, 1.53, 6.54, 4.85, 
6.87, 3.66, 5.07), `NK cells` = c(90.62, 153.6, 159.8, 88, 118, 
159, 74, 82, 64, 30, 344, 73, 29, 198, 79, 145, 258, 307, 30, 
74.4, 0, 47.3, 32, 0, 52.6, 95.3, 51.7)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -27L))
jpsmith
  • 11,023
  • 5
  • 15
  • 36
swats15
  • 15
  • 3
  • I would suggest making this a minimal reproducible example. For instance, if the error is being thrown from the model, we dont need anything related to the ggplot. We also don't have your data to test the error, so providing it would be the first place to start. If you edit your question to be more consistent with the guidelines, you will likely get much better, faster help Good luck! – jpsmith Dec 13 '22 at 14:25
  • As a first time user what is the best way to add my data to my question? – swats15 Dec 13 '22 at 14:32
  • In some cases people use the function `dput` to write the data.frame of their data, but it is often difficult to read through. What I would reccomend doing is trying to generate a tiny data.frame that causes the error. With 5 lines and 3 4 columns max. Then you can place it as readable table here like a markdown table. This might even get you closer to find the error, since you can isolate where it is happening. – JMenezes Dec 13 '22 at 14:41
  • Pasting the output of `dput(df_numeric)` (or a selection of it, such as `dput(df_numeric[1:10,])`) from the console is often preferred because it contains the exact structure of the data, but if the structure is pretty common (ie, an easily reproducible data frame) and its important for folks to _see_ what you want, you could manually create it as well (ie, `df <- data.frame(...)`. I imagine since `df_numeric[[column]]` is the issue you may want to go the `dput()`. See [this link](https://stackoverflow.com/help/minimal-reproducible-example) for more info! – jpsmith Dec 13 '22 at 14:58
  • You should certainly [never post a picture of your data](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) – jpsmith Dec 13 '22 at 16:44
  • I believe this is what was recommended. Thanks for the tips! – swats15 Dec 13 '22 at 18:04

1 Answers1

1

Thank you for providing the dput and code - you are using the df dataset, so you don't really need the df_numeric dataset. To get the names of all your numeric columns, you can use the following code: names(df)[unlist(lapply(df, is.numeric))]. Also you assigned a vector of values to the command dv - it should only be the name of the column.

The below should work for you, it did for me:

for (column in names(df)[unlist(lapply(df, is.numeric))]) {
  res.aov <- rstatix::anova_test(data = df, dv = column, 
                                 wid = `Subject`, within = `Timepoint`, between = `Genotype`)
  rstatix::get_anova_table(res.aov)
}  

Note that in your loop, you are overwriting res.aov with each iteration and you are not storing the results from get_anova_table(res.aov) - I would suggest storing these data in a list:

nnames <- names(df)[unlist(lapply(df, is.numeric))]
res.aov <- list()
aov_tab <- list()
for (column in nnames) {
  res.aov[[column]] <- rstatix::anova_test(data = df, dv = column, 
                                 wid = `Subject`, within = `Timepoint`, between = `Genotype`)
  aov_tab[[column]] <- rstatix::get_anova_table(res.aov[[column]])
}  
jpsmith
  • 11,023
  • 5
  • 15
  • 36