[R] Extracting list names within a looped function

Ista Zahn istazahn at gmail.com
Thu Nov 15 16:40:18 CET 2012


Hi Stan,

On Thu, Nov 15, 2012 at 6:02 AM, Gaj Stan (BIGCAT)
<stan.gaj at maastrichtuniversity.nl> wrote:
> Hello Berend,
>
> Thanks for your quick response.
>
> I am aware of the names() function, but this is not the answer to my question, since it will not work within the foo function.
>
> Let me try to explain it again! (:
>
> I want to analyze several datasets in a similar fashion and stored all data as seperate list components in a (here: $var1, $var2, $var3). Each component of that list has a unique name and will be sent through the foo()-function using a for-loop. This foo() function would (in my case) generate several plots and output files where I would like to use the name of the component ($var1, $var2 or $var3) in the labels and output file names. I currently am unable to do so.
>
> As such, my end-goal would be that the foo function returns the name of the list components and not [1] "a[[i]]", as deparse(substitute(x)) does.
>
> The output of foo() needs to be:
>
>  [1] "var1"
>  [1] "var2"
>  [1] "var3"

I suggest iterating over the names themselves, like this:

for(i in names(a)) {
 print(i)
}
[1] "var1"
[1] "var2"
[1] "var3

If you define your function with two arguments you can do stuff like this:

foo <- function(x, d) {
  print(x)
  cat(d[[x]], file=paste(x, ".txt", sep=""))
  ## other stuff you want to do with d and x
}

for(i in names(a)) {
 foo(x=i, d=a)
}

which is (I think) basically what you are after.

Best,
Ista

>
> But I'm also happy with
>  [1] "a[[\"var1\"]]
>  [1] "a[[\"var2\"]]
>  [1] "a[[\"var3\"]]
> (or something similar)
>
> This leads to the question: how can I adjust my foo() so it gives me the output mentioned above?
>
> I hope that this clears things up.
>
>   -- Stan
>
>
> -----Original Message-----
> From: Berend Hasselman [mailto:bhh at xs4all.nl]
> Sent: 15 November 2012 11:27
> To: Gaj Stan (BIGCAT)
> Cc: r-help at r-project.org
> Subject: Re: [R] Extracting list names within a looped function
>
>
> On 15-11-2012, at 11:14, Gaj Stan (BIGCAT) wrote:
>
>> Hello all,
>>
>> I have the following problem:
>>
>> 1) A list was defined as 'a'
>>
>> a <- list("var1"=c(100:1), "var2"=c(1:100), "var3"=rnorm(100))
>>
>> 2) a function 'foo' was defined that extracts the variable name assigned to x using the deparse(substitute()) functionality. This name will then be used within the function to generate specific output files, etc.
>>
>> foo <- function(x) {
>>  print( deparse(substitute(x)) )
>> }
>>
>> However, I am currently interested in looping through all list variables and extract the list variable name from within the function. The current loop (see below) will result in
>>
>> for(i in 1:length(a)) {
>>  foo(a[[i]])
>> }
>> [1] "a[[i]]"
>>
>> which actually does what I expected of deparse(substitute(x)), but is not what I wanted. I would like to end up with something like
>>
>> [1] "var1"
>> [1] "var2"
>> [1] "var3"
>>
>> or
>>
>> [1] "a[[\"var1\"]]"
>> [1] "a[[\"var2\"]]"
>> [1] "a[[\"var3\"]]"
>>
>> Keep in mind that x has to be a matrix, and not a list. This to keep the function as general as possible.
>>
>> Does anyone have any idea on how to tackle this? Is deparse(substitute(x)) here the best way to go? Are there alternatives?
>
> I'm not sure if I understand what you want but will this give you what you seem to want:
>
> names(a)
>
> Berend
>
> ______________________________________________
> 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.




More information about the R-help mailing list