Sunita22
2009-May-24 16:21 UTC
[R] Assigning variable names from one object to another object
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? or should I paste the column names in csv file (from where I have imported the data) and then import in R? Thank you in advance Regards Sunita -- View this message in context: http://www.nabble.com/Assigning-variable-names-from-one-object-to-another-object-tp23695359p23695359.html Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2009-May-24 16:29 UTC
[R] Assigning variable names from one object to another object
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 > >
Sunita Patil
2009-May-24 16:33 UTC
[R] Assigning variable names from one object to another object
Hello Duncan Thank you so much it worked. I think I was doing it in a more complicated way so I didnt get a solution Thank you very much once again Regards Sunita On Sun, May 24, 2009 at 9:59 PM, Duncan Murdoch <murdoch@stats.uwo.ca>wrote:> 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 >> >> >> >-- Our Thoughts have the Power to Change our Destiny. Sunita [[alternative HTML version deleted]]