Re: [R] how to create column names for the matrix

2016-07-27 Thread lily li
Thanks, this works. I thought the code should put in the for loop, but it
turns out an extra line.

On Wed, Jul 27, 2016 at 12:50 PM,  wrote:

> Hello,
>
> Try
>
>
> chars = c('A','B','C','D')
> matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
> k = 0
> for(i in 1:length(1:5)){  # or more simply just for(i in 1:5)
>   for(j in 1:length(chars)){
> k = k+1
> matrix1[,k] = k
>   }
> }
> matrix1
> tmp <- expand.grid(chars, 1:5, stringsAsFactors = FALSE)
> nms <- paste(tmp[[2]], tmp[[1]], sep = "-")
> rm(tmp)
> colnames(matrix1) <- nms
>
> head(matrix1)
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Citando lily li :
>
> Hi all,
>
> I want to ask that how to create column names for a matrix. For example,
> the matrix below, the column names should be: 1-A, 1-B, 1-C, 1-D, 2-A, 2-B,
> 2-C, 2-D, 3-A, etc. Thanks for your help.
>
> chars = c('A','B','C','D')
> matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
> k = 0
> for(i in seq(1:length(1:5))){
> for(j in seq(1:length(chars))){
>k = k+1
>matrix1[,k] = c(1:100)[k]
> }
> }
>
> [[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.htmland 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.


Re: [R] how to create column names for the matrix

2016-07-27 Thread Hervé Pagès

On 07/27/2016 11:49 AM, lily li wrote:

If replace 1:5 to char2 = c('east','west','south','north','central'),
how to put on column names with the original structure? Thanks again.


Have you tried to actually "replace 1:5 with char2", literally?

You need to try things before you ask. That's the only way to learn!

H.




On Wed, Jul 27, 2016 at 12:40 PM, Hervé Pagès > wrote:

Hi,


On 07/27/2016 11:17 AM, lily li wrote:

Hi all,

I want to ask that how to create column names for a matrix. For
example,
the matrix below, the column names should be: 1-A, 1-B, 1-C,
1-D, 2-A, 2-B,
2-C, 2-D, 3-A, etc. Thanks for your help.

chars = c('A','B','C','D')
matrix1 = matrix(nrow = length(1:100), ncol =
length(1:5)*length(chars))
k = 0
for(i in seq(1:length(1:5))){
  for(j in seq(1:length(chars))){
k = k+1
matrix1[,k] = c(1:100)[k]
  }
}


Also how could you possibly use such level of code obfuscation to
perform such simple initialization of your matrix?

My 1st advice would be that you slow down and take the time to compare
seq(1:length(1:5)) with 1:length(1:5) with 1:5. It will be a great
learning experience!

As for initializing your matrix, what about doing

  ncol <- 5 * length(chars)
  matrix1 <- matrix(seq_len(ncol), nrow=100, ncol=ncol, byrow=TRUE)

instead?

Then set the colnames with:

  colnames(matrix1) <- paste(rep(1:5, length(chars)), chars, sep="-")

Cheers,
H.


[[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.


--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fredhutch.org 
Phone:  (206) 667-5791 
Fax:(206) 667-1319 




--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fredhutch.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

__
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] how to create column names for the matrix

2016-07-27 Thread Hervé Pagès

I see. But please understand that initializing the values is not the
same as setting the colnames. How providing this almost-impossible-
to-read initialization code helps with respect to your question which
is about setting the colnames?

I know people often asked you to show the code in your previous
questions on this site. They're right: showing the code helps.
But only the code that is relevant to your question. Code that is
not relevant to your question is only distracting and confusing.

H.

On 07/27/2016 11:45 AM, lily li wrote:

Thanks. I shorten a more complex matrix to this example, but use the
original structure of the code. The original matrix has all characters
instead of 1:5.


On Wed, Jul 27, 2016 at 12:42 PM, Hervé Pagès > wrote:

On 07/27/2016 11:40 AM, Hervé Pagès wrote:

Hi,

On 07/27/2016 11:17 AM, lily li wrote:

Hi all,

I want to ask that how to create column names for a matrix.
For example,
the matrix below, the column names should be: 1-A, 1-B, 1-C,
1-D, 2-A,
2-B,
2-C, 2-D, 3-A, etc. Thanks for your help.

chars = c('A','B','C','D')
matrix1 = matrix(nrow = length(1:100), ncol =
length(1:5)*length(chars))
k = 0
for(i in seq(1:length(1:5))){
  for(j in seq(1:length(chars))){
k = k+1
matrix1[,k] = c(1:100)[k]
  }
}


Also how could you possibly use such level of code obfuscation to
perform such simple initialization of your matrix?

My 1st advice would be that you slow down and take the time to
compare
seq(1:length(1:5)) with 1:length(1:5) with 1:5. It will be a great
learning experience!

As for initializing your matrix, what about doing

  ncol <- 5 * length(chars)
  matrix1 <- matrix(seq_len(ncol), nrow=100, ncol=ncol, byrow=TRUE)

instead?

Then set the colnames with:

  colnames(matrix1) <- paste(rep(1:5, length(chars)), chars,
sep="-")


or maybe

  colnames(matrix1) <- paste(rep(1:5, each=length(chars)), chars,
sep="-")

is what you're after.

H.



Cheers,
H.


[[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.



--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fredhutch.org 
Phone:  (206) 667-5791 
Fax:(206) 667-1319 




--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fredhutch.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

__
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] how to create column names for the matrix

2016-07-27 Thread ruipbarradas
Hello,

Try

chars = c('A','B','C','D')
matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
k = 0
for(i in 1:length(1:5)){  # or more simply just for(i in 1:5)
  for(j in 1:length(chars)){
    k = k+1
    matrix1[,k] = k
  }
}
matrix1
tmp <- expand.grid(chars, 1:5, stringsAsFactors = FALSE)
nms <- paste(tmp[[2]], tmp[[1]], sep = "-")
rm(tmp)
colnames(matrix1) <- nms

head(matrix1)

Hope this helps,

Rui Barradas
 

Citando lily li :

> Hi all,
>
> I want to ask that how to create column names for a matrix. For example,
> the matrix below, the column names should be: 1-A, 1-B, 1-C, 1-D, 2-A, 2-B,
> 2-C, 2-D, 3-A, etc. Thanks for your help.
>
> chars = c('A','B','C','D')
> matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
> k = 0
> for(i in seq(1:length(1:5))){
> for(j in seq(1:length(chars))){
>    k = k+1
>    matrix1[,k] = c(1:100)[k]
> }
> }
>
>         [[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.htmland 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.

Re: [R] how to create column names for the matrix

2016-07-27 Thread lily li
If replace 1:5 to char2 = c('east','west','south','north','central'), how
to put on column names with the original structure? Thanks again.


On Wed, Jul 27, 2016 at 12:40 PM, Hervé Pagès  wrote:

> Hi,
>
>
> On 07/27/2016 11:17 AM, lily li wrote:
>
>> Hi all,
>>
>> I want to ask that how to create column names for a matrix. For example,
>> the matrix below, the column names should be: 1-A, 1-B, 1-C, 1-D, 2-A,
>> 2-B,
>> 2-C, 2-D, 3-A, etc. Thanks for your help.
>>
>> chars = c('A','B','C','D')
>> matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
>> k = 0
>> for(i in seq(1:length(1:5))){
>>   for(j in seq(1:length(chars))){
>> k = k+1
>> matrix1[,k] = c(1:100)[k]
>>   }
>> }
>>
>
> Also how could you possibly use such level of code obfuscation to
> perform such simple initialization of your matrix?
>
> My 1st advice would be that you slow down and take the time to compare
> seq(1:length(1:5)) with 1:length(1:5) with 1:5. It will be a great
> learning experience!
>
> As for initializing your matrix, what about doing
>
>   ncol <- 5 * length(chars)
>   matrix1 <- matrix(seq_len(ncol), nrow=100, ncol=ncol, byrow=TRUE)
>
> instead?
>
> Then set the colnames with:
>
>   colnames(matrix1) <- paste(rep(1:5, length(chars)), chars, sep="-")
>
> Cheers,
> H.
>
>
>> [[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.
>>
>>
> --
> Hervé Pagès
>
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
>
> E-mail: hpa...@fredhutch.org
> Phone:  (206) 667-5791
> Fax:(206) 667-1319
>

[[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.

Re: [R] how to create column names for the matrix

2016-07-27 Thread lily li
Thanks. I shorten a more complex matrix to this example, but use the
original structure of the code. The original matrix has all characters
instead of 1:5.


On Wed, Jul 27, 2016 at 12:42 PM, Hervé Pagès  wrote:

> On 07/27/2016 11:40 AM, Hervé Pagès wrote:
>
>> Hi,
>>
>> On 07/27/2016 11:17 AM, lily li wrote:
>>
>>> Hi all,
>>>
>>> I want to ask that how to create column names for a matrix. For example,
>>> the matrix below, the column names should be: 1-A, 1-B, 1-C, 1-D, 2-A,
>>> 2-B,
>>> 2-C, 2-D, 3-A, etc. Thanks for your help.
>>>
>>> chars = c('A','B','C','D')
>>> matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
>>> k = 0
>>> for(i in seq(1:length(1:5))){
>>>   for(j in seq(1:length(chars))){
>>> k = k+1
>>> matrix1[,k] = c(1:100)[k]
>>>   }
>>> }
>>>
>>
>> Also how could you possibly use such level of code obfuscation to
>> perform such simple initialization of your matrix?
>>
>> My 1st advice would be that you slow down and take the time to compare
>> seq(1:length(1:5)) with 1:length(1:5) with 1:5. It will be a great
>> learning experience!
>>
>> As for initializing your matrix, what about doing
>>
>>   ncol <- 5 * length(chars)
>>   matrix1 <- matrix(seq_len(ncol), nrow=100, ncol=ncol, byrow=TRUE)
>>
>> instead?
>>
>> Then set the colnames with:
>>
>>   colnames(matrix1) <- paste(rep(1:5, length(chars)), chars, sep="-")
>>
>
> or maybe
>
>   colnames(matrix1) <- paste(rep(1:5, each=length(chars)), chars, sep="-")
>
> is what you're after.
>
> H.
>
>
>
>> Cheers,
>> H.
>>
>>
>>> [[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.
>>>
>>>
>>
> --
> Hervé Pagès
>
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
>
> E-mail: hpa...@fredhutch.org
> Phone:  (206) 667-5791
> Fax:(206) 667-1319
>

[[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.

Re: [R] how to create column names for the matrix

2016-07-27 Thread Hervé Pagès

On 07/27/2016 11:40 AM, Hervé Pagès wrote:

Hi,

On 07/27/2016 11:17 AM, lily li wrote:

Hi all,

I want to ask that how to create column names for a matrix. For example,
the matrix below, the column names should be: 1-A, 1-B, 1-C, 1-D, 2-A,
2-B,
2-C, 2-D, 3-A, etc. Thanks for your help.

chars = c('A','B','C','D')
matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
k = 0
for(i in seq(1:length(1:5))){
  for(j in seq(1:length(chars))){
k = k+1
matrix1[,k] = c(1:100)[k]
  }
}


Also how could you possibly use such level of code obfuscation to
perform such simple initialization of your matrix?

My 1st advice would be that you slow down and take the time to compare
seq(1:length(1:5)) with 1:length(1:5) with 1:5. It will be a great
learning experience!

As for initializing your matrix, what about doing

  ncol <- 5 * length(chars)
  matrix1 <- matrix(seq_len(ncol), nrow=100, ncol=ncol, byrow=TRUE)

instead?

Then set the colnames with:

  colnames(matrix1) <- paste(rep(1:5, length(chars)), chars, sep="-")


or maybe

  colnames(matrix1) <- paste(rep(1:5, each=length(chars)), chars, sep="-")

is what you're after.

H.



Cheers,
H.



[[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.





--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fredhutch.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

__
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] how to create column names for the matrix

2016-07-27 Thread Hervé Pagès

Hi,

On 07/27/2016 11:17 AM, lily li wrote:

Hi all,

I want to ask that how to create column names for a matrix. For example,
the matrix below, the column names should be: 1-A, 1-B, 1-C, 1-D, 2-A, 2-B,
2-C, 2-D, 3-A, etc. Thanks for your help.

chars = c('A','B','C','D')
matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
k = 0
for(i in seq(1:length(1:5))){
  for(j in seq(1:length(chars))){
k = k+1
matrix1[,k] = c(1:100)[k]
  }
}


Also how could you possibly use such level of code obfuscation to
perform such simple initialization of your matrix?

My 1st advice would be that you slow down and take the time to compare
seq(1:length(1:5)) with 1:length(1:5) with 1:5. It will be a great
learning experience!

As for initializing your matrix, what about doing

  ncol <- 5 * length(chars)
  matrix1 <- matrix(seq_len(ncol), nrow=100, ncol=ncol, byrow=TRUE)

instead?

Then set the colnames with:

  colnames(matrix1) <- paste(rep(1:5, length(chars)), chars, sep="-")

Cheers,
H.



[[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.



--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fredhutch.org
Phone:  (206) 667-5791
Fax:(206) 667-1319

__
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] how to create column names for the matrix

2016-07-27 Thread lily li
Hi all,

I want to ask that how to create column names for a matrix. For example,
the matrix below, the column names should be: 1-A, 1-B, 1-C, 1-D, 2-A, 2-B,
2-C, 2-D, 3-A, etc. Thanks for your help.

chars = c('A','B','C','D')
matrix1 = matrix(nrow = length(1:100), ncol = length(1:5)*length(chars))
k = 0
for(i in seq(1:length(1:5))){
  for(j in seq(1:length(chars))){
k = k+1
matrix1[,k] = c(1:100)[k]
  }
}

[[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.