Jeff Newmiller
2023-Jul-03 18:45 UTC
[R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2
I really think you should read that help page. colnames() accesses the second element of dimnames() directly. On July 3, 2023 11:39:37 AM PDT, "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:>Jeff, >Thank you for your reply. >I should have said with dim names not column names. I want the Mateix to have dim names, no row names, dim names j, k, xxx1, xxx2. > >John > >John David Sorkin M.D., Ph.D. >Professor of Medicine >Chief, Biostatistics and Informatics >University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine >Baltimore VA Medical Center >10 North Greene Street<x-apple-data-detectors://12> >GRECC<x-apple-data-detectors://12> (BT/18/GR) >Baltimore, MD 21201-1524<x-apple-data-detectors://13/0> >(Phone) 410-605-711<tel:410-605-7119>9 >(Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above prior to faxing) > >On Jul 3, 2023, at 2:11 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: > >??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://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=y8kLruSvrjxQLegbbPNMMl665EEApCgiSOq%2BEmhQfNE%3D&reserved=0 >PLEASE do read the posting guide https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=HBUMNAeG1KurerS2DAhKxxZVRs71TSF0YJSGjP%2FCixA%3D&reserved=0 >and provide commented, minimal, self-contained, reproducible code. > >-- >Sent from my phone. Please excuse my brevity. > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=y8kLruSvrjxQLegbbPNMMl665EEApCgiSOq%2BEmhQfNE%3D&reserved=0 >PLEASE do read the posting guide https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=HBUMNAeG1KurerS2DAhKxxZVRs71TSF0YJSGjP%2FCixA%3D&reserved=0 >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
Sorkin, John
2023-Jul-03 19:26 UTC
[R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2
Jeff, Again my thanks for your guidance. I replaced dimnames(myvalues)<-list(NULL,c(zzz)) with colnames(myvalues)<-zzz and get the same error, Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent It appears that I am creating the string zzz in a manner that is not compatable with either dimnames(myvalues)<-list(NULL,c(zzz)) or colnames(myvalues)<-zzz I think I need to modify the way I create the string zzz. # 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,c(zzz)) colnames(myvalues)<-zzz ________________________________________ From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> Sent: Monday, July 3, 2023 2:45 PM To: Sorkin, John Cc: r-help at r-project.org Subject: Re: [R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2 I really think you should read that help page. colnames() accesses the second element of dimnames() directly. On July 3, 2023 11:39:37 AM PDT, "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:>Jeff, >Thank you for your reply. >I should have said with dim names not column names. I want the Mateix to have dim names, no row names, dim names j, k, xxx1, xxx2. > >John > >John David Sorkin M.D., Ph.D. >Professor of Medicine >Chief, Biostatistics and Informatics >University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine >Baltimore VA Medical Center >10 North Greene Street<x-apple-data-detectors://12> >GRECC<x-apple-data-detectors://12> (BT/18/GR) >Baltimore, MD 21201-1524<x-apple-data-detectors://13/0> >(Phone) 410-605-711<tel:410-605-7119>9 >(Fax) 410-605-7913<tel:410-605-7913> (Please call phone number above prior to faxing) > >On Jul 3, 2023, at 2:11 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: > >??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. > >______________________________________________ >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.