我有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 :(得分:0)
您有两种选择:
最简单的一个:当您使用read.table
读取数据时,可以通过具有适当向量的函数的col.names
参数指定列名。该功能提供了您可以通过键入help(read.table)
或?read.table
阅读完数据后,您可以使用colnames(your_data_frame)
并将其设置为您选择的名称向量。在你的情况下:
colnames(mydata)<-c("Class label","Alcohol","Malic acid","Ash",...)
答案 1 :(得分:0)
根据@ hssay的回答,除了添加之外,你应该避免在列名中使用空格。下面是一个示例,其中包含您需要的矢量语法,以及有关如何设置列名称格式的建议:
names(mydata) <- c("ClassLabel","Alcohol","MalicAcid","Ash",
"AlcalinityOfAsh","Magnesium","TotalPhenols","Flavanoids",
"NonflavanoidPhenols","Proanthocyanins","ColorIntensity",
"Hue","DilutedWines","Proline")
答案 2 :(得分: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)