开发者

R dataframe naming the headers

开发者 https://www.devze.com 2023-02-27 10:11 出处:网络
I have a data.frame df as follows with the column names A, B, C. A B C 1 2 3 4 5 6 7 8 9 so df$A in the above example 1,4,7.

I have a data.frame df as follows with the column names A, B, C.

 A B C 
 1 2 3
 4 5 6
 7 8 9

so df$A in the above example 1,4,7.

Now I want to column this data.frame 3 times changing the names of the columns to A B C A.1 B.1 C.1 A.2 B.2 C.2 so my new data.frame newDf will have 9 columns.

Essentially what I want is to know how to change the name of the columns of a given data.frame. Once I can do th开发者_如何转开发at I can column bind and things will work. Please give an example with your answer.


If you have dataframe that has c columns, then you can make a vector with c number of strings. Then use the names function to set the headings to be the names of the columns.

col_headings <- c('heading1','heading2', ....,'heading_c')
names(your_dataframe) <- col_headings


Look at the "See Also" section of ?data.frame:

 ‘I’, ‘plot.data.frame’, ‘print.data.frame’, ‘row.names’, ‘names’
 (for the column names), ‘[.data.frame’ for subsetting methods,
 ‘Math.data.frame’ etc, about _Group_ methods for ‘data.frame’s;
 ‘read.table’, ‘make.names’.

There you'll find a reference to names. Now you can read ?names to see how to use it. ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消