Home > database >  Why and how does R distinguish between two constructions of a file name?
Why and how does R distinguish between two constructions of a file name?

Time:06-21

I'm trying to dynamically create names for files that I need to read into R. I'm getting unexpected results because my two ways of constructing the file name, below, are yielding different results, even though the character vectors containing the file names seem to be the same, yet R treats them differently.

Can you help me to understand why this is? How should I be creating (dynamically) file names in R?

phe_num <- 1
cross_num <- 1
chr_num <- 1
pfn <- paste0("pred_ukb_pheno", 
                   phe_num, 
                   "_fold", 
                   cross_num, 
                   "_chr", 
                   chr_num, 
                   "_best.dblsmm.txt.profile")
profile_fn <- file.path("/net/mulan/home/fredboe/research/ukb-intervals/dat", "simulations-ding", "DBSLMM", pfn)
file.exists(profile_fn)
#> [1] FALSE
profile_fn
#> [1] "/net/mulan/home/fredboe/research/ukb-intervals/dat/simulations-ding/DBSLMM/pred_ukb_pheno1_fold1_chr1_best.dblsmm.txt.profile"
my_file <- "/net/mulan/home/fredboe/research/ukb-intervals/dat/simulations-ding/DBSLMM/pred_ukb_pheno1_fold1_chr1_best.dbslmm.txt.profile"
file.exists(my_file)
#> [1] TRUE
my_file == profile_fn
#> [1] FALSE

Created on 2022-06-20 by the reprex package (v2.0.1)

I'm using R 4.2.0 on Ubuntu 22.04 operating system.

CodePudding user response:

Your cue was that

my_file == profile_fn
#> [1] FALSE

which gives you everything to know why one works and the other does not. They differ after all!.

A little bit of eyeballing reveals

>  substr(my_file, 108, 113)
[1] "dbslmm"
>  substr(profile_fn, 108, 113)
[1] "dblsmm"
>

so you have a simple typo as ls != sl.

We've all been there. Computers are deterministic and 'do what I say' instead of 'do what I mean'.

  • Related