[R] R combining many vectors of predictable name into one date frame

Rui Barradas ruipbarradas at sapo.pt
Sun Jul 15 14:01:01 CEST 2012


Hello,

Try the following.


ib1 <- 1:10
ib2 <- rnorm(10)

hold.list <- objects(pattern="ib")
df <- sapply(hold.list, get)
df


Note that you don't need list(), and that sapply() returns a data.frame 
if possible.

Also, 'df' is the name of an R function, use something else like 'df1'.

Hope this helps

Rui Barradas

Em 14-07-2012 00:51, Mathew Vickers escreveu:
> G'day R (power) users,
>
> I have a many vectors, called:
>
> ib1
> ib2
> ib3
> ...
> ib100
>
> and I would like them in one data frame (df) such that:
>
>> df
>
> ib1  ib2   ib3  ib4 .....  ib100
> x      x      x    x            x
> x      x      x    x            x
> x      x      x    x            x
>
> I have attempted:
>
> hold.list <- list(objects(pattern="ib"))
> df <- data.frame(hold.list)
>
> but that didn't work
>
> also:
> do.call(rbind, (objects(pattern="ib")))
>
> and that also didn't work. I tried a whole pile of other things, where I
> also failed.
>
>
> The number of vectors might differ each time I want to make the data frame,
> so that in the example above, I have ib1 : ib100, but next time, I might
> only have ib1 : ib2
>
> Below is my (probably somewhat embarrassing) example script for generating
> the vectors in the first place. Commented out toward the end are a few
> attempts at doing the job I wanted to do.
>
>
> temp <- runif(100)
> tripID <- rep(1:10, 10)
> uni <- rep(1:4, 25)
> temp <- data.frame(temp, tripID, uni)
>
> trips <- unique(temp$tripID)
> uni <- unique(temp$uni[temp$tripID==trips[1]])
>
> for (jj in 1:length(uni)){
>    a <- c()
>    for (ii in 1:10){
>      a <- c(a, IQR(temp$temp[temp$uni %in% sample(uni,jj)]))
>      assign(paste("ib",jj,sep=""), a) # ib is short for ibuttons. The number
> is how many were used to calc IQR
>    }
> #   hold.list <- list(objects(patter="ib"))
> #   trip <- data.frame(list=hold.list # I am trying to put everything into
> a dataframe
> #   do.call("rbind", list=hold.list)
> #   do.call("rbind", list(objects(pattern="ib")))
> }
>
>
> thanks heaps if you can help. And sorry if this is mostly garble. This is
> my first crack at soliciting help from the list.
>
> cheers,
>
> mat
>
>



More information about the R-help mailing list