Search code examples
rpathoperatorsrelational

R path with multiple variables


I have a handful of directories and files in this format

/home/scratch/test/123/local/test.vcf.gz
/home/scratch/test/456/local/test.vcf.gz
/home/scratch/test/789/local/test.vcf.gz

I have a snippet of code that basically points to these directories and combines all the test.vcf.gz into Rdata but this part of the code throws an error

dat = read_csv("input.csv") %>% 
  gather(pipeline, id,  `SRS`) %>% 
  mutate(pipeline_clean = c("SRS" = "main")[pipeline]) %>% 
  mutate(output = sprintf("/home/scratch/test/%s/local/%s", id)) %>% 
  mutate(Group.Id = sprintf("%s_%s_%s_%s", prep, platform, pipeline_clean, ref))

Error: Problem with `mutate()` input `output`.
x too few arguments
ℹ Input `output` is `sprintf(...)`.

But when I change the output directory to /home/scratch/test/123/local/%s, it does not throw any error. I assumed I could provide a path for multiple variables with %s, any suggestion would be helpful.

thankyou


Solution

  • If we want to interpolate the same value multiple times i.e. %s is repeated twice, we need to repeat the arguments in sprintf

    ...
      mutate(output = sprintf("/home/scratch/test/%s/local/%s", id, id)) %>%
    ...
    

    If the suffix is always test.vcf.gz, then we can remove the last %s

      mutate(output = sprintf("/home/scratch/test/%s/local/test.vcf.gz", id))