If the columns are in order you can just past in the new names:
Dat <- read.table(textConnection("name x y name1 x1 y1
test1 1 3 test2 4 4
test1 2 2 test2 5 5
test1 3 1 test2 6 6"), header=TRUE)
closeAllConnections()
x.vars <- grep("x", names(Dat))
y.vars <- grep("y", names(Dat))
names.vars <- grep("name", names(Dat))
names(Dat)[x.vars] <- paste("x-test", 1:length(x.vars),
sep="")
names(Dat)[y.vars] <- paste("y-test", 1:length(y.vars),
sep="")
Dat <- Dat[, -(names.vars)]
-Ista
On Mon, Apr 5, 2010 at 9:09 PM, jda <shrike224@hotmail.com> wrote:
>
> Hi folks,
>
> I have imported data from an Excel spreadsheet. Columns in that
> spreadsheet
> are named "name", "x", and "y", and several
sets of those columns appear in
> the worksheet. For example:
>
> name x y name x y
> test1 1 3 test2 4 4
> test1 2 2 test2 5 5
> test1 3 1 test2 6 6
>
> When I import these data into R, into a dataframe, I end up with something
> like this:
>
> name x y name1 x1 y1
> 1 test1 1 3 test2 4 4
> 2 test1 2 2 test2 5 5
> 3 test1 3 1 test2 6 6
>
> I -cannot- change the excel file, and must work with the data and labels as
> they appear in R. I would like to end up with a dataframe that looks more
> like this:
>
> x-test1 y-test1 x-test2 y-test2
> 1 1 3 4 4
> 2 2 2 5 5
> 3 3 1 6 6
>
> I believe this involves renaming the dataframe's columns using
information
> found within other columns of the dataframe. It subsequently involves
> deleting some of the (now-redundant) columns. If anyone could offer some
> helpful hints, I would be very appreciative! Thank you in advance,
> regardless of reply, for taking the time to read through my plea.
>
> -jda
> --
> View this message in context:
>
http://n4.nabble.com/changing-column-names-in-a-dataframe-tp1752034p1752034.html
> Sent from the R help mailing list archive at Nabble.com.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
[[alternative HTML version deleted]]