[R] hi, i have a problem in R

Jim Lemon jim at bitwrit.com.au
Thu Aug 6 11:04:24 CEST 2009


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




More information about the R-help mailing list