[R] using get() in assign()

Gabor Grothendieck ggrothendieck at myway.com
Wed Dec 29 13:00:05 CET 2004



Its probably easier to keep your data frames in a list 
and to just specify the col.names= argument on 
read.table when you read them in:

cn <- c("name", "infood", "time")
L <- sapply(filenames, read.table, col.names = cn, simplify = FALSE)

You can add other read.table arguments, if you need them, 
in the same way that col.names= was added above.

Note that we used sapply(...whatever..., simplify = FALSE)
instead of lapply since that sapply will label the components
of list L with the filenames whereas a lapply will not.  


Date:   Tue, 28 Dec 2004 21:27:21 -0600 
From:   thomas hills <thills at mail.utexas.edu>
To:   <r-help at stat.math.ethz.ch> 
Subject:   [R] using get() in assign() 

 
I'm trying to rename the columns in a list of data.frames using the 
following...

for(i in 1:length(filenames)) {
assign(names(get(filenames[i])), c("name", "infood", "time") ) }

R returns no errors, but the names are unchanged in the data.frames.

The original names were things like

> names(get(filenames[2]))
[1] "Tc45w4.V1" "Tc45w4.V2" "Tc45w4.V3"

after the above procedure they are still those names.

Ideas appreciated. Thanks.

Thomas




More information about the R-help mailing list