Re: [R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2

2023-07-03 Thread Rui Barradas

Às 20:55 de 03/07/2023, Rui Barradas escreveu:

Às 20:26 de 03/07/2023, Sorkin, John escreveu:

Jeff,
Again my thanks for your guidance.
I replaced dimnames(myvalues)<-list(NULL,c(zzz))
with
colnames(myvalues)<-zzz
and get the same error,
Error in dimnames(x) <- dn :
   length of 'dimnames' [2] not equal to array extent
It appears that I am creating the string zzz in a manner that is not 
compatable with either

dimnames(myvalues)<-list(NULL,c(zzz))
or
colnames(myvalues)<-zzz

I think I need to modify the way I create the string zzz.

# 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)<-zzz

From: Jeff Newmiller 
Sent: Monday, July 3, 2023 2:45 PM
To: Sorkin, John
Cc: r-help@r-project.org
Subject: Re: [R]  Create matrix with column names wiht the same prefix 
 and that end in 1, 2


I really think you should read that help page.  colnames() accesses 
the second element of dimnames() directly.


On July 3, 2023 11:39:37 AM PDT, "Sorkin, John" 
 wrote:

Jeff,
Thank you for your reply.
I should have said with dim names not column names. I want the Mateix 
to have dim names, no row names, dim names j, k, xxx1, xxx2.


John

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and 
Geriatric Medicine

Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above 
prior to faxing)


On Jul 3, 2023, at 2:11 PM, Jeff Newmiller  
wrote:


?colnames

On July 3, 2023 11:00:32 AM PDT, "Sorkin, John" 
 wrote:
I am trying to create an array, myvalues, having 2 rows and 4 
columns, where the column names are j,k,xxx1,xxx2. The code below 
fails, with the following error, "Error in dimnames(myvalues) <- 
list(NULL, zzz) :

length of 'dimnames' [2] not equal to array extent"

Please help me get the code to work.

Thank you,
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,zzz)


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

--
Sent from my phone. Please excuse my brevity.

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


--
Sent from my phone. Please excuse my brevity.
__
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.

Hello,

I should have pointed out in my answer that you are inded creating the 
names vector in a (very) wrong way. When in the loop you paste string 
and name you create one vector of length 1. When the loop ends, you have 
" xxx1 xxx2", not two names.



string=""
for (j in 1:2){
   name <- paste("xxx",j,sep="")
   string <- paste(string,name)
   print(string)
}
#> [1] " xxx1"
#> [1] " xxx1 xxx2"
# Creation of xxx1 and xxx2 works
string
#> [1] " xxx1 xxx2"



Quoting the comment above,

   Creation of xxx1 and xxx2 works

No, it does not!
And then you paste again, adding two extra letters to one string

zzz <- paste("j","k",string)


This zzz also is of length 1, check it.


With a loop the right way would be any of

# 1. concatenate the current

Re: [R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2

2023-07-03 Thread Rui Barradas

Às 20:26 de 03/07/2023, Sorkin, John escreveu:

Jeff,
Again my thanks for your guidance.
I replaced dimnames(myvalues)<-list(NULL,c(zzz))
with
colnames(myvalues)<-zzz
and get the same error,
Error in dimnames(x) <- dn :
   length of 'dimnames' [2] not equal to array extent
It appears that I am creating the string zzz in a manner that is not compatable 
with either
dimnames(myvalues)<-list(NULL,c(zzz))
or
colnames(myvalues)<-zzz

I think I need to modify the way I create the string zzz.

# 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)<-zzz

From: Jeff Newmiller 
Sent: Monday, July 3, 2023 2:45 PM
To: Sorkin, John
Cc: r-help@r-project.org
Subject: Re: [R]  Create matrix with column names wiht the same prefix  and 
that end in 1, 2

I really think you should read that help page.  colnames() accesses the second 
element of dimnames() directly.

On July 3, 2023 11:39:37 AM PDT, "Sorkin, John"  
wrote:

Jeff,
Thank you for your reply.
I should have said with dim names not column names. I want the Mateix to have 
dim names, no row names, dim names j, k, xxx1, xxx2.

John

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric 
Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to 
faxing)

On Jul 3, 2023, at 2:11 PM, Jeff Newmiller  wrote:

?colnames

On July 3, 2023 11:00:32 AM PDT, "Sorkin, John"  
wrote:
I am trying to create an array, myvalues, having 2 rows and 4 columns, where the column 
names are j,k,xxx1,xxx2. The code below fails, with the following error, "Error in 
dimnames(myvalues) <- list(NULL, zzz) :
length of 'dimnames' [2] not equal to array extent"

Please help me get the code to work.

Thank you,
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,zzz)


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

--
Sent from my phone. Please excuse my brevity.

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


--
Sent from my phone. Please excuse my brevity.
__
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.

Hello,

I should have pointed out in my answer that you are inded creating the 
names vector in a (very) wrong way. When in the loop you paste string 
and name you create one vector of length 1. When the loop ends, you have 
" xxx1 xxx2", not two names.



string=""
for (j in 1:2){
  name <- paste("xxx",j,sep="")
  string <- paste(string,name)
  print(string)
}
#> [1] " xxx1"
#> [1] " xxx1 xxx2"
# Creation of xxx1 and xxx2 works
string
#> [1] " xxx1 xxx2"



Quoting the comment above,

  Creation of xxx1 and xxx2 works

No, it does not!
And then you paste again, adding two extra letters to one string

zzz <- paste("j","k",string)


This zzz also is of length 1, check it.


With a loop the right way would be any of

# 1. concatenate the current name with string
string <- NULL   # or c(), perhaps more 

Re: [R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2

2023-07-03 Thread Sorkin, John
Jeff,
Again my thanks for your guidance.
I replaced dimnames(myvalues)<-list(NULL,c(zzz))
with
colnames(myvalues)<-zzz
and get the same error,
Error in dimnames(x) <- dn :
  length of 'dimnames' [2] not equal to array extent
It appears that I am creating the string zzz in a manner that is not compatable 
with either
dimnames(myvalues)<-list(NULL,c(zzz))
or
colnames(myvalues)<-zzz

I think I need to modify the way I create the string zzz.

# 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)<-zzz

From: Jeff Newmiller 
Sent: Monday, July 3, 2023 2:45 PM
To: Sorkin, John
Cc: r-help@r-project.org
Subject: Re: [R]  Create matrix with column names wiht the same prefix  and 
that end in 1, 2

I really think you should read that help page.  colnames() accesses the second 
element of dimnames() directly.

On July 3, 2023 11:39:37 AM PDT, "Sorkin, John"  
wrote:
>Jeff,
>Thank you for your reply.
>I should have said with dim names not column names. I want the Mateix to have 
>dim names, no row names, dim names j, k, xxx1, xxx2.
>
>John
>
>John David Sorkin M.D., Ph.D.
>Professor of Medicine
>Chief, Biostatistics and Informatics
>University of Maryland School of Medicine Division of Gerontology and 
>Geriatric Medicine
>Baltimore VA Medical Center
>10 North Greene Street
>GRECC (BT/18/GR)
>Baltimore, MD 21201-1524
>(Phone) 410-605-7119
>(Fax) 410-605-7913 (Please call phone number above prior to 
>faxing)
>
>On Jul 3, 2023, at 2:11 PM, Jeff Newmiller  wrote:
>
>?colnames
>
>On July 3, 2023 11:00:32 AM PDT, "Sorkin, John"  
>wrote:
>I am trying to create an array, myvalues, having 2 rows and 4 columns, where 
>the column names are j,k,xxx1,xxx2. The code below fails, with the following 
>error, "Error in dimnames(myvalues) <- list(NULL, zzz) :
>length of 'dimnames' [2] not equal to array extent"
>
>Please help me get the code to work.
>
>Thank you,
>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,zzz)
>
>
>__
>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.
>
>--
>Sent from my phone. Please excuse my brevity.
>
>__
>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.

--
Sent from my phone. Please excuse my brevity.
__
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 matrix with column names wiht the same prefix xxxx and that end in 1, 2

2023-07-03 Thread Jeff Newmiller
I really think you should read that help page.  colnames() accesses the second 
element of dimnames() directly.

On July 3, 2023 11:39:37 AM PDT, "Sorkin, John"  
wrote:
>Jeff,
>Thank you for your reply.
>I should have said with dim names not column names. I want the Mateix to have 
>dim names, no row names, dim names j, k, xxx1, xxx2.
>
>John
>
>John David Sorkin M.D., Ph.D.
>Professor of Medicine
>Chief, Biostatistics and Informatics
>University of Maryland School of Medicine Division of Gerontology and 
>Geriatric Medicine
>Baltimore VA Medical Center
>10 North Greene Street
>GRECC (BT/18/GR)
>Baltimore, MD 21201-1524
>(Phone) 410-605-7119
>(Fax) 410-605-7913 (Please call phone number above prior to 
>faxing)
>
>On Jul 3, 2023, at 2:11 PM, Jeff Newmiller  wrote:
>
>?colnames
>
>On July 3, 2023 11:00:32 AM PDT, "Sorkin, John"  
>wrote:
>I am trying to create an array, myvalues, having 2 rows and 4 columns, where 
>the column names are j,k,xxx1,xxx2. The code below fails, with the following 
>error, "Error in dimnames(myvalues) <- list(NULL, zzz) :
>length of 'dimnames' [2] not equal to array extent"
>
>Please help me get the code to work.
>
>Thank you,
>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,zzz)
>
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=y8kLruSvrjxQLegbbPNMMl665EEApCgiSOq%2BEmhQfNE%3D=0
>PLEASE do read the posting guide 
>https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=HBUMNAeG1KurerS2DAhKxxZVRs71TSF0YJSGjP%2FCixA%3D=0
>and provide commented, minimal, self-contained, reproducible code.
>
>--
>Sent from my phone. Please excuse my brevity.
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=y8kLruSvrjxQLegbbPNMMl665EEApCgiSOq%2BEmhQfNE%3D=0
>PLEASE do read the posting guide 
>https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=HBUMNAeG1KurerS2DAhKxxZVRs71TSF0YJSGjP%2FCixA%3D=0
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.

__
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 matrix with column names wiht the same prefix xxxx and that end in 1, 2

2023-07-03 Thread Sorkin, John
Jeff,
Thank you for your reply.
I should have said with dim names not column names. I want the Mateix to have 
dim names, no row names, dim names j, k, xxx1, xxx2.

John

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric 
Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to 
faxing)

On Jul 3, 2023, at 2:11 PM, Jeff Newmiller  wrote:

?colnames

On July 3, 2023 11:00:32 AM PDT, "Sorkin, John"  
wrote:
I am trying to create an array, myvalues, having 2 rows and 4 columns, where 
the column names are j,k,xxx1,xxx2. The code below fails, with the following 
error, "Error in dimnames(myvalues) <- list(NULL, zzz) :
length of 'dimnames' [2] not equal to array extent"

Please help me get the code to work.

Thank you,
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,zzz)


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=y8kLruSvrjxQLegbbPNMMl665EEApCgiSOq%2BEmhQfNE%3D=0
PLEASE do read the posting guide 
https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=HBUMNAeG1KurerS2DAhKxxZVRs71TSF0YJSGjP%2FCixA%3D=0
and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=y8kLruSvrjxQLegbbPNMMl665EEApCgiSOq%2BEmhQfNE%3D=0
PLEASE do read the posting guide 
https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html=05%7C01%7CJSorkin%40som.umaryland.edu%7C4347c6a62c4b4956756708db7bf0ea2b%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638240046889096206%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=HBUMNAeG1KurerS2DAhKxxZVRs71TSF0YJSGjP%2FCixA%3D=0
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.


Re: [R] Create matrix with column names wiht the same prefix xxxx and that end in 1, 2

2023-07-03 Thread Rui Barradas

Às 19:00 de 03/07/2023, Sorkin, John escreveu:

I am trying to create an array, myvalues, having 2 rows and 4 columns, where the column 
names are j,k,xxx1,xxx2. The code below fails, with the following error, "Error in 
dimnames(myvalues) <- list(NULL, zzz) :
   length of 'dimnames' [2] not equal to array extent"

Please help me get the code to work.

Thank you,
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,zzz)


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

Hello,

You don't need so many calls to paste, one is enough.
And you don't need the for loop at all, paste and paste0 are vectorized.



myvalues <- matrix(nrow=2,ncol=4)

cnames <- paste0("xxx", 1:2)
cnames
# [1] "xxx1" "xxx2"

colnames(myvalues) <- c("j", "k", cnames)



Hope this helps,

Rui Barradas

__
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 matrix with column names wiht the same prefix xxxx and that end in 1, 2

2023-07-03 Thread Jeff Newmiller
?colnames

On July 3, 2023 11:00:32 AM PDT, "Sorkin, John"  
wrote:
>I am trying to create an array, myvalues, having 2 rows and 4 columns, where 
>the column names are j,k,xxx1,xxx2. The code below fails, with the following 
>error, "Error in dimnames(myvalues) <- list(NULL, zzz) : 
>  length of 'dimnames' [2] not equal to array extent" 
>
>Please help me get the code to work.
>
>Thank you,
>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,zzz)
>
>
>__
>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.

-- 
Sent from my phone. Please excuse my brevity.

__
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 matrix with column names wiht the same prefix xxxx and that end in 1, 2

2023-07-03 Thread Sorkin, John
I am trying to create an array, myvalues, having 2 rows and 4 columns, where 
the column names are j,k,xxx1,xxx2. The code below fails, with the following 
error, "Error in dimnames(myvalues) <- list(NULL, zzz) : 
  length of 'dimnames' [2] not equal to array extent" 

Please help me get the code to work.

Thank you,
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,zzz)


__
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 Matrix with Float32 values

2011-06-01 Thread Chris English

Dear R_Help:
The following gives me a matrix with integer values.
z= matrix(rep(10:1, each= 10), ncol= 10, byrow=TRUE) str(z) int [1:10, 1:10] 
10 9 8 7 6 5 4 3 2 1 ...
How do I specify that I want Float32 values instead.
Thanks,Chris  
__
R-help@r-project.org mailing list
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 Matrix with Float32 values

2011-06-01 Thread Peter Ehlers

On 2011-06-01 09:16, Chris English wrote:


Dear R_Help:
The following gives me a matrix with integer values.
z= matrix(rep(10:1, each= 10), ncol= 10, byrow=TRUE)  str(z) int [1:10, 1:10] 
10 9 8 7 6 5 4 3 2 1 ...
How do I specify that I want Float32 values instead.
Thanks,Chris


Have you tried adding 0? Or you could use the seq() function.
I assume that you have a reason for wanting this.

Peter Ehlers


__
R-help@r-project.org mailing list
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
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 Matrix with Float32 values

2011-06-01 Thread Duncan Murdoch

On 01/06/2011 12:16 PM, Chris English wrote:


Dear R_Help:
The following gives me a matrix with integer values.
z= matrix(rep(10:1, each= 10), ncol= 10, byrow=TRUE)  str(z) int [1:10, 1:10] 
10 9 8 7 6 5 4 3 2 1 ...
How do I specify that I want Float32 values instead.


You can't.  R doesn't support that type.

If you want to pass it to an external function that is expecting C 
single type, you can use as.single(x).  That won't convert it to single 
precision, but it will cause it to be copied to a single precision array 
whenever you call .C or .Fortran.


Duncan Murdoch

__
R-help@r-project.org mailing list
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 Matrix of 2 Dim from two vectors

2010-11-08 Thread abotaha

Hello, 

I have two data. 

x-c(1, 2, 3) 
y-c(4,5,6) 

How do i create matrix of 3 by 3 from this two, such that  

 (1,4)  (1,5)  (1,6)
 (2,4)  (2,5)  (2,6)
 (3,4)  (3,5)  (3,6)

I tried some thing like this:

xy - as.data.frame(c(0,0,0), dim=c(3,3))
for(i in 1:3)
for(j in 1:3)
xy[i][j]-c(x[i],y[j])

but i got errors..
any help would appreciate




-- 
View this message in context: 
http://r.789695.n4.nabble.com/Create-Matrix-of-2-Dim-from-two-vectors-tp3031718p3031718.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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 Matrix of 2 Dim from two vectors

2010-11-08 Thread Vaiva P
Try
 x1-matrix(1,3,1)%x%x
 y1-y%x%matrix(1,3,1)

Z-cbind(x1,y1)
And later you need to move towards list and matrix


On Mon, Nov 8, 2010 at 11:15 AM, abotaha yaseen0...@gmail.com wrote:

 Hello,

 I have two data.

 x-c(1, 2, 3)
 y-c(4,5,6)

 How do i create matrix of 3 by 3 from this two, such that

  (1,4)  (1,5)  (1,6)
  (2,4)  (2,5)  (2,6)
  (3,4)  (3,5)  (3,6)

 I tried some thing like this:

 xy - as.data.frame(c(0,0,0), dim=c(3,3))
 for(i in 1:3)
 for(j in 1:3)
 xy[i][j]-c(x[i],y[j])

 but i got errors..
 any help would appreciate




 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Create-Matrix-of-2-Dim-from-two-vectors-tp3031718p3031718.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 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
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 Matrix of 2 Dim from two vectors

2010-11-08 Thread Ivan Calandra

Hi,

First, create a MATRIX of the correct size:
xy - matrix(nrow=3, ncol=3)

Then, in your for loop, you need to index each cell correctly, like this:
xy[i,j]

Finally, if you want to assign 1,4 to each cell, you need to paste 
x[i] and y[j] together, like this:

xy[i,j]-paste(x[i],y[j], sep=,)

With the brackets if you want:
xy[i,j]-paste((, x[i], ,, y[j], ), sep=)

Or easier like this:
xy - matrix(rep(x,3),nrow=3, ncol=3)
apply(xy, 1, FUN=paste, y, sep=,)

HTH,
Ivan

Le 11/8/2010 11:15, abotaha a écrit :

Hello,

I have two data.

x-c(1, 2, 3)
y-c(4,5,6)

How do i create matrix of 3 by 3 from this two, such that

  (1,4)  (1,5)  (1,6)
  (2,4)  (2,5)  (2,6)
  (3,4)  (3,5)  (3,6)

I tried some thing like this:

xy- as.data.frame(c(0,0,0), dim=c(3,3))
for(i in 1:3)
for(j in 1:3)
xy[i][j]-c(x[i],y[j])

but i got errors..
any help would appreciate






--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

__
R-help@r-project.org mailing list
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 Matrix of 2 Dim from two vectors

2010-11-08 Thread Dennis Murphy
Hi:

If you want the literal character strings, this works:

 x-c(1, 2, 3)
 y-c(4,5,6)
 outer(x, y, function(x, y) paste('(', x, ',', y, ')', sep = '') )
 [,1][,2][,3]
[1,] (1,4) (1,5) (1,6)
[2,] (2,4) (2,5) (2,6)
[3,] (3,4) (3,5) (3,6)

However, I have the sense you want to use the values of x and y as
coordinates in a function. This is also a job for outer(); e.g.,

f - function(x, y) x + 0.5 * y
outer(x, y, f)
 [,1] [,2] [,3]
[1,]3  3.54
[2,]4  4.55
[3,]5  5.56

If you want to apply a two-dimensional function to a pair of vectors,
outer() may be what you're after.

HTH,
Dennis


On Mon, Nov 8, 2010 at 2:15 AM, abotaha yaseen0...@gmail.com wrote:


 Hello,

 I have two data.

 x-c(1, 2, 3)
 y-c(4,5,6)

 How do i create matrix of 3 by 3 from this two, such that

  (1,4)  (1,5)  (1,6)
  (2,4)  (2,5)  (2,6)
  (3,4)  (3,5)  (3,6)

 I tried some thing like this:

 xy - as.data.frame(c(0,0,0), dim=c(3,3))
 for(i in 1:3)
 for(j in 1:3)
 xy[i][j]-c(x[i],y[j])

 but i got errors..
 any help would appreciate




 --
 View this message in context:
 http://r.789695.n4.nabble.com/Create-Matrix-of-2-Dim-from-two-vectors-tp3031718p3031718.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 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
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 Matrix of 2 Dim from two vectors

2010-11-08 Thread abotaha

Thanks a lot guys...all of your solution are useful, and good.
once again thanks.


-- 
View this message in context: 
http://r.789695.n4.nabble.com/Create-Matrix-of-2-Dim-from-two-vectors-tp3031718p3031797.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
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 matrix with subset from unlist

2010-02-01 Thread Muhammad Rahiz

Hello all,

Thanks for all your replies.

Usually, when I make a post to the R-mailing list, I would keep on 
trying to get the solution myself rather than waiting for one. At times, 
I was able to derive my own solution. This would explain why my solution 
and that of Dennis's produces the same result - not that I totally 
ignore the method of his...


Anyway, I did manage to create the matrix I require. But there is still 
the recurring problem of  (list) object cannot be coerced to type 
'double'   in further analysis using the dataset. I thought I  could 
resolve it by changing converting to matrix. Seems not.


Given, the following, is there any other way I can define y other that 
using list()? Seems that producing a list of matrices does not work.



y -
for (i in 1:32){
y[[i]] - matrix(xx[c(1:4)],2,2)
}


Muhammad 






Dennis Murphy wrote:

Correct me if I'm wrong, but isn't this the solution I gave??

On Fri, Jan 29, 2010 at 9:43 AM, Muhammad Rahiz 
muhammad.ra...@ouce.ox.ac.uk mailto:muhammad.ra...@ouce.ox.ac.uk 
wrote:


Thanks David  Dennis,

I may have found something.

Given that the object xx is the product of unlist(x), to create a
2x2 matrix with subsets, I could do,

 y - matrix(xx[c(1:4)], 2, 2).

First object named y...
 


This returns,


[,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

If I do,

 y2 - matrix(xx[c(5:8)],2,2)


second object named y2

it returns,


[,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4


And I presume you want to do the same with the remaining 30 matrices,
assigning them to different objects. That is *precisely* what my solution
provided. Run it, observe the results and tell me what it is that 
differs from

what you want, because I don't see it.

Dennis
 


The results are exactly what I want to achieve.

The question is, how can I incorporate the increment in a for loop
so that it becomes

c(1:4)
c(5:8)
c(9:12) and so on

How should I modify this code?

y -# typeof ? for (i in 1:32){
y[[i]] - matrix(xx[c(1:4)],2,2)
}


Muhammad


David Winsemius wrote:

On Jan 29, 2010, at 9:45 AM, Dennis Murphy wrote:

 


Hi:

The problem, I'm guessing, is that you need to assign each
of the  matrices
to an object.
There's undoubtedly a slick apply family solution for this
(which I  want to
see, BTW!),
   



I don't have a method that would assign names but you could
populate  an array of sufficient size and dimension. I
populated a three-element  list with his data:

  dput(x)
list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)),
.Names =  c(V1,
V2), class = data.frame, row.names = c(1, 2)),
structure(list(
V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)),
structure(list(
V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)))

  xx - array( , dim=c(2,2,3))

  xx[,,1:3] - sapply(x, data.matrix)
  xx
, , 1

  [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

, , 2

  [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

, , 3

 [,1]  [,2]
[1,] 29.0 -38.1
[2,] -3.4  55.1

Without the more complex structure ready to accept the 2x2
arrays I  got this:

  sapply(x, data.matrix)
  [,1]  [,2]  [,3]
[1,] -27.3  14.4  29.0
[2,]  29.0 -38.1  -3.4
[3,]  14.4  29.0 -38.1
[4,] -38.1  -3.4  55.1

 





__
R-help@r-project.org mailing list
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 matrix with subset from unlist

2010-02-01 Thread David Winsemius


On Feb 1, 2010, at 9:38 AM, Muhammad Rahiz wrote:


Hello all,

Thanks for all your replies.

Usually, when I make a post to the R-mailing list, I would keep on  
trying to get the solution myself rather than waiting for one. At  
times, I was able to derive my own solution. This would explain why  
my solution and that of Dennis's produces the same result - not that  
I totally ignore the method of his...


Anyway, I did manage to create the matrix I require. But there is  
still the recurring problem of  (list) object cannot be coerced to  
type 'double'   in further analysis using the dataset. I thought I   
could resolve it by changing converting to matrix. Seems not.


Given, the following, is there any other way I can define y other  
that using list()? Seems that producing a list of matrices does not  
work.


I produced an array as an intermediate result in an earlier posting.  
If you defined the dimensions properly that would seem to be a  
sensible alternative.


 dput(x)
list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)), .Names =  
c(V1,

V2), class = data.frame, row.names = c(1, 2)), structure(list(
   V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)), structure(list(
   V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)))

 xx - array( , dim=c(2,2,3))
 xx[,,1:3] - sapply(x, data.matrix)

All you would need to do is change the 3's to 32.






y -
for (i in 1:32){
y[[i]] - matrix(xx[c(1:4)],2,2)
}


Muhammad




Dennis Murphy wrote:

Correct me if I'm wrong, but isn't this the solution I gave??

On Fri, Jan 29, 2010 at 9:43 AM, Muhammad Rahiz muhammad.ra...@ouce.ox.ac.uk 
 mailto:muhammad.ra...@ouce.ox.ac.uk wrote:


   Thanks David  Dennis,

   I may have found something.

   Given that the object xx is the product of unlist(x), to create a
   2x2 matrix with subsets, I could do,

y - matrix(xx[c(1:4)], 2, 2).

First object named y...

   This returns,


   [,1]  [,2]
   [1,] -27.3  14.4
   [2,]  29.0 -38.1

   If I do,

y2 - matrix(xx[c(5:8)],2,2)


second object named y2

   it returns,


   [,1] [,2]
   [1,]  14.4 29.0
   [2,] -38.1 -3.4


And I presume you want to do the same with the remaining 30 matrices,
assigning them to different objects. That is *precisely* what my  
solution
provided. Run it, observe the results and tell me what it is that  
differs from

what you want, because I don't see it.

Dennis

   The results are exactly what I want to achieve.

   The question is, how can I incorporate the increment in a for loop
   so that it becomes

   c(1:4)
   c(5:8)
   c(9:12) and so on

   How should I modify this code?

   y -# typeof ? for (i in 1:32){
   y[[i]] - matrix(xx[c(1:4)],2,2)
   }


   Muhammad


   David Winsemius wrote:

   On Jan 29, 2010, at 9:45 AM, Dennis Murphy wrote:


   Hi:

   The problem, I'm guessing, is that you need to assign each
   of the  matrices
   to an object.
   There's undoubtedly a slick apply family solution for this
   (which I  want to
   see, BTW!),


   I don't have a method that would assign names but you could
   populate  an array of sufficient size and dimension. I
   populated a three-element  list with his data:

 dput(x)
   list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)),
   .Names =  c(V1,
   V2), class = data.frame, row.names = c(1, 2)),
   structure(list(
   V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c(V1,
   V2), class = data.frame, row.names = c(1, 2)),
   structure(list(
   V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c(V1,
   V2), class = data.frame, row.names = c(1, 2)))

 xx - array( , dim=c(2,2,3))

 xx[,,1:3] - sapply(x, data.matrix)
 xx
   , , 1

 [,1]  [,2]
   [1,] -27.3  14.4
   [2,]  29.0 -38.1

   , , 2

 [,1] [,2]
   [1,]  14.4 29.0
   [2,] -38.1 -3.4

   , , 3

[,1]  [,2]
   [1,] 29.0 -38.1
   [2,] -3.4  55.1

   Without the more complex structure ready to accept the 2x2
   arrays I  got this:

 sapply(x, data.matrix)
 [,1]  [,2]  [,3]
   [1,] -27.3  14.4  29.0
   [2,]  29.0 -38.1  -3.4
   [3,]  14.4  29.0 -38.1
   [4,] -38.1  -3.4  55.1





David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@r-project.org mailing list
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 matrix with subset from unlist

2010-01-29 Thread Muhammad Rahiz

Hello all,

I'm trying to create a 2x2 matrix, 32 times after unlist() so that I can 
convert the list to matrix. I've looked through the R archive but 
couldn't find the answer. There is what I've done.



 f - system(ls *.txt, intern=TRUE)
 x - lapply(f, read.table)
 x
[[1]]
V1V2
1 -27.3  14.4
2  29.0 -38.1

[[2]]
V1   V2
1  14.4 29.0
2 -38.1 -3.4

[[3]]
   V1V2
1 29.0 -38.1
2 -3.4  55.1

[[4]]
V1   V2
1 -38.1 -3.4
2  55.1 -1.0

[[5]]
   V1   V2
1 -3.4 55.1
2 -1.0 21.9

[[6]]
   V1V2
1 55.1  -1.0
2 21.9 -10.9

...

 xx - unlist(x)
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-27.3  29.0  14.4 -38.1  14.4 -38.1  29.0  -3.4  29.0  -3.4 -38.1  55.1
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-38.1  55.1  -3.4  -1.0  -3.4  -1.0  55.1  21.9  55.1  21.9  -1.0 -10.9
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-1.0 -10.9  21.9  -7.8  21.9  -7.8 -10.9 -48.2 -10.9 -48.2  -7.8 -44.9
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-7.8 -44.9 -48.2 -43.8 -48.2 -43.8 -44.9 -10.3 -44.9 -10.3 -43.8  44.2
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-43.8  44.2 -10.3  -0.5 -10.3  -0.5  44.2  96.7  44.2  96.7  -0.5 -32.0
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-0.5 -32.0  96.7  -0.2  96.7  -0.2 -32.0 -38.6 -32.0 -38.6  -0.2  73.6
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-0.2  73.6 -38.6 -17.5 -38.6 -17.5  73.6 -57.8  73.6 -57.8 -17.5  10.7
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-17.5  10.7 -57.8 -33.4 -57.8 -33.4  10.7  46.1  10.7  46.1 -33.4  26.7
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-33.4  26.7  46.1 -37.3  46.1 -37.3  26.7   1.2  26.7   1.2 -37.3  36.3
 V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-37.3  36.3   1.2  39.6   1.2  39.6  36.3  31.0  36.3 -27.3  39.6  14.4
 V11   V12   V21   V22   V11   V12   V21   V22
39.6  29.0  31.0 -38.1  31.0  -3.4 -27.3  55.1


The output should be

[[1]]
 [,1]  [,2]
[1,]-27.314.4
[2,] 29.0-38.1

[[2]]
  [,1] [,2]
[1,]  14.429.0
[2,] -38.1-3.4

[[3]]
 [,1] [,2]
[1,]29.0-38.1
[2,]-3.4 55.1

...
Thanks and much appreciated!



Muhammad

__
R-help@r-project.org mailing list
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 matrix with subset from unlist

2010-01-29 Thread Dennis Murphy
Hi:

The problem, I'm guessing, is that you need to assign each of the matrices
to an object.
There's undoubtedly a slick apply family solution for this (which I want to
see, BTW!),
but here's the brute force method using a loop:

nms - paste('x', 1:32, sep = )
for(i in seq_along(nms)) assign(nms[i], x[[i]])

HTH,
Dennis

On Fri, Jan 29, 2010 at 6:30 AM, Muhammad Rahiz 
muhammad.ra...@ouce.ox.ac.uk wrote:

 Hello all,

 I'm trying to create a 2x2 matrix, 32 times after unlist() so that I can
 convert the list to matrix. I've looked through the R archive but couldn't
 find the answer. There is what I've done.


  f - system(ls *.txt, intern=TRUE)
  x - lapply(f, read.table)
  x
 [[1]]
V1V2
 1 -27.3  14.4
 2  29.0 -38.1

 [[2]]
V1   V2
 1  14.4 29.0
 2 -38.1 -3.4

 [[3]]
   V1V2
 1 29.0 -38.1
 2 -3.4  55.1

 [[4]]
V1   V2
 1 -38.1 -3.4
 2  55.1 -1.0

 [[5]]
   V1   V2
 1 -3.4 55.1
 2 -1.0 21.9

 [[6]]
   V1V2
 1 55.1  -1.0
 2 21.9 -10.9

 ...

  xx - unlist(x)
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -27.3  29.0  14.4 -38.1  14.4 -38.1  29.0  -3.4  29.0  -3.4 -38.1  55.1
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -38.1  55.1  -3.4  -1.0  -3.4  -1.0  55.1  21.9  55.1  21.9  -1.0 -10.9
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -1.0 -10.9  21.9  -7.8  21.9  -7.8 -10.9 -48.2 -10.9 -48.2  -7.8 -44.9
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -7.8 -44.9 -48.2 -43.8 -48.2 -43.8 -44.9 -10.3 -44.9 -10.3 -43.8  44.2
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -43.8  44.2 -10.3  -0.5 -10.3  -0.5  44.2  96.7  44.2  96.7  -0.5 -32.0
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -0.5 -32.0  96.7  -0.2  96.7  -0.2 -32.0 -38.6 -32.0 -38.6  -0.2  73.6
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -0.2  73.6 -38.6 -17.5 -38.6 -17.5  73.6 -57.8  73.6 -57.8 -17.5  10.7
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -17.5  10.7 -57.8 -33.4 -57.8 -33.4  10.7  46.1  10.7  46.1 -33.4  26.7
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -33.4  26.7  46.1 -37.3  46.1 -37.3  26.7   1.2  26.7   1.2 -37.3  36.3
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -37.3  36.3   1.2  39.6   1.2  39.6  36.3  31.0  36.3 -27.3  39.6  14.4
  V11   V12   V21   V22   V11   V12   V21   V22
 39.6  29.0  31.0 -38.1  31.0  -3.4 -27.3  55.1


 The output should be

 [[1]]
 [,1]  [,2]
 [1,]-27.314.4
 [2,] 29.0-38.1

 [[2]]
  [,1] [,2]
 [1,]  14.429.0
 [2,] -38.1-3.4

 [[3]]
 [,1] [,2]
 [1,]29.0-38.1
 [2,]-3.4 55.1

 ...
 Thanks and much appreciated!



 Muhammad

 __
 R-help@r-project.org mailing list
 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
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 matrix with subset from unlist

2010-01-29 Thread David Winsemius


On Jan 29, 2010, at 9:45 AM, Dennis Murphy wrote:


Hi:

The problem, I'm guessing, is that you need to assign each of the  
matrices

to an object.
There's undoubtedly a slick apply family solution for this (which I  
want to

see, BTW!),


I don't have a method that would assign names but you could populate  
an array of sufficient size and dimension. I populated a three-element  
list with his data:


 dput(x)
list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)), .Names =  
c(V1,

V2), class = data.frame, row.names = c(1, 2)), structure(list(
V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)), structure(list(
V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)))

 xx - array( , dim=c(2,2,3))

 xx[,,1:3] - sapply(x, data.matrix)
 xx
, , 1

  [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

, , 2

  [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

, , 3

 [,1]  [,2]
[1,] 29.0 -38.1
[2,] -3.4  55.1

Without the more complex structure ready to accept the 2x2 arrays I  
got this:


 sapply(x, data.matrix)
  [,1]  [,2]  [,3]
[1,] -27.3  14.4  29.0
[2,]  29.0 -38.1  -3.4
[3,]  14.4  29.0 -38.1
[4,] -38.1  -3.4  55.1

--
David.


but here's the brute force method using a loop:

nms - paste('x', 1:32, sep = )
for(i in seq_along(nms)) assign(nms[i], x[[i]])

HTH,
Dennis

On Fri, Jan 29, 2010 at 6:30 AM, Muhammad Rahiz 
muhammad.ra...@ouce.ox.ac.uk wrote:


Hello all,

I'm trying to create a 2x2 matrix, 32 times after unlist() so that  
I can
convert the list to matrix. I've looked through the R archive but  
couldn't

find the answer. There is what I've done.



f - system(ls *.txt, intern=TRUE)
x - lapply(f, read.table)
x

[[1]]
  V1V2
1 -27.3  14.4
2  29.0 -38.1

[[2]]
  V1   V2
1  14.4 29.0
2 -38.1 -3.4

[[3]]
 V1V2
1 29.0 -38.1
2 -3.4  55.1

[[4]]
  V1   V2
1 -38.1 -3.4
2  55.1 -1.0

[[5]]
 V1   V2
1 -3.4 55.1
2 -1.0 21.9

[[6]]
 V1V2
1 55.1  -1.0
2 21.9 -10.9

...


xx - unlist(x)

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-27.3  29.0  14.4 -38.1  14.4 -38.1  29.0  -3.4  29.0  -3.4 -38.1   
55.1

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-38.1  55.1  -3.4  -1.0  -3.4  -1.0  55.1  21.9  55.1  21.9  -1.0  
-10.9

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-1.0 -10.9  21.9  -7.8  21.9  -7.8 -10.9 -48.2 -10.9 -48.2  -7.8  
-44.9

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-7.8 -44.9 -48.2 -43.8 -48.2 -43.8 -44.9 -10.3 -44.9 -10.3 -43.8   
44.2

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-43.8  44.2 -10.3  -0.5 -10.3  -0.5  44.2  96.7  44.2  96.7  -0.5  
-32.0

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-0.5 -32.0  96.7  -0.2  96.7  -0.2 -32.0 -38.6 -32.0 -38.6  -0.2   
73.6

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-0.2  73.6 -38.6 -17.5 -38.6 -17.5  73.6 -57.8  73.6 -57.8 -17.5   
10.7

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-17.5  10.7 -57.8 -33.4 -57.8 -33.4  10.7  46.1  10.7  46.1 -33.4   
26.7

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-33.4  26.7  46.1 -37.3  46.1 -37.3  26.7   1.2  26.7   1.2 -37.3   
36.3

V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-37.3  36.3   1.2  39.6   1.2  39.6  36.3  31.0  36.3 -27.3  39.6   
14.4

V11   V12   V21   V22   V11   V12   V21   V22
39.6  29.0  31.0 -38.1  31.0  -3.4 -27.3  55.1


The output should be

[[1]]
   [,1]  [,2]
[1,]-27.314.4
[2,] 29.0-38.1

[[2]]
[,1] [,2]
[1,]  14.429.0
[2,] -38.1-3.4

[[3]]
   [,1] [,2]
[1,]29.0-38.1
[2,]-3.4 55.1

...
Thanks and much appreciated!



Muhammad

__
R-help@r-project.org mailing list
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
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.


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@r-project.org mailing list
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 matrix with subset from unlist

2010-01-29 Thread Muhammad Rahiz

Thanks David  Dennis,

I may have found something.

Given that the object xx is the product of unlist(x), to create a 2x2 
matrix with subsets, I could do,


 y - matrix(xx[c(1:4)], 2, 2).

This returns,

 [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

If I do,

 y2 - matrix(xx[c(5:8)],2,2)

it returns,

 [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

The results are exactly what I want to achieve.

The question is, how can I incorporate the increment in a for loop so that it 
becomes

c(1:4)
c(5:8)
c(9:12) and so on

How should I modify this code?

y - 		# typeof ? 
for (i in 1:32){

y[[i]] - matrix(xx[c(1:4)],2,2)
}


Muhammad 




David Winsemius wrote:

On Jan 29, 2010, at 9:45 AM, Dennis Murphy wrote:

  

Hi:

The problem, I'm guessing, is that you need to assign each of the  
matrices

to an object.
There's undoubtedly a slick apply family solution for this (which I  
want to

see, BTW!),



I don't have a method that would assign names but you could populate  
an array of sufficient size and dimension. I populated a three-element  
list with his data:


  dput(x)
list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)), .Names =  
c(V1,

V2), class = data.frame, row.names = c(1, 2)), structure(list(
 V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)), structure(list(
 V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)))

  xx - array( , dim=c(2,2,3))

  xx[,,1:3] - sapply(x, data.matrix)
  xx
, , 1

   [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

, , 2

   [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

, , 3

  [,1]  [,2]
[1,] 29.0 -38.1
[2,] -3.4  55.1

Without the more complex structure ready to accept the 2x2 arrays I  
got this:


  sapply(x, data.matrix)
   [,1]  [,2]  [,3]
[1,] -27.3  14.4  29.0
[2,]  29.0 -38.1  -3.4
[3,]  14.4  29.0 -38.1
[4,] -38.1  -3.4  55.1




__
R-help@r-project.org mailing list
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 matrix with subset from unlist

2010-01-29 Thread David Winsemius


On Jan 29, 2010, at 12:43 PM, Muhammad Rahiz wrote:


Thanks David  Dennis,

I may have found something.

Given that the object xx is the product of unlist(x), to create a  
2x2 matrix with subsets, I could do,


 y - matrix(xx[c(1:4)], 2, 2).

This returns,

[,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1


Much simpler to do:

 xx[ , , 1]
  [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1



If I do,

 y2 - matrix(xx[c(5:8)],2,2)

it returns,

[,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

The results are exactly what I want to achieve.

The question is, how can I incorporate the increment in a for loop  
so that it becomes


c(1:4)
c(5:8)
c(9:12) and so on





How should I modify this code?

y - # typeof ? for (i in 1:32){
y[[i]] - matrix(xx[c(1:4)],2,2)
}


I don't get it. You had the data in a list. You wanted it out of that  
list, and now you're going to put it back in another list???  ( y2  
would not be the same as y[[2]] )  What's wrong with xx[ , , 2]?





Muhammad


David Winsemius wrote:

On Jan 29, 2010, at 9:45 AM, Dennis Murphy wrote:



Hi:

The problem, I'm guessing, is that you need to assign each of the   
matrices

to an object.
There's undoubtedly a slick apply family solution for this (which  
I  want to

see, BTW!),



I don't have a method that would assign names but you could  
populate  an array of sufficient size and dimension. I populated a  
three-element  list with his data:


 dput(x)
list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)), .Names  
=  c(V1,
V2), class = data.frame, row.names = c(1, 2)),  
structure(list(

V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)),  
structure(list(

V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)))

 xx - array( , dim=c(2,2,3))

 xx[,,1:3] - sapply(x, data.matrix)
 xx
, , 1

  [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

, , 2

  [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

, , 3

 [,1]  [,2]
[1,] 29.0 -38.1
[2,] -3.4  55.1

Without the more complex structure ready to accept the 2x2 arrays  
I  got this:


 sapply(x, data.matrix)
  [,1]  [,2]  [,3]
[1,] -27.3  14.4  29.0
[2,]  29.0 -38.1  -3.4
[3,]  14.4  29.0 -38.1
[4,] -38.1  -3.4  55.1




David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@r-project.org mailing list
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 matrix with subset from unlist

2010-01-29 Thread Muhammad Rahiz
OK, I've got this. The output prints what I want, but I'm not sure if 
there will be problems in further analysis because the main idea is to 
convert the data from list to matrix. I'm quite concerned with how I 
define xx2.


   xx - unlist(x)   # Unlist  from lapply + read.table

   a - seq(1,128,by=4) # creates sequence for increment in loop

   xx2 - list() # Is this the correct definition?
for (z in 1:32){
xx2[[z]] - matrix(xx[c(a[z]:(a[z]+4))],2,2)
}

When I do,
mode(xx2)
[1] list

When I do,
xx3 - xx2[[1]] + 5   # simple test
mode(xx3)
numeric


Am I doing this right?


Muhammad

--

Muhammad Rahiz wrote:

Thanks David  Dennis,

I may have found something.

Given that the object xx is the product of unlist(x), to create a 2x2 
matrix with subsets, I could do,


  y - matrix(xx[c(1:4)], 2, 2).

This returns,

  [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

If I do,

  y2 - matrix(xx[c(5:8)],2,2)

it returns,

  [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

The results are exactly what I want to achieve.

The question is, how can I incorporate the increment in a for loop so that it 
becomes

c(1:4)
c(5:8)
c(9:12) and so on

How should I modify this code?

y - 		# typeof ? 
for (i in 1:32){

 y[[i]] - matrix(xx[c(1:4)],2,2)
}


Muhammad 




David Winsemius wrote:
  

On Jan 29, 2010, at 9:45 AM, Dennis Murphy wrote:

  


Hi:

The problem, I'm guessing, is that you need to assign each of the  
matrices

to an object.
There's undoubtedly a slick apply family solution for this (which I  
want to

see, BTW!),

  
I don't have a method that would assign names but you could populate  
an array of sufficient size and dimension. I populated a three-element  
list with his data:


  dput(x)
list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4, -38.1)), .Names =  
c(V1,

V2), class = data.frame, row.names = c(1, 2)), structure(list(
 V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)), structure(list(
 V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)))

  xx - array( , dim=c(2,2,3))

  xx[,,1:3] - sapply(x, data.matrix)
  xx
, , 1

   [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

, , 2

   [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

, , 3

  [,1]  [,2]
[1,] 29.0 -38.1
[2,] -3.4  55.1

Without the more complex structure ready to accept the 2x2 arrays I  
got this:


  sapply(x, data.matrix)
   [,1]  [,2]  [,3]
[1,] -27.3  14.4  29.0
[2,]  29.0 -38.1  -3.4
[3,]  14.4  29.0 -38.1
[4,] -38.1  -3.4  55.1





__
R-help@r-project.org mailing list
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
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 matrix with subset from unlist

2010-01-29 Thread David Winsemius


On Jan 29, 2010, at 1:07 PM, Muhammad Rahiz wrote:

OK, I've got this. The output prints what I want, but I'm not sure  
if there will be problems in further analysis because the main idea  
is to convert the data from list to matrix. I'm quite concerned with  
how I define xx2.


  xx - unlist(x)   # Unlist  from lapply + read.table

  a - seq(1,128,by=4) # creates sequence for increment in loop

  xx2 - list() # Is this the correct definition?

It will work.

for (z in 1:32){
xx2[[z]] - matrix(xx[c(a[z]:(a[z]+4))],2,2) # which would be a list  
of matrices

}



If you go back to your original posting, you could shortcut the whole  
process since you already had a list of 32 dataframes (lists) . That  
was the starting point. If a list is acceptable, then skip the  
intermediate array.


 class(x[[1]])
[1] data.frame
 class(lapply(x, data.matrix)[[1]])
[1] matrix

So just do this:

xx2 - lapply(x, data.matrix)  # a list of matrices

--
David.



When I do,
   mode(xx2)
   [1] list

When I do,
   xx3 - xx2[[1]] + 5   # simple test
   mode(xx3)
   numeric


Am I doing this right?


Muhammad

--

Muhammad Rahiz wrote:

Thanks David  Dennis,

I may have found something.

Given that the object xx is the product of unlist(x), to create a  
2x2 matrix with subsets, I could do,


 y - matrix(xx[c(1:4)], 2, 2).

This returns,

 [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

If I do,

 y2 - matrix(xx[c(5:8)],2,2)

it returns,

 [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

The results are exactly what I want to achieve.

The question is, how can I incorporate the increment in a for loop  
so that it becomes


c(1:4)
c(5:8)
c(9:12) and so on

How should I modify this code?

y - # typeof ? for (i in 1:32){
y[[i]] - matrix(xx[c(1:4)],2,2)
}


Muhammad


David Winsemius wrote:


On Jan 29, 2010, at 9:45 AM, Dennis Murphy wrote:



Hi:

The problem, I'm guessing, is that you need to assign each of  
the  matrices

to an object.
There's undoubtedly a slick apply family solution for this (which  
I  want to

see, BTW!),

I don't have a method that would assign names but you could  
populate  an array of sufficient size and dimension. I populated a  
three-element  list with his data:


 dput(x)
list(structure(list(V1 = c(-27.3, 29), V2 = c(14.4,  
-38.1)), .Names =  c(V1,
V2), class = data.frame, row.names = c(1, 2)),  
structure(list(

V1 = c(14.4, -38.1), V2 = c(29, -3.4)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)),  
structure(list(

V1 = c(29, -3.4), V2 = c(-38.1, 55.1)), .Names = c(V1,
V2), class = data.frame, row.names = c(1, 2)))

 xx - array( , dim=c(2,2,3))

 xx[,,1:3] - sapply(x, data.matrix)
 xx
, , 1

  [,1]  [,2]
[1,] -27.3  14.4
[2,]  29.0 -38.1

, , 2

  [,1] [,2]
[1,]  14.4 29.0
[2,] -38.1 -3.4

, , 3

 [,1]  [,2]
[1,] 29.0 -38.1
[2,] -3.4  55.1

Without the more complex structure ready to accept the 2x2 arrays  
I  got this:


 sapply(x, data.matrix)
  [,1]  [,2]  [,3]
[1,] -27.3  14.4  29.0
[2,]  29.0 -38.1  -3.4
[3,]  14.4  29.0 -38.1
[4,] -38.1  -3.4  55.1





__
R-help@r-project.org mailing list
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.



David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@r-project.org mailing list
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 Matrix from Loop of Vectors, Sort It and Pick Top-K

2008-06-19 Thread Gundala Viswanath
Hi,

I have the following dataset (simplified for example).

__DATA__
300.35  200.25 104.30
22.00 31.12 89.99
444.50 22.10 43.00
22.10 200.55 66.77

Now from that I wish to do the following:

1. Compute variance of each row
2. Pick top-2 row with highest variance
3. Store those selected rows for further processing

To achieve this, I tried to: a) read the table and compute
variance  for each row, b) append variance with its original
row in a vector, c) store a vector into multidimentional array (matrix),
d) sort that array. But I am stuck at the step (b).

Can anybody suggest what's the best way to achieve
my aim above?

This is the sample code I have so far (not working).

__BEGIN__

#data - read.table(testdata.txt)


# Is this a right way to initialize?
all.arr = NULL

for (gi in 1:nofrow) {
   gex - as.vector(data.matrix(data[gi,],rownames.force=FALSE))

   #compute variance
   gexvar - var(gex)

   # join variance with its original vector
   nvec - c(gexvar,gex)

   # I'm stuck here.This doesn't seem to work
   all.arr - data.frame(nvec)
}

print(all.arr)
__END__
--
 Gundala Viswanath
Jakarta - Indonesia

__
R-help@r-project.org mailing list
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 Matrix from Loop of Vectors, Sort It and Pick Top-K

2008-06-19 Thread Jorge Ivan Velez
Dear Gundala,

Try this:

# Data set
DF=read.table(textConnection(300.35  200.25 104.30
22.00 31.12 89.99
444.50 22.10 43.00
22.10 200.55 66.77),header=FALSE,sep=)

# Variances
VAR=apply(DF,1,var)

# Order
pos=order(VAR)

# Print VAR and pos
VAR
pos

# ordered VAR
VAR[pos]

# top-2 highest VAR
VAR[pos][3:4]


HTH,

Jorge




On Thu, Jun 19, 2008 at 10:59 AM, Gundala Viswanath [EMAIL PROTECTED]
wrote:

 Hi,

 I have the following dataset (simplified for example).

 __DATA__
 300.35  200.25 104.30
 22.00 31.12 89.99
 444.50 22.10 43.00
 22.10 200.55 66.77

 Now from that I wish to do the following:

 1. Compute variance of each row
 2. Pick top-2 row with highest variance
 3. Store those selected rows for further processing

 To achieve this, I tried to: a) read the table and compute
 variance  for each row, b) append variance with its original
 row in a vector, c) store a vector into multidimentional array (matrix),
 d) sort that array. But I am stuck at the step (b).

 Can anybody suggest what's the best way to achieve
 my aim above?

 This is the sample code I have so far (not working).

 __BEGIN__

 #data - read.table(testdata.txt)


 # Is this a right way to initialize?
 all.arr = NULL

 for (gi in 1:nofrow) {
   gex - as.vector(data.matrix(data[gi,],rownames.force=FALSE))

   #compute variance
   gexvar - var(gex)

   # join variance with its original vector
   nvec - c(gexvar,gex)

   # I'm stuck here.This doesn't seem to work
   all.arr - data.frame(nvec)
 }

 print(all.arr)
 __END__
 --
  Gundala Viswanath
 Jakarta - Indonesia

 __
 R-help@r-project.org mailing list
 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
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 Matrix from Loop of Vectors, Sort It and Pick Top-K

2008-06-19 Thread Marc Schwartz

on 06/19/2008 09:59 AM Gundala Viswanath wrote:

Hi,

I have the following dataset (simplified for example).

__DATA__
300.35  200.25 104.30
22.00 31.12 89.99
444.50 22.10 43.00
22.10 200.55 66.77

Now from that I wish to do the following:

1. Compute variance of each row
2. Pick top-2 row with highest variance
3. Store those selected rows for further processing

To achieve this, I tried to: a) read the table and compute
variance  for each row, b) append variance with its original
row in a vector, c) store a vector into multidimentional array (matrix),
d) sort that array. But I am stuck at the step (b).

Can anybody suggest what's the best way to achieve
my aim above?

This is the sample code I have so far (not working).

__BEGIN__

#data - read.table(testdata.txt)


# Is this a right way to initialize?
all.arr = NULL

for (gi in 1:nofrow) {
   gex - as.vector(data.matrix(data[gi,],rownames.force=FALSE))

   #compute variance
   gexvar - var(gex)

   # join variance with its original vector
   nvec - c(gexvar,gex)

   # I'm stuck here.This doesn't seem to work
   all.arr - data.frame(nvec)
}

print(all.arr)
__END__
--


If your data is contained in a data frame 'DF':

 DF
  V1 V2 V3
1 300.35 200.25 104.30
2  22.00  31.12  89.99
3 444.50  22.10  43.00
4  22.10 200.55  66.77


# Get row-wise variances and cbind() them to DF
 DF.var - cbind(DF, var = apply(DF, 1, var, na.rm = TRUE))

 DF.var
  V1 V2 V3   var
1 300.35 200.25 104.30  9610.336
2  22.00  31.12  89.99  1361.915
3 444.50  22.10  43.00 56676.803
4  22.10 200.55  66.77  8622.817


# Sort DF by 'var' using order()
 DF.var[order(DF.var$var, decreasing = TRUE), ]
  V1 V2 V3   var
3 444.50  22.10  43.00 56676.803
1 300.35 200.25 104.30  9610.336
4  22.10 200.55  66.77  8622.817
2  22.00  31.12  89.99  1361.915


To get the top 2, you can take a couple of approaches:

 DF.var[order(DF.var$var, decreasing = TRUE)[1:2], ]
  V1 V2V3   var
3 444.50  22.10  43.0 56676.803
1 300.35 200.25 104.3  9610.336


or

 head(DF.var[order(DF.var$var, decreasing = TRUE), ], 2)
  V1 V2V3   var
3 444.50  22.10  43.0 56676.803
1 300.35 200.25 104.3  9610.336


See ?cbind, ?apply, ?order and ?head for more information.

HTH,

Marc Schwartz

__
R-help@r-project.org mailing list
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 matrix

2008-03-20 Thread Felix Zajitschek - UNSW
Hi all,
 
I have a dataset consisting of 5 columns and over 5000 rows. Each row
gives information about an individual animal, including longevity, i.e.
at what age an animal died.
For the model I use I need to create n rows for each animal, n being its
longevity, and a new column 'survival' with a binary 0/1 outcome. When
an animal died e.g. at age 5, there have to be 5 rows of identical data,
except 4 with 0 (=alive) for 'survival', and 1 row with '1' for
'survival'.
 
I thought of creating matrices for each individual, adding first one
column 'survival' containing zeros to the original dataset, then
creating matrices with data = 'the vector containing all elements of an
individual/row' ([1,], nrow = [a,b], exctracting the element for
longevity, and then with byrow = TRUE letting the data be filled in by
row. At the end I would have to set the last element in 'survival' to
'1', and then combine all matrices into one single one.
 
So far I've used Excel to create these datesets manually, but with more
than 1000 individuals this gets really tedious. I haven't used R before
for this sort of a bit more advanced data manipulation, and I would
really appreciate any input/primer about how people would go about doing
this.
 
Thanks,
Felix
 
 
__
::Felix Zajitschek 
Evolution  Ecology Research Centre
School of Biological, Earth and Environmental Sciences 
University of New South Wales - Sydney NSW 2052 - Australia 
Tel   +61 (0)2 9385 8068
Fax  +61 (0)2 9385 1558 
eMail   mailto:[EMAIL PROTECTED]
[EMAIL PROTECTED]
 
http://www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.htm
l www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.html
 

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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 matrix

2008-03-20 Thread jim holtman
Since you did not provide a sample of your data, here is an example of
how to take a vector and create a matrix with 5 entries for each
value, with the extra ones having a zero in the second column:

 x - sample(1:7, 20, T)
 table(x)
x
1 2 3 4 5 6 7
2 4 3 2 4 4 1
 # create a matrix with 5 rows of each value in the vector 'x' with the extra 
 rows
 # having 0 in the second column
 x.l - lapply(split(x, x), function(.val){
+ # pad with at least 5 extra rows to make sure matrix is filled out
+ z - cbind(c(.val, rep(.val[1],5)), c(rep(1, length(.val)), rep(0,5)))
+ z[1:5,]   # only return the first 5
+ })
 # output the new matrix
 do.call(rbind, x.l)
  [,1] [,2]
 [1,]11
 [2,]11
 [3,]10
 [4,]10
 [5,]10
 [6,]21
 [7,]21
 [8,]21
 [9,]21
[10,]20
[11,]31
[12,]31
[13,]31
[14,]30
[15,]30
[16,]41
[17,]41
[18,]40
[19,]40
[20,]40
[21,]51
[22,]51
[23,]51
[24,]51
[25,]50
[26,]61
[27,]61
[28,]61
[29,]61
[30,]60
[31,]71
[32,]70
[33,]70
[34,]70
[35,]70



On Thu, Mar 20, 2008 at 1:51 AM, Felix Zajitschek - UNSW
[EMAIL PROTECTED] wrote:
 Hi all,

 I have a dataset consisting of 5 columns and over 5000 rows. Each row
 gives information about an individual animal, including longevity, i.e.
 at what age an animal died.
 For the model I use I need to create n rows for each animal, n being its
 longevity, and a new column 'survival' with a binary 0/1 outcome. When
 an animal died e.g. at age 5, there have to be 5 rows of identical data,
 except 4 with 0 (=alive) for 'survival', and 1 row with '1' for
 'survival'.

 I thought of creating matrices for each individual, adding first one
 column 'survival' containing zeros to the original dataset, then
 creating matrices with data = 'the vector containing all elements of an
 individual/row' ([1,], nrow = [a,b], exctracting the element for
 longevity, and then with byrow = TRUE letting the data be filled in by
 row. At the end I would have to set the last element in 'survival' to
 '1', and then combine all matrices into one single one.

 So far I've used Excel to create these datesets manually, but with more
 than 1000 individuals this gets really tedious. I haven't used R before
 for this sort of a bit more advanced data manipulation, and I would
 really appreciate any input/primer about how people would go about doing
 this.

 Thanks,
 Felix


 __
 ::Felix Zajitschek
 Evolution  Ecology Research Centre
 School of Biological, Earth and Environmental Sciences
 University of New South Wales - Sydney NSW 2052 - Australia
 Tel   +61 (0)2 9385 8068
 Fax  +61 (0)2 9385 1558
 eMail   mailto:[EMAIL PROTECTED]
 [EMAIL PROTECTED]

 http://www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.htm
 l www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.html


[[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 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.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

__
R-help@r-project.org mailing list
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 matrix

2008-03-20 Thread Simon Blomberg
There may be a less baroque way of doing it, but does this do what you want?

Say you have a data.frame called dat:

 dat
  x1 x2 Longevity
1 -1.9582519  a 4
2  0.8724081  b 2
3 -0.9150847  c 5

# now create a new long data.frame:

 dat.long - as.data.frame(mapply(function (x) rep(x, dat$Longevity), 
 dat[,1:2]))

# Add in the survival column:

 dat.long$Survival - unlist(sapply(dat$Longevity, function (x) c(rep(0, 
 x-1),1)))
 dat.long
  x1 x2 Survival
1  -1.95825191986208  a0
2  -1.95825191986208  a0
3  -1.95825191986208  a0
4  -1.95825191986208  a1
5  0.872408144284977  b0
6  0.872408144284977  b1
7  -0.91508470125413  c0
8  -0.91508470125413  c0
9  -0.91508470125413  c0
10 -0.91508470125413  c0
11 -0.91508470125413  c1

HTH,

Simon.
 
Simon Blomberg, BSc (Hons), PhD, MAppStat. 
Lecturer and Consultant Statistician 
Faculty of Biological and Chemical Sciences 
The University of Queensland 
St. Lucia Queensland 4072 
Australia 
T: +61 7 3365 2506 
email: S.Blomberg1_at_uq.edu.au

Policies:
1.  I will NOT analyse your data for you.
2.  Your deadline is your problem.

The combination of some data and an aching desire for 
an answer does not ensure that a reasonable answer can 
be extracted from a given body of data. - John Tukey.



-Original Message-
From: [EMAIL PROTECTED] on behalf of Felix Zajitschek - UNSW
Sent: Thu 20/03/2008 4:51 PM
To: r-help@r-project.org
Subject: [R] create matrix
 
Hi all,
 
I have a dataset consisting of 5 columns and over 5000 rows. Each row
gives information about an individual animal, including longevity, i.e.
at what age an animal died.
For the model I use I need to create n rows for each animal, n being its
longevity, and a new column 'survival' with a binary 0/1 outcome. When
an animal died e.g. at age 5, there have to be 5 rows of identical data,
except 4 with 0 (=alive) for 'survival', and 1 row with '1' for
'survival'.
 
I thought of creating matrices for each individual, adding first one
column 'survival' containing zeros to the original dataset, then
creating matrices with data = 'the vector containing all elements of an
individual/row' ([1,], nrow = [a,b], exctracting the element for
longevity, and then with byrow = TRUE letting the data be filled in by
row. At the end I would have to set the last element in 'survival' to
'1', and then combine all matrices into one single one.
 
So far I've used Excel to create these datesets manually, but with more
than 1000 individuals this gets really tedious. I haven't used R before
for this sort of a bit more advanced data manipulation, and I would
really appreciate any input/primer about how people would go about doing
this.
 
Thanks,
Felix
 
 
__
::Felix Zajitschek 
Evolution  Ecology Research Centre
School of Biological, Earth and Environmental Sciences 
University of New South Wales - Sydney NSW 2052 - Australia 
Tel   +61 (0)2 9385 8068
Fax  +61 (0)2 9385 1558 
eMail   mailto:[EMAIL PROTECTED]
[EMAIL PROTECTED]

http://www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.htm
l www.bees.unsw.edu.au/school/researchstudents/zajitschekfelix.html


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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
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.