Hi, How to turn a character string into an unevaluated R object? I want to load some files in a directory into data matrix R objects. I could do this with read.table and assign (see below). Then, I want to turn the character string representing a file name (the evaluated expression of i) into an unevaluated R object. Basically, I want to create matrices whose names are the same as the related file names. Chees, Carol ---------------------------- suppose I have mat1, mat2, mat3 files v = list.files(".", all.files = F, pattern = "mat") for (i in v) assign(i, read.table(i, sep = "\t"), pos = 1) then, I want to have 3 objects with the names mat1, mat2, mat3 containing what mat1, mat2, mat3 files contain.
On 02/03/2010 5:42 PM, carol white wrote:> Hi, > How to turn a character string into an unevaluated R object? I want to load some files in a directory into data matrix R objects. I could do this with read.table and assign (see below). Then, I want to turn the character string representing a file name (the evaluated expression of i) into an unevaluated R object. Basically, I want to create matrices whose names are the same as the related file names.I don't understand what you mean by an "unevaluated R object". The code you give below looks as though it would do the rest of what you describe; what goes wrong with it?> Chees, > > Carol > ---------------------------- > suppose I have > > mat1, mat2, mat3 files > > v = list.files(".", all.files = F, pattern = "mat") > for (i in v) > assign(i, read.table(i, sep = "\t"), pos = 1)I would leave off "pos = 1" here, though it should be harmless. It will only make a difference if you execute this code inside a function or other local environment. Duncan Murdoch> > then, I want to have 3 objects with the names mat1, mat2, mat3 containing what mat1, mat2, mat3 files contain. > > ______________________________________________ > R-help at r-project.org 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.
On 3/2/10, carol white <wht_crl at yahoo.com> wrote:> How to turn a character string into an unevaluated R object? I want to load some >I'm not sure if this is what you're looking for:> as.name("iris")iris> parse(text="iris")expression(iris) attr(,"srcfile") <text>> head(eval(as.name("iris")))Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa> head(eval(parse(text="iris")))Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa Liviu