[R] reading and storing files in the workspace

jim holtman jholtman at gmail.com
Tue Jul 31 21:30:27 CEST 2007


try:

for (i in test){
    assign(gsub(".txt$", "", i), read.table(i, header=TRUE))
}

On 7/31/07, Luis Ridao Cruz <Luisr at frs.fo> wrote:
> R-help,
>
> I have a vector containing (test) some file names.
> The files contents are matrixes.
>
> > test
>
>  [1] "aaOki.txt"    "aOki.txt"     "bOki.txt"     "c1Oki.txt"
> "c2Oki.txt"    "c3Oki.txt"    "cOki.txt"     "dOki.txt"     "dyp100.txt"
>  "dyp200.txt"
> [11] "dyp300.txt"   "dyp400.txt"   "dyp500.txt"   "dyp600.txt"
> "dyp700.txt"   "dyp800.txt"   "eOki.txt"     "FBdyp100.txt"
> "FBdyp150.txt" "FBdyp200.txt".............
>
> What I want to do is to import to R using the same file name
> and remove the ".txt" extension out of the object name.
> Something like this:
>
> for(i in test)
> gsub("\\.", "", paste(i, sep = "")) <- read.table(file = paste(i, sep =
> ""), header = TRUE)
>
> But I get the following message:
>
> Error in gsub("\\.", "", paste(i, sep = "")) <- read.table(file =
> paste(i,  :
>        target of assignment expands to non-language object
>
>
> Thanks in advance.
>
>
> > version
>               _
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status
> major          2
> minor          5.1
> year           2007
> month          06
> day            27
> svn rev        42083
> language       R
> version.string R version 2.5.1 (2007-06-27)
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list