Does the following do what you want?
> d <- list() # empty list
> for(i in 1:8) if (i %% 2 == 0) {
newElement <- structure(list(i), names=LETTERS[i])
d <- c(d, newElement)
}
> str(d)
List of 4
$ B: int 2
$ D: int 4
$ F: int 6
$ H: int 8
Bill Dunlap
TIBCO Software
wdunlap tibco.c
Your explanation of the problem is unclear and your use of HTML formatting
corrupts your code examples.
One issue is your reference to a loop while showing no loop nor input nor
output. There is more than one construct for iterating in R. One problem you
may be encountering is that you cannot c
t;d"
#variable el
> el<-"a"
#result
> d[[el]]<-"whatever I want to put under name a"
> d
$a
[1] "whatever I want to put under name a"
$b
[1] "a"
$c
[1] "e"
$d
[1] "b"
$e
[1] "d"
Cheers
Petr
From: carol w
white [mailto:wht_...@yahoo.com]
Sent: Friday, June 05, 2015 11:51 AM
To: PIKAL Petr
Subject: Re: [R] building a list in a loop for (i in c("a","b")) best, On
Friday, June 5, 2015 11:36 AM, PIKAL Petr wrote: Hi
Can you please specify how loop without index shall be constructe
arol white [mailto:wht_...@yahoo.com]
Sent: Friday, June 05, 2015 11:51 AM
To: PIKAL Petr
Subject: Re: [R] building a list in a loop for (i in c("a","b")) best, On
Friday, June 5, 2015 11:36 AM, PIKAL Petr wrote: Hi
Can you please specify how loop without index shall be const
t(a=NULL, b=NULL)
for (i in c("a","b")) {
d[[i]]<-1:3
}
Cheers
Petr
From: carol white [mailto:wht_...@yahoo.com]
Sent: Friday, June 05, 2015 11:51 AM
To: PIKAL Petr
Subject: Re: [R] building a list in a loop
for (i in c("a","b"))
best,
On Friday, Jun
Hi
Can you please specify how loop without index shall be constructed? It is
rather new topic for me.
Petr
From: carol white [mailto:wht_...@yahoo.com]
Sent: Friday, June 05, 2015 11:21 AM
To: PIKAL Petr
Subject: Re: [R] building a list in a loop
also consider a loop without index (not like
rm(1)
fit<-lm(y~s)
d[[i]]<-fit
names(d)[i] <- paste(letters[round(mean(s))],i, sep="")
}
So my opinion is that the code for populating list is pretty general.
Petr
From: carol white [mailto:wht_...@yahoo.com]
Sent: Friday, June 05, 2015 11:18 AM
To: PIKAL Petr
Subject: Re: [R]
It might be an easy question but how to construct correctly a list in a loop?
The following doesn't work
before starting the loopd = NULL#in the loop, 1st iteration
d = list(d,c(1,2,3)d[[1]]
NULL
[[2]]
[1] 1 2 3#in the loop, 2nd iterationd=list(d,c(4,5,6)d
[[1]]
[[1]][[1]]
NULL
[[1]][[2]]
[1] 1
9 matches
Mail list logo