This is a test version of Biostars. For the public version, visit https://www.biostars.org.
nrow,ncol name used amino acid string

I have two amino acid sequence.

seq1="GCATGCT"
seq2="GATTACA"

They used string form for R code

> s1string
[1] "G" "C" "A" "T" "G" "C" "T"
> s2string
[1] "G" "A" "T" "T" "A" "C" "A"

I create matrix for this string

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    0    0    0    0    0
[2,]    0    0    0    0    0    0    0    0
[3,]    0    0    0    0    0    0    0    0
[4,]    0    0    0    0    0    0    0    0
[5,]    0    0    0    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0
[7,]    0    0    0    0    0    0    0    0
[8,]    0    0    0    0    0    0    0    0

I want to define matrix ncol with s1string, nrow= s2string. Can you help me for this topic?

sequence

I want to [1,] col and [,1] row don't define. I will define finish [2,] and [2,] row and col.

maybe if you explain what you're trying to accomplish we can help a bit more?

       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0   -1   -2   -3   -4   -5   -6   -7
[2,]   -1    1    0   -1   -2   -3   -4   -5
[3,]   -2    0    0    1    0   -1   -2   -3
[4,]   -3   -1   -1    0    2    1    0   -1
[5,]   -4   -2   -2   -1    1    1    0    1
[6,]   -5   -3   -3   -1    0    0    0    0
[7,]   -6   -4   -2   -2   -1   -1    1    0
[8,]   -7   -5   -3   -1   -2   -2    0    0

I want to colname define start [,2] [,3] [,4] [,5] [,6] [,7] [,8]= "GCATGCT"

for example,

[,2]="G" [,3]="C" [,4]="A" [,5]="T" [,6]="G" [,7]="C" [,8]="T" . Because my matrix must create nrow+1 ncol+1 and I don't want define in this [1,1].

3 answers

Not sure if I understand...or why you might be doing this sort of operation, but you can define the matrix dimensions (ncol and nrow) using the length of each list. If you're looking to name each row/column with the corresponding base, you can use dimnames.

seq1 = "GCATGCT"
seq2 = "GATTACA"

s1string <- strsplit(seq1, "")[[1]]
s2string <- strsplit(seq2, "")[[1]]

matrix(data = 0, nrow = length(s1string), ncol = length(s2string), dimnames = list(s1string,s2string))

You want to assign 7 dimnames to a 8x8 matrix. This is not possible as shown by the error message. You either must assign dimnames of the same length as the matrix dimensions or none at all, this is true for most or all data types. However, for a data.frame you can replace individual dimnames afterwards as shown below. Or you instead simply choose sensible dimnames of length 8, so assign a name to [1,1], say "0", and then

seq1 = "0GCATGCT"
seq2 = "0GATTACA"
s1string <- strsplit(seq1, "")[[1]]
s2string <- strsplit(seq2, "")[[1]]

Otherwise something is wrong with your problem definition.

x = data.frame(matrix(0,ncol=8, nrow=8))
x
  X1 X2 X3 X4 X5 X6 X7 X8
1  0  0  0  0  0  0  0  0
2  0  0  0  0  0  0  0  0
3  0  0  0  0  0  0  0  0
4  0  0  0  0  0  0  0  0
5  0  0  0  0  0  0  0  0
6  0  0  0  0  0  0  0  0
7  0  0  0  0  0  0  0  0
8  0  0  0  0  0  0  0  0 

colnames(x)[2:3] <- c('A','B')
rownames(x)[2:3] <- c('A','B')

x
  X1 A B X4 X5 X6 X7 X8
1  0 0 0  0  0  0  0  0
A  0 0 0  0  0  0  0  0
B  0 0 0  0  0  0  0  0
4  0 0 0  0  0  0  0  0
5  0 0 0  0  0  0  0  0
6  0 0 0  0  0  0  0  0
7  0 0 0  0  0  0  0  0
8  0 0 0  0  0  0  0  0

if you matrix is named matrix, you can define colnames(matrix) and rownames(matrix)

colnames(matrix) <- strplit(seq1, "")[[1]]

I try it but programme give error:

Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent

Log in to answer this question.