histelheim
histelheim

Reputation: 5098

Editing column names of a CSV file in R

I have a simple CSV file:

Alpha   Beta    Gamma
1       4       4
2       6       3

How can I add a specific integer or string to the column names, say 1 or "June"? The output I would expect is:

Alpha_1 Beta_1  Gamma_1
1       4       4
2       6       3

Upvotes: 0

Views: 2160

Answers (2)

Jilber Urbina
Jilber Urbina

Reputation: 61214

DF <- read.table(text='
       Alpha   Beta    Gamma
       1       4       4
       2       6       3', header=TRUE)


colnames(DF) <- paste(colnames(DF), 1, sep='_')

Upvotes: 1

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193687

Use paste or paste0 with names.

Assuming your data.frame is called test: names(test) = paste0(names(test), "_1")

Upvotes: 2

Related Questions