Sorkin, John
2024-Jul-01 15:54 UTC
[R] Create matrix with variable number of columns AND CREATE NAMES FOR THE COLUMNS
#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
Ebert,Timothy Aaron
2024-Jul-01 17:43 UTC
[R] Create matrix with variable number of columns AND CREATE NAMES FOR THE COLUMNS
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 at r-project.org> On Behalf Of Sorkin, John Sent: Monday, July 1, 2024 11:54 AM To: r-help at r-project.org (r-help at r-project.org) <r-help at 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 at 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.
Rui Barradas
2024-Jul-01 17:56 UTC
[R] Create matrix with variable number of columns AND CREATE NAMES FOR THE COLUMNS
?s 16:54 de 01/07/2024, Sorkin, John escreveu:> #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 at 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.Hello, Something like this? names_cols <- function(x, First = "NSims", Prefix = "Value") { nms <- c(First, sprintf("%s%d", Prefix, seq_len(ncol(x) - 1L))) colnames(x) <- nms x } NSims <- 4 Grps <- 5 DiffMeans <- matrix(nrow=NSims,ncol=1+Grps) names_cols(DiffMeans) #> NSims Value1 Value2 Value3 Value4 Value5 #> [1,] NA NA NA NA NA NA #> [2,] NA NA NA NA NA NA #> [3,] NA NA NA NA NA NA #> [4,] NA NA NA NA NA NA Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Rui Barradas
2024-Jul-01 18:05 UTC
[R] Create matrix with variable number of columns AND CREATE NAMES FOR THE COLUMNS
?s 16:54 de 01/07/2024, Sorkin, John escreveu:> #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 at 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.Hello, Sorry for my first answer, I thought you only wanted to name the matrix columns. After reading the OP again, this time actually reading it, I realized you also want to create the matrix. This is even in the question title line :(. create_matrix <- function(nsims, ngrps, First = "NSims", Prefix = "Value") { # could also be paste0(Prefix, seq_len(ngrps)) grp_names <- sprintf("%s%d", Prefix, seq_len(ngrps)) nms <- c(First, grp_names) matrix(nrow = nsims, ncol = 1L + ngrps, dimnames = list(NULL, nms)) } NSims <- 4 Grps <- 5 create_matrix(NSims, Grps) #> NSims Value1 Value2 Value3 Value4 Value5 #> [1,] NA NA NA NA NA NA #> [2,] NA NA NA NA NA NA #> [3,] NA NA NA NA NA NA #> [4,] NA NA NA NA NA NA Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Jeff Newmiller
2024-Jul-01 18:50 UTC
[R] Create matrix with variable number of columns AND CREATE NAMES FOR THE COLUMNS
I think you should reconsider your goal. Matrices must have all elements of the same type, and in this case you seem to be trying to mix a number of something (integer) with mean values (double). This would normally be stored together in a data frame or separately in a vector for counts and a matrix for means. If you are just thinking about data presentation, a data frame would be a better choice than a single matrix. On July 1, 2024 8:54:21 AM PDT, "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:>#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 at 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.-- Sent from my phone. Please excuse my brevity.
Heinz Tuechler
2024-Jul-01 18:55 UTC
[R] Create matrix with variable number of columns AND CREATE NAMES FOR THE COLUMNS
Sorkin, John wrote/hat geschrieben on/am 01.07.2024 17:54:> #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 >maybe: NSims <- 4 Grps <- 5 DiffMeans <- matrix(nrow=NSims,ncol=1+Grps, dimnames=list(NULL, c('Nsimn', paste('Item', 1:Grps, sep='')))) DiffMeans best, Heinz> > ______________________________________________ > R-help at 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. >