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
[[alternative text/enriched version deleted]]
Consider the following:
> changeNames <- function(dfName,
+ newNames=c("A1", "B1"), pos.=1){
+ DF. <- get(dfName)
+ names(DF.) <- newNames
+ assign(dfName, DF., pos=pos.)
+ "done"
+ }
>
> DF <- data.frame(a=1, b=2)
> changeNames("DF")
[1] "done"
> DF
A1 B1
1 1 2
>
There's probably a way to do this with "do.call", but the
above
seems to work.
hope this helps. spencer graves
thomas hills wrote:
>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
>
>
>
> [[alternative text/enriched version deleted]]
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
>
--
Spencer Graves, PhD, Senior Development Engineer
O: (408)938-4420; mobile: (408)655-4567
thomas hills <thills at mail.utexas.edu> writes:> 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.assign() takes a character string as its first argument, so you probably now have a variable called "Tc45w4.V1" (it's a semi-bug that it doesn't barf when passed a character vector of length > 1). What you seem to want is for(i in 1:length(filenames)) { t <- get(filenames[i]) names(t) <- c("name", "infood", "time") assign(filenames[i], t) } It is easier with a list for (i in seq(along=listoftables)) names(listoftables[[i]]) <- c("name", "infood", "time") but *not* "for (i in listoftables) names(i) <- ..." because you'd be changing the names of a copy of the actual elements. I suspect the jury is still out on whether it is good programming practice to do listoftables <- lapply(listoftables, "names<-", c("name", "infood", "time")) but it is tempting in cases like this. (The awkward bit is that for performance reasons, assignment functions -- names<-, [<-, etc. -- might not leave their argument unchanged.) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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
Possibly Parallel Threads
- read.table from a list of filenames
- Barchart in lattice - wrong order of groups, data labels on top of each other, and a legend question
- sqldf: issues with natural joins
- using C code to create data frame but always return as list
- Add grid lines to levelplot