0

I'm quite new in R. I have a function that uses 3 data frames to generate reports (basically do.reports does a couple does some calculation among the tables and merged them.

create_report = do_reports(df1, df2, df3)

ID = c("ID1", "ID2", "ID3")
size = c(2, 5, 4)
p = c(0.05, 0.1, 0.3)
var = c(0.3, 0.5, 1.2)
location = c("blood", "tissue", "blood")

df1 = data.frame(ID  = ID , size= size, p = p)
df2 = data.frame(ID  = ID , var = var)
df3 = data.frame(ID  = ID , location = location)

The output of this consists in 2 different objects ( create_report[1], create_report[2])

#expected create_report[1]

create_report[1] = data.frame(ID  = ID , location = location, size = size , p=p)

 #expected create_report[2]

create_report[2] = data.frame(location = location, size = size , p=p, var=var)

However, I would like to use do_reports row by row in df2 (something like create_report = do_reports(df1, df2[1,], df3), save the results independently and once finishes, merge all of them in a table. What I've tried so far is:

for (i in seq_len(nrow(df2) {
  create_report = do_reports(df1, df2, df3)
}

I know that using a loop over it could be very tedious, so I was wondering if someone in here could help me with this.

Thank you

  • 1
    Welcome to SO, Lila McPhee! Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Nov 09 '21 at 15:02
  • thank you for the comment, is now clearer? – Lila McPhee Nov 09 '21 at 15:22

1 Answers1

0
library(tidyverse)
ID <- c("ID1", "ID2", "ID3")
size <- c(2, 5, 4)
p <- c(0.05, 0.1, 0.3)
var <- c(0.3, 0.5, 1.2)
location <- c("blood", "tissue", "blood")

df1 <- data.frame(ID = ID, size = size, p = p)
df2 <- data.frame(ID = ID, var = var)
df3 <- data.frame(ID = ID, location = location)


do_report <- function(ID, size, p, var, location) {
  # create some result
  result <- "test"
  
  write_rds(result, file = tempfile())
}

list(df1, df2, df3) %>%
  reduce(full_join) %>%
  mutate(result = list(ID, size, p, var, location) %>% pmap(do_report))
danlooo
  • 10,067
  • 2
  • 8
  • 22
  • I think you have misunderstood my question. What I want to do is to pass the function do_reports row by row in df2 (something like create_report = do_reports(df1, df2[1,], df3), save the results independently and once finishes, merge all of them in a table. I can do it with a loop but is not very efficient. – Lila McPhee Nov 09 '21 at 15:45
  • You have not provided functions `do.reports` or `do_report` as mentioned in the question. Does saving means writing the result to files? – danlooo Nov 09 '21 at 15:50
  • yes, I mean writing the result for each row (or save them independently) and combined them in a df at the end – Lila McPhee Nov 09 '21 at 15:52
  • `list(df1, df2, df3) %>% lapply(do_report)` can be used instead of a for loop to apply teh function `do_report` on every element of the list – danlooo Nov 09 '21 at 15:52
  • but I want to pass it row by row as I saw in the `for` loop – Lila McPhee Nov 09 '21 at 15:58
  • I revised my answer, This produced one file per ID. Input arguments of the reporting func are still missing. – danlooo Nov 09 '21 at 15:58
  • According to the question, the for loop will create three identical files. – danlooo Nov 09 '21 at 16:01