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

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Tue Jul 4 03:24:02 CEST 2023


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 <r-help-bounces using r-project.org> On Behalf Of Sorkin, John
Sent: Monday, July 3, 2023 4:08 PM
To: r-help using r-project.org (r-help using r-project.org) <r-help using 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 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