0

Given the data.frame df obtained within the function myfunc, how can I add some more columns to df within the function?

It works if done manually outside the function:

require("broom")

myfunct <- function(mylabel1, mylabel2){

  df <- broom::glance(t.test(extra ~ group, data = sleep))

  # df$lab1 <- mylabel1
  # df$lab2 <- mylabel2
}

result <- myfunct(mylabel1 = "AA", mylabel2 = "BB")
mylabel1 = "AA"
mylabel2 = "BB"
result$lab1 <- mylabel1
result$lab2 <- mylabel2

> result
  estimate estimate1 estimate2 statistic    p.value parameter  conf.low conf.high                  method alternative lab1 lab2
1    -1.58      0.75      2.33 -1.860813 0.07939414  17.77647 -3.365483 0.2054832 Welch Two Sample t-test   two.sided   AA   BB

But it does not when creating the new variables within myfunc

myfunct <- function(mylabel1, mylabel2){

  df <- broom::glance(t.test(extra ~ group, data = sleep))

  df$lab1 <- mylabel1
  df$lab2 <- mylabel2
}

result <- myfunct(mylabel1 = "AA", mylabel2 = "BB")

> result
[1] "BB"

PD: This is a minimal reproducible example of the issue I'm facing with a more complex function comprising multiple results.

AJMA
  • 1,134
  • 2
  • 13
  • 28

0 Answers0