Sorkin, John
2023-Jul-03 18:00 UTC
[R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2
I am trying to create an array, myvalues, having 2 rows and 4 columns, where the column names are j,k,xxx1,xxx2. The code below fails, with the following error, "Error in dimnames(myvalues) <- list(NULL, zzz) : length of 'dimnames' [2] not equal to array extent" Please help me get the code to work. Thank you, John # create variable names xxx1 and xxx2. string="" for (j in 1:2){ name <- paste("xxx",j,sep="") string <- paste(string,name) print(string) } # Creation of xxx1 and xxx2 works string # Create matrix myvalues <- matrix(nrow=2,ncol=4) head(myvalues,1) # Add "j" and "k" to the string of column names zzz <- paste("j","k",string) zzz # assign column names, j, k, xxx1, xxx2 to the matrix # create column names, j, k, xxx1, xxx2. dimnames(myvalues)<-list(NULL,zzz)
Jeff Newmiller
2023-Jul-03 18:11 UTC
[R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2
?colnames On July 3, 2023 11:00:32 AM PDT, "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:>I am trying to create an array, myvalues, having 2 rows and 4 columns, where the column names are j,k,xxx1,xxx2. The code below fails, with the following error, "Error in dimnames(myvalues) <- list(NULL, zzz) : > length of 'dimnames' [2] not equal to array extent" > >Please help me get the code to work. > >Thank you, >John > ># create variable names xxx1 and xxx2. >string="" >for (j in 1:2){ > name <- paste("xxx",j,sep="") > string <- paste(string,name) > print(string) >} ># Creation of xxx1 and xxx2 works >string > ># Create matrix >myvalues <- matrix(nrow=2,ncol=4) >head(myvalues,1) ># Add "j" and "k" to the string of column names >zzz <- paste("j","k",string) >zzz ># assign column names, j, k, xxx1, xxx2 to the matrix ># create column names, j, k, xxx1, xxx2. >dimnames(myvalues)<-list(NULL,zzz) > > >______________________________________________ >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.
Rui Barradas
2023-Jul-03 18:34 UTC
[R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2
?s 19:00 de 03/07/2023, Sorkin, John escreveu:> I am trying to create an array, myvalues, having 2 rows and 4 columns, where the column names are j,k,xxx1,xxx2. The code below fails, with the following error, "Error in dimnames(myvalues) <- list(NULL, zzz) : > length of 'dimnames' [2] not equal to array extent" > > Please help me get the code to work. > > Thank you, > John > > # create variable names xxx1 and xxx2. > string="" > for (j in 1:2){ > name <- paste("xxx",j,sep="") > string <- paste(string,name) > print(string) > } > # Creation of xxx1 and xxx2 works > string > > # Create matrix > myvalues <- matrix(nrow=2,ncol=4) > head(myvalues,1) > # Add "j" and "k" to the string of column names > zzz <- paste("j","k",string) > zzz > # assign column names, j, k, xxx1, xxx2 to the matrix > # create column names, j, k, xxx1, xxx2. > dimnames(myvalues)<-list(NULL,zzz) > > > ______________________________________________ > 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, You don't need so many calls to paste, one is enough. And you don't need the for loop at all, paste and paste0 are vectorized. myvalues <- matrix(nrow=2,ncol=4) cnames <- paste0("xxx", 1:2) cnames # [1] "xxx1" "xxx2" colnames(myvalues) <- c("j", "k", cnames) Hope this helps, Rui Barradas