1

I have 2 txt file. first one have data and other one have variable name. I loaded data into R and Now I want add column names to the data frame which specified in variable_names.txt file. How can I do that.

My steps :

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

Now I want put header name with respect to each column.

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)
h3rm4n
  • 4,126
  • 15
  • 21
Kumar P
  • 61
  • 1
  • 2
  • 9
  • try: `colnames(mydata) <- c("Class label","Alcohol","Malic acid","Ash","Alcalinity of ash","Magnesium","Total phenols","Flavanoids","Nonflavanoid phenols","Proanthocyanins","Color intensity","Hue","Diluted wines","Proline")` or `colnames(mydata) <- var_names_object` – TJGorrie Aug 02 '16 at 11:40
  • But I am getting below error message::Error in names(mydata) <- variable_names : 'names' attribute [14] must be the same length as the vector [1] – Kumar P Aug 02 '16 at 16:32

3 Answers3

0

You have two options:

  1. The Easiest one: when you are reading the data using read.table, you can specify the column names through the col.names argument of the function with appropriate vector. The function provides other options that you can access by typing help(read.table) or ?read.table

  2. After having read the data you can use colnames(your_data_frame) and set it to a vector of names of your choice. In your case:

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

DeveauP
  • 1,217
  • 11
  • 21
hssay
  • 178
  • 1
  • 10
0

As per @hssay's answer, except to add that you should avoid using spaces in your column names. Here's an example with the kind of vector syntax you'd need, and a suggestion as to how to format your column names:

names(mydata) <- c("ClassLabel","Alcohol","MalicAcid","Ash",
    "AlcalinityOfAsh","Magnesium","TotalPhenols","Flavanoids",
    "NonflavanoidPhenols","Proanthocyanins","ColorIntensity",
    "Hue","DilutedWines","Proline")
rosscova
  • 5,430
  • 1
  • 22
  • 35
0

Concatenate all the your variable names to a vector and then apply names function to your data

    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

Or you can just add column names of of data frame which have values by

colnames(mydata) <-colnames(no_data) 
Arun kumar mahesh
  • 2,289
  • 2
  • 14
  • 22