Johannes Radinger
2012-Feb-03 10:07 UTC
[R] Assigning objects to variable and variable to list in a for loop
Hello,
I want to use a for loop for repeadely calculating
a maxent model (package dismo, function maxent()) which
creates an object of the class maxent (S4).
I want to collect all the resulting object in a list.
I tried to simplify my for loop to explain what I want.
There are two problems/questions:
1) How can I create the new variables in the loop (using paste) and assign the
objects
2) How can I collect the results (objects) in a list
X <- factor(c("A","B","C"))
for(in in X){
as.name(paste("result","X",sep="_")) <-
runif(5) #any object
# create list of objects with names
}
I read something about assign(), but that assigns a value and not an object to a
variable. Some time ago I did something similar but with a matrix: Thus I
created an empty matrix before the loop and indexed the matrix inside the loop
to assign values. But here it is about assigning ojects to variables and
coercing these to a list.
Any suggestions are mostly welcomme.
Thank you,
best regards,
Johannes Radinger
--
Joshua Wiley
2012-Feb-03 10:22 UTC
[R] Assigning objects to variable and variable to list in a for loop
Hi Johannes,
There is a relatively elegant solution if you assign in a list:
reslist <- lapply(1:3, function(x) runif(5))
names(reslist) <- paste("result", LETTERS[1:3], sep =
"_")
Cheers,
Josh
On Fri, Feb 3, 2012 at 2:07 AM, Johannes Radinger <JRadinger at gmx.at>
wrote:> Hello,
>
> I want to use a for loop for repeadely calculating
> a maxent model (package dismo, function maxent()) which
> creates an object of the class maxent (S4).
> I want to collect all the resulting object in a list.
>
> I tried to simplify my for loop to explain what I want.
> There are two problems/questions:
> 1) How can I create the new variables in the loop (using paste) and assign
the objects
> 2) How can I collect the results (objects) in a list
>
> X <- factor(c("A","B","C"))
>
> for(in in X){
> ? ? ? ?as.name(paste("result","X",sep="_"))
<- runif(5) #any object
> ? ? ? ?# create list of objects with names
> ? ? ? ?}
>
> I read something about assign(), but that assigns a value and not an object
to a variable. Some time ago I did something similar but with a matrix: Thus I
created an empty matrix before the loop and indexed the matrix inside the loop
to assign values. But here it is about assigning ojects to variables and
coercing these to a list.
>
> Any suggestions are mostly welcomme.
>
> Thank you,
>
> best regards,
> Johannes Radinger
> --
>
> ______________________________________________
> 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.
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/
Johannes Radinger
2012-Feb-03 13:00 UTC
[R] Assigning objects to variable and variable to list in a for loop
Hello,
I tried to use the lapply approach, but I am not sure how to
se it correctly for my task. Here I just want to give an short
script which explains how my data structure looks like. It also
contains the second approach with a for loop which is working but
there is the question of how assining the result to a list.
I think the script is somehow self explaining. Anyway what is called
res is in "reality" rather an object (maxent) than a single value
(thats why I need the list)
#create data
cat <-
c("A","A","B","C","C")
value <- runif(5)
df <- data.frame(cat,value)
# get names of cat with more than 1 entries
select.cat <- names(table(df$cat)[table(df$cat)>1])
cat.list <- as.list(select.cat)
###### lapply approach ####
fun = function(x){
sub.df<- subset(df,cat == x)
# here are other operations, result is an object
res <- sub.df
res #here just a single value but in the long script it is an object
}
reslist <- lapply(cat.list, fun(unlist(cat.list)))
###### For loop approach ####
for(i in select.cat){
sub.df<- subset(df,cat == i)
res <- sub.df
print(res) #here just a single value but in the long script it is an object
#Now I have to collect the results in a list
}
# My task is to do run a function on different parts
#of a dataframe. This dataframe is subdivided with subset on
#one variable.
Thank you very much,
best regards,
Johannes
-------- Original-Nachricht --------> Datum: Fri, 3 Feb 2012 02:22:30 -0800
> Von: Joshua Wiley <jwiley.psych at gmail.com>
> An: Johannes Radinger <JRadinger at gmx.at>
> CC: r-help at r-project.org
> Betreff: Re: [R] Assigning objects to variable and variable to list in a
for loop
> Hi Johannes,
>
> There is a relatively elegant solution if you assign in a list:
>
> reslist <- lapply(1:3, function(x) runif(5))
> names(reslist) <- paste("result", LETTERS[1:3], sep =
"_")
>
> Cheers,
>
> Josh
>
> On Fri, Feb 3, 2012 at 2:07 AM, Johannes Radinger <JRadinger at
gmx.at>
> wrote:
> > Hello,
> >
> > I want to use a for loop for repeadely calculating
> > a maxent model (package dismo, function maxent()) which
> > creates an object of the class maxent (S4).
> > I want to collect all the resulting object in a list.
> >
> > I tried to simplify my for loop to explain what I want.
> > There are two problems/questions:
> > 1) How can I create the new variables in the loop (using paste) and
> assign the objects
> > 2) How can I collect the results (objects) in a list
> >
> > X <- factor(c("A","B","C"))
> >
> > for(in in X){
> > ? ? ?
?as.name(paste("result","X",sep="_")) <-
runif(5) #any object
> > ? ? ? ?# create list of objects with names
> > ? ? ? ?}
> >
> > I read something about assign(), but that assigns a value and not an
> object to a variable. Some time ago I did something similar but with a
matrix:
> Thus I created an empty matrix before the loop and indexed the matrix
> inside the loop to assign values. But here it is about assigning ojects to
> variables and coercing these to a list.
> >
> > Any suggestions are mostly welcomme.
> >
> > Thank you,
> >
> > best regards,
> > Johannes Radinger
> > --
> >
> > ______________________________________________
> > 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.
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/
--
Seemingly Similar Threads
- .jinit() : Cannot create Java virtual machine
- Error .jcall(mxe, "S", "fit", c("autorun", "-e", afn, "-o", dirout, : java.lang.NoSuchMethodError: density.Params.readFromArgs([Ljava/lang/String; )Ljava/lang/String;
- Can not invoke maxent() of library(dismo) in GNU linux
- Save/Load function()-result to file in a loop
- Argument validation within functions