0

I want to create a table and set the names to it but it does not work as expected. Actually, I can't find what is wrong

arr <- array(dim = c(1,5))
names(arr) <- c("Year", "Month", "Day", "Name", "Surname")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Check the class of "arr".. Its a matrix.For a matrix, define column names using either colnames function instead of names. – Aayush Agrawal Mar 26 '18 at 06:43
  • You may want to ask this question differently. I'm not sure you want a matrix. What's the purpose of your data structure? Depending on the purpose, something like this might work better: `var <- c(Year = 2305, Month = "July", Day = 13, Name = "Jean-Luc", Surname = "Picard")` – De Novo Mar 26 '18 at 06:50

1 Answers1

0

It is a matrix and matrix have dimnames. So, either we provide the dimnames as a list when the array is initialized

arr <- array(dim = c(1,5), dimnames = list(NULL, 
           c("Year", "Month", "Day", "Name", "Surname")))

or assign the column names with colnames

colnames(arr) <- c("Year", "Month", "Day", "Name", "Surname")
arr
#    Year Month Day Name Surname
#[1,]   NA    NA  NA   NA      NA
akrun
  • 874,273
  • 37
  • 540
  • 662