Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to names dataframe's columns using a vector

I want to name dataframe’s columns using a vector of character. I don’t know why with the following code I get the error:

Error in names(x) <- value :
‘names’ attribute [5] must be the same length as the vector [1]

vec <- 1:5
vec <- as.data.frame(vec)
colnames(vec) <- c("a", "b", "c", "d", "e")

Thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The as.data.frame still returns a single column. Instead, we may need to use as.data.frame.list

vec <- 1:5
vec <- as.data.frame.list(vec)
colnames(vec) <- c("a", "b", "c", "d", "e")

-output

> vec
  a b c d e
1 1 2 3 4 5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading