[R] Calling variables dynamically [Broadcast]

Liaw, Andy andy_liaw at merck.com
Sat Apr 15 01:58:58 CEST 2006


1. The FAQ entry advise you to use a list, instead of using assign() in a
loop.  Have you considered that?

2. See ?get.

Andy

From: kewley at eden.rutgers.edu
> 
> I've looked in the online documentation for this, but have 
> been unable to find an answer. I can name variables 
> dynamically, but I cannot call them dynamically. Either a 
> direct answer or a reference to an online resource that has 
> the answer would be great.
> 
> I'm trying to write a script that imports and compares 
> results from various IR (information retrieval systems). Each 
> imported file will contain results for multiple (50+) 
> queries, each query can have 2000+ results.
> 
> My immediate task is to import the files and assign the 
> scores for each query to its own variable (e.g. File1-Query1, 
> File1-Query2, etc)
> 
> I've written code that reads in the data files and enters 
> them as variables:
> 
> res.paths <-file.path(choose.files("c:/res/*.*"))
> num.files<- NROW(res.paths)
> for (j in 1: num.files) {
> 	assign(paste("res",j, sep=""), read.table(res.paths[j]))
> This gives me variables named res1, res2, etc, one for each 
> file. So far so good.
> 
> But the next line of code is designed to find out how many 
> unique values are in the first column (i.e. number of 
> queries) of the file I just
> imported:
> 
> assign(paste("res.count",j, sep="_"), NROW(unique(res[j]$V1)))
> 	}
> 
> What I expected is a variable named e.g. res.count_1. 
> However, I get an
> error:
> Error in unique(res[j]$V1) : object "res" not found
> 
> How can I dynamically call res1, res2, etc? I've also tried 
> using quotes and paste, but with no success.
> 
> Any help is appreciated.
> 
> Andrew Noyes
> SCILS
> Rutgers University
> 
> ______________________________________________
> 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