2016-08-02 67 views
1

我有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) 
+0

嘗試有值的數據幀的列名:'colnames(MYDATA)< - C( 「類標籤」, 「酒精」,「蘋果酸「,」灰「,」灰的鹼度「,」鎂「,」 ,「黃酮類」,「類黃酮」,「非類黃酮酚」,「原花青素」,「顏色強度」,「色調」,「稀釋的葡萄酒」,「脯氨酸」)或「colnames(mydata)< - var_names_object' – TJGorrie

+0

但我獲取以下錯誤消息::名稱錯誤(mydata)< - variable_names: 'names'屬性[14]必須與向量長度相同[1] –

回答

0

你有兩個選擇:

  1. 一個最簡單的:當您使用read.table讀取數據時,你可以通過與合適的載體功能的col.names參數中指定的列名。該功能提供了一個可以通過鍵入help(read.table)?read.table

  2. 看了你可以使用colnames(your_data_frame)並將其設置爲您所選擇的名稱的矢量數據後訪問其他選項。你的情況:

    colnames(mydata)<-c("Class label","Alcohol","Malic acid","Ash",...)

+0

非常感謝您的幫助。它解決了。 –

+0

@Kumar P請將答案標記爲已接受,如果它對您有幫助。 – hssay

0

按@ hssay的回答,除了補充一點,你應該避免在列名中使用空格。下面是與載體的種類語法的你需要,並建議就如何列名形式的例:

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

串接所有的變量名的載體,然後應用名稱功能數據

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) 
+0

@kumar P需要檢查列名在這兩個數據幀中的長度是否相同 –

+0

名稱錯誤(mydata)< - variable_names: '名稱'屬性[14]必須與矢量[1]的長度相同。 –

相關問題