[R] variable-substitution / command-substitution

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 11 14:00:17 CEST 2006


Here are some different approaches:

x <- 1
y <- 2

paste("p", x, y, ".dat", sep = "") # p12.dat

sprintf("p%s%s.dat", x, y)

file.path("a", "b.dat")  # a/b.dat

library(gsubfn)
gsubfn(,,"p$x$y.dat") # p12.dat



On 4/11/06, Damaris Zurell <damaris at zurell.de> wrote:
> Hello,
>
> I have about 2000 data files which I want to analyse.
> The file names are all very similar =>
> p"variable1"_t"variable2"_c"variable3".txt
> There aren´t so much different variables (about 70) , just different
> combinations of them.
> To allow an easy way of handling those data files I was wondering
> whether R is able to do variable-substitution /command-substitution
> similar to shell-programming, so that I could imbed the read.table
> option (and further commands) in 3 loops (one for every variable in the
> filenames) .
>
> e.g.:
> for var1 in (varlist1)
>  for var2 in (varlist2)
>  for var3 in (varlist3)
>   read.table("..../p$var1_t$var2_c$var3.txt")
>   ....statistics......
>  done
>  done
> done
>
>
> Cheers,
> Damaris
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list