Hi all, I have a very simple problem that I cannot seem to find the answer to. Consider the following toy dataset: x <- read.table(textConnection("V1 apples bananas cherries indv.1 7 8 4 3 indv.2 7 7 4 9"), header = TRUE) How would I change the column name of ONLY the first column, not the others? Surely I should not have to re-specify the names of ALL the columns -- e.g., colnames(x) <- c("pears", "apples", "bananas", "cherries") -- if only the first column name will change. Thanks very much, Josh [[alternative HTML version deleted]]
Try: names(x)[1] <- 'pears' On Wed, Jun 9, 2010 at 2:45 PM, Josh B <joshb41@yahoo.com> wrote:> Hi all, > > I have a very simple problem that I cannot seem to find the answer to. > Consider the following toy dataset: > > x <- read.table(textConnection("V1 apples bananas cherries > indv.1 7 8 4 3 > indv.2 7 7 4 9"), header = TRUE) > > How would I change the column name of ONLY the first column, not the > others? Surely I should not have to re-specify the names of ALL the columns > -- e.g., colnames(x) <- c("pears", "apples", "bananas", "cherries") -- if > only the first column name will change. > > Thanks very much, > Josh > > > > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi Josh, Here is a suggestion: colnames(x)[1] <- 'mynewname' colnames(x) # [1] "mynewname" "apples" "bananas" "cherries" HTH, Jorge On Wed, Jun 9, 2010 at 1:45 PM, Josh B <> wrote:> Hi all, > > I have a very simple problem that I cannot seem to find the answer to. > Consider the following toy dataset: > > x <- read.table(textConnection("V1 apples bananas cherries > indv.1 7 8 4 3 > indv.2 7 7 4 9"), header = TRUE) > > How would I change the column name of ONLY the first column, not the > others? Surely I should not have to re-specify the names of ALL the columns > -- e.g., colnames(x) <- c("pears", "apples", "bananas", "cherries") -- if > only the first column name will change. > > Thanks very much, > Josh > > > > > [[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. >[[alternative HTML version deleted]]