[R] [FORGED] turning data in list format into a matrix format

Rolf Turner r.turner at auckland.ac.nz
Sat Jul 2 02:28:01 CEST 2016


Your question is filled with uh, infelicities.  See inline below.

On 02/07/16 07:53, Marietta Suarez wrote:
> As of now, I simulate 6 variables and manipulate 2 of them. The way my
> syntax is written my final database

    Do you mean "data set"?

is in list mode. I need all of it to be
> in just 1 database

    Do you mean data frame?

any help would be MUCH appreciated. Here's my syntax:
>
> fun=function(n, k,rep){
>   #prepare to store data
>   data=matrix(0,nrow=10*k, ncol=6)
>   db=vector("list",length=rep) #here's the problem
>
>   for (j in 1:rep){
>     for (i in 1:k){
>       #generate data under normal, skewed, and logistic distributions here
>       data[,1]<-seq(1,nrow(data),1)
>       data[,2]<-rep(i,nrow(data))
>       data[,3]<-rep(n,nrow(data))
>       data[,4]<-rnorm(n, 100, 15)
>       data[,5]<-rsnorm(n, 100, 15, 1)

    There is no such function as "rsnorm" in base R or in the standard
    packages.  Where does this function come from?  Do not expect
    your respondents to be psychic.

>       data[,6]<-rlogis(n, 100, 15)
>     }
>     db[[j]]<-data
>   }
>   DataReturn<<-db

    Do *NOT* use "<<-"!  Especially if you actually mean "<-"!!!

    What is the point of "DataReturn" anyway?  It's the same thing
    as "db".  Why confuse things by introducing a new name?

>   return(DataReturn)
>   #DataReturn <- data.frame(matrix(unlist(DataReturn), nrow=rep,
> byrow=T)) #here's
> where I tried to solve it unsuccessfully
> }

In what sense is this unsuccessful?  It returns a data frame.  What is 
wrong with this data frame?  What did you *want* it to look like?

A *reproducible* "toy" example showing what you want to get would be 
helpful.

A wild guess is that you want your reps "stacked" into a single data 
frame.  In which case something like

    do.call(rbind,db)

may be what you want.

Finally, do you really want the result to be a data frame?  Since all 
values dealt with are numeric, you might as well use a matrix, and save 
on the overheads that data frames involve.

cheers,

Rolf Turner

-- 
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list