[R] Create a variable lenght string that can be used in a dimnames statement

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Wed Jul 5 02:05:47 CEST 2023


Interesting to read all the answers. Personally, I was a bit irked to see
that using a combination of assignments using rownames() and colnames() did
not work as one canceled what the other had done.

But it turns out if we listed to what John really wanted versus what he said
he wanted, then a fairly simple parameterized answer is to add the row and
column names when creating the matrix as in:

# Create a matrix of size M by N rows, initialiazed to NA
# and also add row names that look like row1, row2, ... rowM
# as well as column names that look like col1, col2, ... colN
# Set the parameters and the rest is a one-liner, wrapped a bit
# for legibility:
M <- 2
N <- 4
rowpref <- "row"
colpref <- "col"

myvalues <- matrix(data=NA, 
                   nrow=M, 
                   ncol=N, 
                   dimnames=list(rows=paste("row", seq(M), sep=""), 
                                 cols=paste("col", seq(N), sep="")))

The resulting value is:

> myvalues
      cols
rows   col1 col2 col3 col4
  row1   NA   NA   NA   NA
  row2   NA   NA   NA   NA



-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Sorkin, John
Sent: Tuesday, July 4, 2023 12:17 AM
To: Rolf Turner <rolfturner using posteo.net>; Bert Gunter
<bgunter.4567 using gmail.com>
Cc: r-help using r-project.org (r-help using r-project.org) <r-help using r-project.org>;
Achim Zeileis <Achim.Zeileis using uibk.ac.at>
Subject: Re: [R] Create a variable lenght string that can be used in a
dimnames statement

My life is complete.
I have inspired a fortune!
John

________________________________________
From: Rolf Turner <rolfturner using posteo.net>
Sent: Monday, July 3, 2023 6:34 PM
To: Bert Gunter
Cc: Sorkin, John; r-help using r-project.org (r-help using r-project.org); Achim Zeileis
Subject: Re: [R]  Create a variable lenght string that can be used in a
dimnames statement


On Mon, 3 Jul 2023 13:40:41 -0700
Bert Gunter <bgunter.4567 using gmail.com> wrote:

> I am not going to try to sort out your confusion, as others have
> already tried and failed.

<SNIP>

Fortune nomination!!!

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Stats. Dep't. (secretaries) phone:
         +64-9-373-7599 ext. 89622
Home phone: +64-9-480-4619

______________________________________________
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