如何将标题名称添加到第一列[重复]

Posted

技术标签:

【中文标题】如何将标题名称添加到第一列[重复]【英文标题】:How to add header names to the first column [duplicate] 【发布时间】:2016-12-07 17:54:42 【问题描述】:

我有 2 个 txt 文件。第一个有数据,另一个有变量名。 我将数据加载到 R 中,现在我想将列名添加到 variable_names.txt 文件中指定的数据框中。我该怎么做。

我的步骤:

mydata <- read.table("wine.txt")  # Read data
mydata

structure(list(V1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), V2 = c(14.23, 13.2, 13.16, 14.37, 13.24, 14.2, 
14.39, 14.06, 14.83, 13.86, 14.1, 14.12, 13.75, 14.75), V3 = c(1.71, 
1.78, 2.36, 1.95, 2.59, 1.76, 1.87, 2.15, 1.64, 1.35, 2.16, 1.48, 
1.73, 1.73), V4 = c(2.43, 2.14, 2.67, 2.5, 2.87, 2.45, 2.45, 
2.61, 2.17, 2.27, 2.3, 2.32, 2.41, 2.39), V5 = c(15.6, 11.2, 
18.6, 16.8, 21, 15.2, 14.6, 17.6, 14, 16, 18, 16.8, 16, 11.4), 
V6 = c(127L, 100L, 101L, 113L, 118L, 112L, 96L, 121L, 97L, 
98L, 105L, 95L, 89L, 91L), V7 = c(2.8, 2.65, 2.8, 3.85, 2.8, 
3.27, 2.5, 2.6, 2.8, 2.98, 2.95, 2.2, 2.6, 3.1), V8 = c(3.06, 
2.76, 3.24, 3.49, 2.69, 3.39, 2.52, 2.51, 2.98, 3.15, 3.32, 
2.43, 2.76, 3.69), V9 = c(0.28, 0.26, 0.3, 0.24, 0.39, 0.34, 
0.3, 0.31, 0.29, 0.22, 0.22, 0.26, 0.29, 0.43), V10 = c(2.29, 
1.28, 2.81, 2.18, 1.82, 1.97, 1.98, 1.25, 1.98, 1.85, 2.38, 
1.57, 1.81, 2.81), V11 = c(5.64, 4.38, 5.68, 7.8, 4.32, 6.75, 
5.25, 5.05, 5.2, 7.22, 5.75, 5, 5.6, 5.4), V12 = c(1.04, 
1.05, 1.03, 0.86, 1.04, 1.05, 1.02, 1.06, 1.08, 1.01, 1.25, 
1.17, 1.15, 1.25), V13 = c(3.92, 3.4, 3.17, 3.45, 2.93, 2.85, 
3.58, 3.58, 2.85, 3.55, 3.17, 2.82, 2.9, 2.73), V14 = c(1065, 
1050, 1185, 1480, 735, 1450, 1290, 1295, 1045, 1045, 1510, 
1280, 1320, 1150)), .Names = c("V1", "V2", "V3", "V4", "V5", 
"V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", "V14"), 
class = "data.frame", row.names = c(NA, 
-14L))

现在我想为每一列添加标题名称。

mydata <- data.frame(Class label,Alcohol,Malic acid,Ash,Alcalinity of ash,Magnesium,Total phenols,Flavanoids,Nonflavanoid phenols,Proanthocyanins,Color intensity,Hue,Diluted wines,Proline)

【问题讨论】:

试试:colnames(mydata) &lt;- c("Class label","Alcohol","Malic acid","Ash","Alcalinity of ash","Magnesium","Total phenols","Flavanoids","Nonflavanoid phenols","Proanthocyanins","Color intensity","Hue","Diluted wines","Proline")colnames(mydata) &lt;- var_names_object 但我收到以下错误消息::名称错误(mydata) 【参考方案1】:

你有两个选择:

    最简单的一种:当你使用read.table读取数据时,你可以通过函数的col.names参数用适当的向量来指定列名。该函数提供了其他选项,您可以通过键入help(read.table)?read.table 来访问这些选项

    读取数据后,您可以使用colnames(your_data_frame) 并将其设置为您选择的名称向量。在你的情况下:

    colnames(mydata)&lt;-c("Class label","Alcohol","Malic acid","Ash",...)

【讨论】:

非常感谢您的帮助。解决了。​​ @Kumar P 如果对您有帮助,请将答案标记为已接受。【参考方案2】:

根据@hssay 的回答,除了要补充一点,您应该避免在列名中使用空格。这是您需要的向量语法的示例,以及有关如何格式化列名的建议:

names(mydata) <- c("ClassLabel","Alcohol","MalicAcid","Ash",
    "AlcalinityOfAsh","Magnesium","TotalPhenols","Flavanoids",
    "NonflavanoidPhenols","Proanthocyanins","ColorIntensity",
    "Hue","DilutedWines","Proline")

【讨论】:

【参考方案3】:

将所有变量名称连接到一个向量,然后将名称函数应用于您的数据

    variable_names <- c("Class label","Alcohol","Malic acid","Ash","Alcalinity of ash","Magnesium","Total phenols","Flavanoids",
      "Nonflavanoid phenols","Proanthocyanins","Color intensity","Hue","Diluted wines","Proline")

       names(mydata) <- variable_names

或者您可以只添加具有值的数据框的列名

colnames(mydata) <-colnames(no_data) 

【讨论】:

@kumar P 需要检查两个数据框中的列名长度是否相同 名称错误(mydata)

以上是关于如何将标题名称添加到第一列[重复]的主要内容,如果未能解决你的问题,请参考以下文章

sql server如何将字段添加到第一列

R的read.csv在第一列名称前加上垃圾文本[重复]

R:扩展矩阵的第一列

如何将多列中的字符串添加到R中单列中的多行

将Excel选择移动到第一列(偏移?)

如何将矩阵子集为一列,维护矩阵数据类型,维护行/列名称?