Skip to content
Back to formatted view

Raw Message

Message-ID: <4A1975E9.6070305@stats.uwo.ca>
Date: 2009-05-24T16:29:29Z
From: Duncan Murdoch
Subject: Assigning variable names from one object to another object
In-Reply-To: <23695359.post@talk.nabble.com>

On 24/05/2009 12:21 PM, Sunita22 wrote:
> Hello
> 
> I have 2 datasets say Data1 and Data2 both are of different dimesions. 
> 
> Data1:
> 120 rows and 6 columns (Varname, Vartype, Labels, Description, ....)
> The column Varname has 120 rows which has variable names such id, age,
> gender,.....so on
> 
> Data2:
> 12528 rows and 120 columns
> The column names in this case are V1, V2, ......... V120 (which are default
> names in R when we say head=F in read.csv)
> 
> I want to assign the variable names from Data1 to Data2 as the column
> headings in Data2 i.e V1 should be id, V2 should be age, ...... so on
> 
> Is it possible to do in R?
> I tired assigning variable names from Data1 in one object and transposing
> them and then used rbind but it doesnot work.
> 
> Can I use colnames? I could not apply it in this case. Can any1 tell me how
> can i apply it for this case?

names or colnames should work:

colnames(Data2) <- Data1$Varname

or

names(Data2) <- Data1$Varname

> 
> or should I paste the column names in csv file (from where I have imported
> the data) and then import in R?

That's another way that would work.

Duncan Murdoch
> 
> Thank you in advance
> 
> Regards
> Sunita
> 
>