An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120801/6ca1567f/attachment.pl>
add vectors to multiple objects
7 messages · John linux-user, David Winsemius, R. Michael Weylandt
On Aug 1, 2012, at 8:11 AM, John linux-user wrote:
Hi everyone,
I try to add many vectors (L1,L2,L3....) to many list objects
(a.list, b.list....) in a workspace. Somethings like below, but it
is not working. Any suggestions will be appreciated. Best, John
lf=ls(pattern=".lst")
for (x in listfiles) {
dat=read.delim(x,header=F)
Presumably that would fail since 'listfiles' has not been defined. did
you mean 'lf'? If you did,then wouldn't the second line overwrite all
the early values of "dat" leaving only the last one?
Perhaps:
datfils <- list()
for (x in listfiles) {
datfils[x] <- read.delim(x,header=F)
for (i in 1: lf) {
And that would fail because 'lf' is a character vector, and it's not
meaningful to specify such a range. Try instead:
for (i in names(datfils[x]) ) {
#
# which will then iterate over the names of the files which are now
also the names of the list elements
assign(i$add,as.numeric(dat[,3]))
But since 'i' is a length-1 character vector, the expression `i$add`
will be meaningless. The "$" operator does not do function calling in
R unless you do fancy things with environments, and you cannot "sub-
assign" in that manner, at least not with the assign() function.
Try instead:
assign(i, as.numeric(datfils[x][,3]))
names(i)[length(i)] <- "add"
Or:
i <- transform(i, add=datfils[x][,3] )
#or i$add=as.numeric(dat[,3]
names(i)[names(i)=="add"]=substr(x,1,5)
I'm not sure these would be doing the same thing. What was your goal here?
print (i[1:3,])
}
}
David Winsemius, MD Alameda, CA, USA
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120803/0c6d8b2c/attachment.pl>
On Aug 3, 2012, at 8:19 AM, John linux-user wrote:
Hi David,
Thanks for response, but my key question still remains unsolved.
That is, how to add many vectors (L1,L2,L3....) to many list objects
(a.list, b.list....) in a workspace?
listObjects=ls(pattern=".list") #for example, a.list, b.list...
for (object in listObjects){
#how to sign vectors L1,L2,..Ln to each object (a.list, b.list...)
}
Any suggestions/comments/ideas will be appreciated.
Can we have code that creates these entities in the workspace? (That is what is meant as a reproducible example.) I cannot tell whether there are already objects named "L1", "L2",... to which you wish to append the value of the objects found, or that you want to assign values to names that only exist as character vectors.
David.
>
> John
>
>
> From: David Winsemius <dwinsemius at comcast.net>
> To: John linux-user <johnlinuxuser at yahoo.com>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Wednesday, August 1, 2012 7:21 PM
> Subject: Re: [R] add vectors to multiple objects
>
>
> On Aug 1, 2012, at 8:11 AM, John linux-user wrote:
>
> > Hi everyone,
> >
> > I try to add many vectors (L1,L2,L3....) to many list objects
> (a.list, b.list....) in a workspace. Somethings like below, but it
> is not working. Any suggestions will be appreciated. Best, John
> >
> > lf=ls(pattern=".lst")
> >
> > for (x in listfiles) {
> > dat=read.delim(x,header=F)
>
> Presumably that would fail since 'listfiles' has not been defined.
> did you mean 'lf'? If you did,then wouldn't the second line
> overwrite all the early values of "dat" leaving only the last one?
>
> Perhaps:
> datfils <- list()
> for (x in listfiles) {
> datfils[x] <- read.delim(x,header=F)
>
>
> >
> > for (i in 1: lf) {
>
> And that would fail because 'lf' is a character vector, and it's not
> meaningful to specify such a range. Try instead:
>
> for (i in names(datfils[x]) ) {
> #
> # which will then iterate over the names of the files which are now
> also the names of the list elements
>
> > assign(i$add,as.numeric(dat[,3]))
>
> But since 'i' is a length-1 character vector, the expression `i$add`
> will be meaningless. The "$" operator does not do function calling
> in R unless you do fancy things with environments, and you cannot
> "sub-assign" in that manner, at least not with the assign() function.
>
> Try instead:
>
> assign(i, as.numeric(datfils[x][,3]))
> names(i)[length(i)] <- "add"
>
> Or:
> i <- transform(i, add=datfils[x][,3] )
>
>
>
> > #or i$add=as.numeric(dat[,3]
> > names(i)[names(i)=="add"]=substr(x,1,5)
>
> I'm not sure these would be doing the same thing. What was your goal
> here?
>
> >
> > print (i[1:3,])
> > }
> > }
>
>
> David Winsemius, MD
> Alameda, CA, USA
>
>
>
David Winsemius, MD
Alameda, CA, USA
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120803/79c0065c/attachment.pl>
On Fri, Aug 3, 2012 at 2:53 PM, John linux-user <johnlinuxuser at yahoo.com> wrote:
Maybe what I previously posted was not clear enough or something else. All vectors L1,L2.. and objects (e.g. a.list, b.list, c.list) already exit or easily to be created in a workspace. Do not worry about those. The key question is how to systematically append/assign these vectors to many objects (hundreds) in a loop without explicitly typing these object names one by one. Please refer to my code posted below to detail my question. I really appreciate your ideas/suggestions. John
Like David, I find this still somewhat confusing, but perhaps this
will get you started:
a3 <- 1:4
b3 <- 6
c3 <- letters
a_new_thing <- do.call(list, lapply(ls(pattern = "3"), get))
old_thing <- list("cow")
a_longer_old_thing <- c(old_thing, a_new_thing)
adapt as needed.
Best,
Michael
On Aug 3, 2012, at 2:11 PM, R. Michael Weylandt wrote:
On Fri, Aug 3, 2012 at 2:53 PM, John linux-user <johnlinuxuser at yahoo.com
wrote: Maybe what I previously posted was not clear enough or something else. All vectors L1,L2.. and objects (e.g. a.list, b.list, c.list) already exit or easily to be created in a workspace. Do not worry about those. The key question is how to systematically append/ assign these vectors to many objects (hundreds) in a loop without explicitly typing these object names one by one. Please refer to my code posted below to detail my question. I really appreciate your ideas/suggestions. John
Like David, I find this still somewhat confusing, but perhaps this
will get you started:
a3 <- 1:4
b3 <- 6
c3 <- letters
a_new_thing <- do.call(list, lapply(ls(pattern = "3"), get))
old_thing <- list("cow")
a_longer_old_thing <- c(old_thing, a_new_thing)
adapt as needed.
Heh. Users of this should realize that Michael probably did this in
clean session. If you run it with any other items in the workspace
that have "3" in their names they get wrapped up too. I had a 300
line dataframe as well as a function whose values both got gathered up.
In the case where one needed to isolate a disjoint group of items, one
might want to create an environment in which to segregate those items.
the ls function can be directed to look in only one environment.
I wondered if this were the route intended:
for( i in seq_along(ls(patt=".lst") ) ) {
assign( ls(patt=".lst")[i], # the name of the i-th ".lst"-
item
append(
sapply(ls(patt=".lst$"), get)[i], # the
original value
sapply(ls(patt="^L\\d"), get)[i] ) )} # the
appended i-th "L"-value
I think it is far from optimal to have a loose collection of objects
in the workspace. Much better to have them all in one list or an
environment.
David Winsemius, MD Alameda, CA, USA