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

时间:2022-11-13 15:51:24

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?

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

1 个解决方案

#1


1  

You could use paste or sprintf

你可以使用paste或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 

#1


1  

You could use paste or sprintf

你可以使用paste或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