为列名添加前缀 [英] Add a prefix to column names
本文介绍了为列名添加前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
阅读以下帮助文件时,应该可以在列名称中添加前缀:
When reading the following helpfile it should be possible to add a prefix to the column names :
colnames(x, do.NULL = TRUE, prefix = "col")
以下对我不起作用。我在这里做什么错了?
The following doesn't work for me. What am I doing wrong here?
m2 <- cbind(1,1:4)
colnames(m2, do.NULL = FALSE)
colnames(m2) <- c("x","Y")
colnames(m2) <- colnames(m2, prefix = "Sub_")
colnames(m2)
推荐答案
您误读了帮助文件。这是要看的论点:
You have misread the help file. Here's the argument to look at:
do.NULL
:合乎逻辑。如果 FALSE
并且名称为 NULL
,则会创建名称。
do.NULL
: logical. If FALSE
and names are NULL
, names are created.
请注意该说明中的和。您的名字不再是 NULL
,因此使用前缀将不起作用。
Notice the and in that description. Your names are no longer NULL
, so using prefix won't work.
相反,请使用以下内容:
Instead, use something like this:
> m2 <- cbind(1,1:4)
> colnames(m2) <- c("x","Y")
> colnames(m2) <- paste("Sub", colnames(m2), sep = "_")
> m2
Sub_x Sub_Y
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
这篇关于为列名添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文