Skip to content
Prev 302050 / 398503 Next

add vectors to multiple objects

On Aug 1, 2012, at 8:11 AM, John linux-user wrote:

            
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)
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
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] )
I'm not sure these would be doing the same thing. What was your goal  
here?
David Winsemius, MD
Alameda, CA, USA