Hi, I'm new to R language. There is a problem I couldn't understand. Hope you can answer my question. when i type>for (i in 1:10){+ print(sample(9,4,replace=T)) +} and it shows ten of four numbers and how do I do to calculate the frequencies in each list? I know there is a hint; list10<-vector(mode="list",length=4) But I don't know how to use it. How do I name each list? There are my problems. Thanks!! Best Regards, vie _________________________________________________________________ Show them the way! Add maps and directions to your party invites. [[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On > Behalf Of Gina Liao > Sent: Wednesday, August 05, 2009 7:15 AM > To: r-help at stat.math.ethz.ch > Subject: [R] hi, i have a problem in R > > > Hi, I'm new to R language. > > There is a problem I couldn't understand. > > Hope you can answer my question. > > > > when i type > > > > >for (i in 1:10){ > > + print(sample(9,4,replace=T)) > > +} > > > > and it shows ten of four numbers > > and how do I do to calculate the frequencies in each list? > > > > I know there is a hint; list10<-vector(mode="list",length=4) > > > > But I don't know how to use it. > > How do I name each list? > > > > There are my problems. > > Thanks!! > > > > Best Regards, > > vie >It is not clear to me what you are trying to calculate frequencies for. Are you trying to get 10 sets of 4 random digits, or 4 sets of ten random digits? What frequencies are you trying to calculate? To generate 10 rows of 4 digits I would probably do something like digits <- matrix(sample(9,40,replace=T), nrow=10) Then you could get frequencies of digits by row or by column. But I am not sure what you want. Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
Hi, I'm sorry i didn't say clearly.>for (i in 1:10){+ print(sample(9,4,replace=T)) + } [1] 2 5 5 2 [1] 6 2 1 5 [1] 9 5 9 7 [1] 2 6 4 1 [1] 8 5 4 5 [1] 6 2 3 7 [1] 6 1 7 3 [1] 9 5 4 7 [1] 6 4 8 5 [1] 1 5 6 3 I mean when it shows these reults. Then, what should I do to show the top 3 numbers with highest frequencies for each position. It shows ten rows and four lists. But I'd like to calculate the highest frequencies in each list. For example, in the first list, the highest frequencie is 6. Because I have to do that procedure for 100 times, and it's possible to calculate by self. Thank you!! _________________________________________________________________ [[alternative HTML version deleted]]
Hi vie, I've inserted comments within your message below. Gina Liao wrote:> Hi, I'm new to R language. > > There is a problem I couldn't understand. > > Hope you can answer my question. > > > > when i type > > > > >> for (i in 1:10){ >> > > + print(sample(9,4,replace=T)) > > +} > >The above produces 10 vectors, each containing four numbers between 1 and 9.> > > and it shows ten of four numbers > > and how do I do to calculate the frequencies in each list? > >table(sample(9,4))> > > I know there is a hint; list10<-vector(mode="list",length=4) > > >What you probably want to do is to make each vector an element of a list. If you really want a list containing four vectors with ten integers in each vector, you want: mysamplelist<-vector("list",4) for(i in 1:4) mysamplelist[[i]]<-sample(9,10,TRUE) sapply(mysamplelist,table)> But I don't know how to use it. > > How do I name each list? > >names(mysamplelist)<-c("Allan","Bertrand","Cicero","Dracula") Jim