[R] Create matrix with variable number of columns AND CREATE NAMES FOR THE COLUMNS

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Mon Jul 1 19:43:36 CEST 2024


NSims <- 4
Grps <- 5
DiffMeans <- matrix(nrow=NSims,ncol=1+Grps)
DiffMeans

#I have a problem when I try to name the columns of the matrix. I want the first column to be NSims, #and the other columns to be something like Value1, Value2, . . . Valuen where N=Grps
Colnames <- as.vector("NSims")
num_elements <- ncol(DiffMeans)
for (i in 2:num_elements) {
  Colnames[i] <- paste0("Item",i)
}
colnames(DiffMeans) <- Colnames

You need the vector "Colnames" to have the same number of elements as columns in DiffMeans.
Colnames in created with the first element in place because that will not change.
The loop starts with the second element so that the first element is not overwritten by the for loop.


As a function it might look something like this:
# Name columns is a function that will take a matrix and two strings.
# The first string is the name of the first column.
# The second string is the base part of column names.
#     The function adds a number after the base part.
name_cols <- function(matrix, name1, name2){
  colnames <- as.vector(name1)
  num_elements <- ncol(DiffMeans)
  # I wrote a function to build a list of length Grps createValuelist <- function(num_elements) {
  for (i in 2:num_elements) {
    colnames[i] <- paste0(name2,i)
  }
}

colnames(DiffMeans) <- name_cols(DiffNames,"Book", "Cola")

You can now make the names anything you want for any specific use.
You could add some error checking like making sure the first parameter is a matrix and the other two parmeters are appropriate strings.
If the function will only be called once or twice it might be simpler to not use a function.


Tim
-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Sorkin, John
Sent: Monday, July 1, 2024 11:54 AM
To: r-help using r-project.org (r-help using r-project.org) <r-help using r-project.org>
Subject: [R] Create matrix with variable number of columns AND CREATE NAMES FOR THE COLUMNS

[External Email]

#I am trying to write code that will create a matrix with a variable number of columns where the #number of columns is 1+Grps #I can do this:
NSims <- 4
Grps <- 5
DiffMeans <- matrix(nrow=NSims,ncol=1+Grps) DiffMeans

#I have a problem when I try to name the columns of the matrix. I want the first column to be NSims, #and the other columns to be something like Value1, Value2, . . . Valuen where N=Grps

# I wrote a function to build a list of length Grps createValuelist <- function(num_elements) {
  for (i in 1:num_elements) {
    cat("Item", i, "\n", sep = "")
  }
}
createValuelist(Grps)

# When I try to assign column names I receive an error:
#Error in dimnames(DiffMeans) <- list(NULL, c("NSim", createValuelist(Grps))) :
# length of 'dimnames' [2] not equal to array extent
dimnames(DiffMeans) <- list(NULL,c("NSim",createValuelist(Grps)))
DiffMeans

# Thank you for your help!


John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine; Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center; PI Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center; Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.r-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list