0
    p_values=''
    s <- 0
    tab <- map_df(list(result),tidy(for(c in colnames(ordered_gene)[5:ncol(ordered_gene)]){
    result=t.test(as.numeric(ordered_gene[which(ordered_gene$group=="high"),c]),as.numeric(ordered_gene[which(ordered_gene$group=="low"),c]));
    na.omit(result);
    p_values <- c(p_values,result$p);
    s <- s + 1
    }))

The aim of the code above is supposed to print out all the Welch Two Sample t-test results (from the high & low gene groups) and display them as an organized table that lists out the p-value, df, and t value as organized columns of those 2 groups. But now, it keeps showing the error of: Can't convert a tbl_df/tbl/data.frame object to function.

What do I do? Thanks!

Limey
  • 10,234
  • 2
  • 12
  • 32
JustaCoder
  • 23
  • 5
  • 2
    Welcome to SO, JustaCoder! It really helps when questions are reproducible, including sufficient sample data to be able to execute the code in question. It is also strongly urged to explicitly list non-base R packages you are using; in this case, perhaps it's `generics::tidy`, I'm not certain. Please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. Thanks, and good luck! – r2evans Jan 17 '22 at 17:18
  • 1
    I suspect the fundamental issue is the inclusion of a `for` loop as a parameter to `tidy`... With problems like this, a good approach is to break the problem down: does the code work for one pair of columns. If not, fix that. if it does, what aspect of the loop causes the failure. Etc, etc... – Limey Jan 17 '22 at 17:24

1 Answers1

0

Two approaches, based on the tidyverse.

To begin, generate some data as the OP has not provided any test data. From the code sample shown, it seems we have a data.frame in wide format, but we don't know how the columns are named.

library(tidyverse)
library(broom)

ordered_gene <- tibble(
  Group=rep(c("low", "medium", "high"), each=5),
  Var1=rnorm(15),
  Var2=rnorm(15),
  Var3=rnorm(15)
)
> ordered_gene
# A tibble: 15 × 4
   Group    Var1    Var2   Var3
   <chr>   <dbl>   <dbl>  <dbl>
 1 low    -1.21  -0.212   0.792
 2 low     0.674  0.0245  2.05 
 3 low     0.977 -0.792  -0.519
 4 low    -1.21  -0.687   0.504
 5 low    -0.478 -0.864   0.620
 6 medium  1.87  -1.44   -1.16 
 7 medium -1.41   0.325   0.687
 8 medium  2.72   0.230  -0.887
 9 medium  1.72   0.493  -0.346
10 medium  0.752 -1.86    1.11 
11 high   -0.560 -1.08   -0.376
12 high   -0.819  1.44   -0.906
13 high    0.603  0.0608 -0.704
14 high    0.713  2.59   -0.449
15 high    0.128  1.88    1.13 
> 

The first approach is very awkward. The awkwardness is caused by the fact that the input dataset appears not to be tidy. The second tidies the data and then performs the same analyses, but much more simply.

First approach

lapply(
  # For each column in ordered_gene, other than Group...
  names(ordered_gene %>% select(-Group)),
  function(col) {
    ordered_gene %>% 
    # Keep only the data relating to rowes where group is 'low' or 'high'
    filter(Group %in% c("low", "high")) %>% 
    # Apply the following function
    group_map(
      function(.x, .y) {
        # Convert the output of t.test to a data.frame
        tidy(
          # Perform the t.test using the 'formula' version of the function
          t.test(as.formula(paste0(".x$", col, " ~ .x$Group")))
        ) %>% 
        # Identify the column being analysed
        add_column(Var=col, .before=1)
      } 
    )
  }
) %>% 
# Bind the list of tibbles returned by lapply into a single tibble
bind_rows()
# A tibble: 3 × 11
  Var   estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method                  alternative
  <chr>    <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl> <chr>                   <chr>      
1 Var1     0.263    0.0130    -0.250     0.475  0.649       6.95   -1.05      1.57  Welch Two Sample t-test two.sided  
2 Var2     1.48     0.978     -0.506     2.17   0.0870      4.56   -0.323     3.29  Welch Two Sample t-test two.sided  
3 Var3    -0.951   -0.260      0.690    -1.74   0.121       7.87   -2.22      0.314 Welch Two Sample t-test two.sided  

Second approach

# Tidy the data
tidy_gene <- ordered_gene %>% 
               pivot_longer(
                 !Group,
                 names_to="Var",
                 values_to="Value"
               )
tidy_gene
# A tibble: 45 × 3
   Group Var     Value
   <chr> <chr>   <dbl>
 1 low   Var1  -1.21  
 2 low   Var2  -0.212 
 3 low   Var3   0.792 
 4 low   Var1   0.674 
 5 low   Var2   0.0245
 6 low   Var3   2.05  
 7 low   Var1   0.977 
 8 low   Var2  -0.792 
 9 low   Var3  -0.519 
10 low   Var1  -1.21  
# … with 35 more rows

tidy_gene %>% 
  # For each variable
  group_by(Var) %>% 
  # Select only rows where Group is 'low' or 'high'
  filter(Group %in% c("low", "high")) %>% 
  # Perform the t-test
  group_modify(~tidy(t.test(as.formula(".x$Value ~ .x$Group"))))
# A tibble: 3 × 11
# Groups:   Var [3]
  Var   estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method                  alternative
  <chr>    <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl> <chr>                   <chr>      
1 Var1     0.263    0.0130    -0.250     0.475  0.649       6.95   -1.05      1.57  Welch Two Sample t-test two.sided  
2 Var2     1.48     0.978     -0.506     2.17   0.0870      4.56   -0.323     3.29  Welch Two Sample t-test two.sided  
3 Var3    -0.951   -0.260      0.690    -1.74   0.121       7.87   -2.22      0.314 Welch Two Sample t-test two.sided  

The two approaches produce identical results

Limey
  • 10,234
  • 2
  • 12
  • 32