Hi all, Hope someone can help me. I start out with a matrix called haplo with 600 rows and two columns. To start with, the elements of the matrix are character strings of 20 numbers. I then want to create separate objects, called ha1-ha600, that are the concatenated 20 + 20 numbers, as numeric. I am having no trouble doing anything except getting the index i to paste to the object ha(i) in some fashion. When I do substring(haplo[(i)...) it works fine, but I can't seem to create objects that are ha1 or ha2 etc., containing the 40 number row I just created in the loop. I've tried paste but I can't seem to get it to do the right thing. For this example, nc=20 for (i in 1:600) { tst<-substring(haplo[(i),1],1:nc,1:nc) a1<-as.numeric(tst) tst<-substring(haplo[(i),2],1:nc,1:nc) a2<-as.numeric(tst) ha<-append(a1,a2) ha(i)<-ha # DOESN'T WORK } TIA, Kristin Nicodemus [[alternative HTML version deleted]]
as.numeric(apply(haplo, 1 ,function(x) paste(x,collapse=""))) perhaps? Kristin Kay Nicodemus wrote:> Hi all, > > Hope someone can help me. I start out with a matrix called haplo with 600 rows and two columns. To start with, the elements of the matrix are character strings of 20 numbers. I then want to create separate objects, called ha1-ha600, that are the concatenated 20 + 20 numbers, as numeric. I am having no trouble doing anything except getting the index i to paste to the object ha(i) in some fashion. When I do substring(haplo[(i)...) it works fine, but I can't seem to create objects that are ha1 or ha2 etc., containing the 40 number row I just created in the loop. I've tried paste but I can't seem to get it to do the right thing. > > For this example, nc=20 > > for (i in 1:600) { > tst<-substring(haplo[(i),1],1:nc,1:nc) > a1<-as.numeric(tst) > tst<-substring(haplo[(i),2],1:nc,1:nc) > a2<-as.numeric(tst) > ha<-append(a1,a2) > ha(i)<-ha # DOESN'T WORK > } > > TIA, > Kristin Nicodemus > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Tue, 19 Oct 2004, Kristin Kay Nicodemus wrote:> Hi all, > > Hope someone can help me. I start out with a matrix called haplo with > 600 rows and two columns. To start with, the elements of the matrix are > character strings of 20 numbers. I then want to create separate > objects, called ha1-ha600, that are the concatenated 20 + 20 numbers, as > numeric. I am having no trouble doing anything except getting the index > i to paste to the object ha(i) in some fashion. When I do > substring(haplo[(i)...) it works fine, but I can't seem to create > objects that are ha1 or ha2 etc., containing the 40 number row I just > created in the loop. I've tried paste but I can't seem to get it to do > the right thing.If you really want to create 600 objects, then you want assign(paste("ha",i,sep=""), ha) and to retrieve them get(paste("ha",i,sep="")) Often it is better to keep things like this in a list. -thomas ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html [[alternative HTML version deleted]]