Jaikumar S
Jaikumar S

Reputation: 144

Changing column names in a data frame in R with the specific name

my output required in the column name is now i am getting as

Column A    Column B
x = 128
Y = 145
in the code as we need to write is "Column A (N=x)" "Column B (N=Y)"
How we will call the value in X in quotation mark.

but i need the column name as below

Column A    Column B
(N=128)      (N=145)

how we need to create this Could someone help me to figure this out?

Upvotes: 0

Views: 156

Answers (1)

akrun
akrun

Reputation: 886938

You could use paste or sprintf

 names(df1) <- paste0(names(df1), ' (N=', c(x,Y), ')')
 names(df1)
 #[1] "Column A (N=128)" "Column B (N=145)"

It may be better to keep it in a single line instead of multi-line column names. For printing, we can use cat

  cat(paste0(names(df1), '\n (N=', c(x,Y), ')\n'))

data

df1 <- data.frame("Column A" =1:5, "Column B"= 6:10, check.names=FALSE)
x <- 128
Y <- 145 

Upvotes: 1

Related Questions