Re: [R] Create a variable lenght string that can be used in a dimnames statement
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 On Behalf Of Sorkin, John Sent: Tuesday, July 4, 2023 12:17 AM To: Rolf Turner ; Bert Gunter Cc: r-help@r-project.org (r-help@r-project.org) ; Achim Zeileis 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 Sent: Monday, July 3, 2023 6:34 PM To: Bert Gunter Cc: Sorkin, John; r-help@r-project.org (r-help@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 wrote: > I am not going to try to sort out your confusion, as others have > already tried and failed. 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@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. __ R-help@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.
Re: [R] Create a variable lenght string that can be used in a dimnames statement
(Sorry for the double post.) On Tue, 4 Jul 2023 10:14:43 +0300 Ivan Krylov wrote: > Try replacing the _second_ paste() in the example above with a c(). What I had forgotten to mention is that you also need to replace the initial assignment > string="" with the following: string = character() (NULL and c(), as mentioned by Rui, are also valid options here.) If you don't do that, the empty string remains an element in the resulting vector c('', 'xxx1', 'xxx2'), which is not the desired result. It's awkward to build data structures in an iterative manner, which is why solutions like Rui's paste0("xxx", 1:2) are considered more idiomatic. -- Best regards, Ivan __ R-help@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.
Re: [R] Create a variable lenght string that can be used in a dimnames statement
On Mon, 3 Jul 2023 20:08:06 + "Sorkin, John" wrote: > # 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 You need to distinguish between a space separated string and a string vector. A space separated string is a single object that R won't split for you unless you tell it to. (Are you coming from a Unix background? A command line shell will split a space-separated string into a list of words unless you tell it not to do that, but that's because its main job is to convert space separated command lines typed by the operator into lists of command line arguments. R is not like that at all.) What you're getting here is an individual string. You can confirm that by typing: string[1] and still seeing the whole string. You can also type: string[2] and see an NA instead of the second word in the string. Try replacing the _second_ paste() in the example above with a c(). Given individual strings, paste() concatenates its arguments into a string. c() concatenates its arguments into a vector of separate objects. You should still get a variable named `string`, but when you print it whole, the results will look different (R will separate the two strings inside that vector), and you should be able to type string[1] and string[2] and get "xxx1" and "xxx2" respectively. What about Rui's solution, the one that used paste() and got a vector, contrary to what I wrote above? Rui gave a vector to paste(). paste() runs a loop inside it and produces a string for every vector element of it encounters, unless you tell it to collapse the result. > zzz <- paste("j","k",string) Same thing here. paste() adds spaces and returns a string. You need c() to retain "j" and "k" separate from "xxx1" and "xxx2" as individual elements of the vector. It may *look* similar ([1] "j k xxx1 xxx2" vs. [1] "j" "k" "xxx1" "xxx2"), but the resulting objects are different. When in doubt, use str() on an object to see its structure. -- Best regards, Ivan __ R-help@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.
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 Sent: Monday, July 3, 2023 6:34 PM To: Bert Gunter Cc: Sorkin, John; r-help@r-project.org (r-help@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 wrote: > I am not going to try to sort out your confusion, as others have > already tried and failed. 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@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.
Re: [R] Create a variable lenght string that can be used in a dimnames statement
At least on my system string has a single value of " xxx1 xxx2" not "xxx1" and "xxx2". The variable zzz has two values: "J K xxx1" and "J K xxx2" What you want is "J", "K", "xxx1", "xxx2" If I cheat everything works. So then the goal is to rewrite the program so cheating is not needed. # 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 string = c("xxx1","xxx2") #Cheating # 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) # assign column names, j, k, xxx1, xxx2 to the matrix # create column names, j, k, xxx1, xxx2. zzz<-c("J", "K", "xxx1", "xxx2") #Cheating again dimnames(myvalues)<-list(NULL,c(zzz)) Here is one version that runs without cheating # create variable names xxx1 and xxx2. string="" for (j in 1:2){ string[j] <- paste("xxx",j,sep="") print(string) } # Creation of xxx1 and xxx2 works string # Create matrix myvalues <- matrix(nrow=2,ncol=4) zzz <- c("j","k",string) # assign column names, j, k, xxx1, xxx2 to the matrix # create column names, j, k, xxx1, xxx2. dimnames(myvalues)<-list(NULL,c(zzz)) Regards, Tim -Original Message- From: R-help On Behalf Of Sorkin, John Sent: Monday, July 3, 2023 4:08 PM To: r-help@r-project.org (r-help@r-project.org) Subject: [R] Create a variable lenght string that can be used in a dimnames statement [External Email] Colleagues, I am sending this email again with a better description of my problem and the area where I need help. I need help creating a string of variables that will be accepted by the dimnames function. The string needs to start with the dimnames j and k followed by a series of dimnames xxx1, . . . ., xxx2, . . ., xxxn. I create xxx1, xxx2 (not going to xxxn to shorten the code below) as a string using a for loop and the paste function. I then use a paste function, zzz <- paste("j","k",string) to create the full set of dimnames, j, k, xxx1, xxx2 as string. I create the matrix myvalues in the usual way and attempt to assign dim names to the matrix using the following dimnames statement, dimnames(myvalues)<-list(NULL,c(zzz)) The dimnames statement leads to the following error, Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent A colnames statement, colnames(myvalues)<-as.character(zzz) produces the same error. Can someone tell me how to create a sting that can be used in the dimnames statment? Thank you (and please accept my apologies for double posting). 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,c(zzz)) colnames(myvalues) <- string __ R-help@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. __ R-help@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.
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 wrote: > I am not going to try to sort out your confusion, as others have > already tried and failed. 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@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.
Re: [R] Create a variable lenght string that can be used in a dimnames statement
I am not going to try to sort out your confusion, as others have already tried and failed. But I will point out that "string" of variables is pretty much nonsense in R. A "character vector"/"vector of strings" is probably what you mean and want to provide column names // names for the second component of the dim list for a matrix. e.g. > m <- matrix(1:6, nr=3) > m [,1] [,2] [1,]14 [2,]25 [3,]36 > dimnames(m) NULL > dimnames(m) <- list(letters[1:3], c('foo','bar')) > m foo bar a 1 4 b 2 5 c 3 6 Cheers, Bert On Mon, Jul 3, 2023 at 1:10 PM Sorkin, John wrote: > Colleagues, > > I am sending this email again with a better description of my problem and > the area where I need help. > > I need help creating a string of variables that will be accepted by the > dimnames function. The string needs to start with the dimnames j and k > followed by a series of dimnames xxx1, . . . ., xxx2, . . ., xxxn. I create > xxx1, xxx2 (not going to xxxn to shorten the code below) as a string using > a for loop and the paste function. I then use a paste function, zzz <- > paste("j","k",string) to create the full set of dimnames, j, k, xxx1, xxx2 > as string. I create the matrix myvalues in the usual way and attempt to > assign dim names to the matrix using the following dimnames statement, > dimnames(myvalues)<-list(NULL,c(zzz)) > The dimnames statement leads to the following error, > Error in dimnames(x) <- dn : > length of 'dimnames' [2] not equal to array extent > A colnames statement, > colnames(myvalues)<-as.character(zzz) > produces the same error. > > Can someone tell me how to create a sting that can be used in the dimnames > statment? > > Thank you (and please accept my apologies for double posting). > > 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,c(zzz)) > colnames(myvalues) <- string > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
[R] Create a variable lenght string that can be used in a dimnames statement
Colleagues, I am sending this email again with a better description of my problem and the area where I need help. I need help creating a string of variables that will be accepted by the dimnames function. The string needs to start with the dimnames j and k followed by a series of dimnames xxx1, . . . ., xxx2, . . ., xxxn. I create xxx1, xxx2 (not going to xxxn to shorten the code below) as a string using a for loop and the paste function. I then use a paste function, zzz <- paste("j","k",string) to create the full set of dimnames, j, k, xxx1, xxx2 as string. I create the matrix myvalues in the usual way and attempt to assign dim names to the matrix using the following dimnames statement, dimnames(myvalues)<-list(NULL,c(zzz)) The dimnames statement leads to the following error, Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent A colnames statement, colnames(myvalues)<-as.character(zzz) produces the same error. Can someone tell me how to create a sting that can be used in the dimnames statment? Thank you (and please accept my apologies for double posting). 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,c(zzz)) colnames(myvalues) <- string __ R-help@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.