使用特定名称更改R中数据框中的列名称

时间:2015-05-12 16:52:44

标签: r

列名称中需要的输出是     现在我正在

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)

我们如何创建这个   有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您可以使用pastesprintf

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

将它保存在一行而不是多行列名称可能更好。对于打印,我们可以使用cat

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

数据

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