0

I have a data frame that I want to set names to it. I tried to use rbind function.

I created a vector of names with the same number as the number of the data frame columns. The vector length 32.

names <- c("Pan troglodytes schweinfurthii A910","Pan troglodytes troglodytes A957", 
           "Pan troglodytes troglodytes A958", "Pan troglodytes troglodytes A960", 
           "Pan troglodytes verus A965", "Pan paniscus A914", "Pan paniscus A915", 
           "Pan paniscus A917", "Pan paniscus A919", "Pan paniscus A920 ",
           "Pan paniscus A925", "Gorilla beringei graueri A929", 
           "Gorilla gorilla gorilla A931", "Gorilla gorilla gorilla A932", 
           "Gorilla gorilla gorilla A937", "Gorilla gorilla gorilla A962", 
           "Gorilla beringei graueri A967", "Pongo pygmaeus A943", 
           "Pongo pygmaeus A944", "Pongo abelii A947",
           "Pongo abelii A949", "Pongo abelii A953", "Pongo abelii A955",
           "Homo sapiens SD647", "Homo sapiens SD428", "Homo sapiens SD455", 
           "Homo sapiens SD583", "Homo sapiens SD643", "Homo sapiens SD961",
           "Homo sapiens SD860","Homo sapiens SD249","Homo sapiens SD1067")

The data frame looks like that with 32 columns:

v218            v227           v254     
A914.AVG_Beta   A915.AVG_Beta   A917.AVG_Beta   
0.6289983   0.4749584   0.5873566   0.5803553
0.9145997   0.8802801   0.8940547   0.9315571
0.8656011   0.8590986   0.853209    0.9070981

When I run :

tomas_data_450K_b_values <- rbind(names, process_tomas)

I get the warning:

There were 32 warnings (use warnings() to see them)

And that outcome:

v218            v227           v254  
NA              NA              NA
A914.AVG_Beta   A915.AVG_Beta   A917.AVG_Beta   
0.6289983   0.4749584   0.5873566   0.5803553
0.9145997   0.8802801   0.8940547   0.9315571
0.8656011   0.8590986   0.853209    0.9070981

How can I set the vector names as the data frame names and save the data frame with the new names?

Parfait
  • 104,375
  • 17
  • 94
  • 125
Nir Galun
  • 11
  • 1
  • 2
  • `colnames(process_tomas) <- c("Pan troglodytes schweinfurthii A910","Pan troglodytes troglodytes A957","Pan troglodytes troglodytes A958","Pan troglodytes troglodytes A960", ...)`? – nghauran Sep 25 '18 at 14:12
  • 3
    Possible duplicate of [Changing column names of a data frame](https://stackoverflow.com/questions/6081439/changing-column-names-of-a-data-frame) – nghauran Sep 25 '18 at 14:24
  • `setNames(process_tomas, names)` – acylam Sep 25 '18 at 14:33

1 Answers1

0

You cannot add names to columns by using rbind since that is for combining rows of data. You change the column names of a data frame by using the function colnames()

colnames(my_data_frame) <- c("colname_1", "colname_2", "colname_3")
stenevang
  • 116
  • 7