Quantcast

Create Arrays

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Create Arrays

dpender
Hi,

For this example:

O <- c(0 0 0 2 0 0 2 0)

I want to create an array every time O[i] > 0.  The array should be in the form;

R[j] <- array(-1, dim=c(2,O[i]))

i.e. if O[i] > 0 4 times I want 4 R arrays.

Does anyone have any suggestions?

Thanks,

Doug
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create Arrays

Barry Rowlingson
On Fri, Oct 15, 2010 at 9:55 AM, dpender <[hidden email]> wrote:

>
> Hi,
>
> For this example:
>
> O <- c(0 0 0 2 0 0 2 0)
>
> I want to create an array every time O[i] > 0.  The array should be in the
> form;
>
> R[j] <- array(-1, dim=c(2,O[i]))
>
> i.e. if O[i] > 0 4 times I want 4 R arrays.
>
> Does anyone have any suggestions?
>

 Suggestion number l is don't use O for objects! Far too confusing!

 Serious suggestion is that a concrete example will help. Let me try:

If:

 X = c(0,0,0,2,0,0,3,0) # I'm not using O here!

 Then

R will be a list of length 2 because there are 2 values in X bigger than 0.

R[1] will be array(-1,dim=c(2,2))   # because X[4] is 2
and
R[2] will be array(-1,dim=c(2,3))  # because X[7] is 3

 Yup?

 Okay, first get rid of the zeroes:

Xnz = X[X!=0]

 That simplifies the problem. Then use lapply to iterate over Xnz with
a function that returns the array given the value:

> lapply(Xnz,function(x){array(-1,dim=c(2,x))})
[[1]]
     [,1] [,2]
[1,]   -1   -1
[2,]   -1   -1

[[2]]
     [,1] [,2] [,3]
[1,]   -1   -1   -1
[2,]   -1   -1   -1

 2-d arrays are just matrices, so you can do it all in one line with:

lapply(X[X!=0],function(x){matrix(-1,2,x)})

Barry

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create Arrays

Gerrit Eichner
In reply to this post by dpender
Hi, Doug,

maybe

columns <- c( 0, 3, 0, 2, 0, 1)
lapply( columns[ columns > 0],
         function( o) array( -1, dim = c( 2, o)))

does what you want?

Regards -- Gerrit

---------------------------------------------------------------------
AOR Dr. Gerrit Eichner               Mathematical Institute, Room 212
[hidden email]   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109              http://www.uni-giessen.de/~gcb7
---------------------------------------------------------------------


Zitat von dpender <[hidden email]>:

>
> Hi,
>
> For this example:
>
> O <- c(0 0 0 2 0 0 2 0)
>
> I want to create an array every time O[i] > 0.  The array should be in the
> form;
>
> R[j] <- array(-1, dim=c(2,O[i]))
>
> i.e. if O[i] > 0 4 times I want 4 R arrays.
>
> Does anyone have any suggestions?
>
> Thanks,
>
> Doug
> --
> View this message in context:  
> http://r.789695.n4.nabble.com/Create-Arrays-tp2996706p2996706.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create Arrays

dpender
Barry, Gerrit,

That was what I am after but unfortunately only the starting point.  I am now trying to amend a function that inserts the R matrices into a dataset in the correct places:

i.e.

H <- c(0.88, 0.72, 0.89, 0.93, 1.23, 0.86, 0.98, 0.85, 1.23)

T <- c(7.14, 7.14, 7.49, 8.14, 7.14, 7.32, 7.14, 7.14, 7.14)

O <- c(0, 0, 0, 3, 0, 0, 0, 2, 0)


R <- lapply(O[O!=0], function(x){matrix(-1,1,x)})

m <- rbind(H, T)

O2 <- rbind(O,O)

f <- function(x, y, z) {
    if(nrow(x) != nrow(y) || nrow(y) != length(z))
          stop('unequal numbers of rows among inputs')
    out <- matrix(NA,2,length(H)+sum(O))
    for(i in 1:2)
       out[[i,]] <- append(x[i,], as.numeric(z[[i]]), after = which(y[i,] > 0) - 1)
    out
   }

f(m, O2, R)


f is the function that requires amendment and was originally written for a single R value and treated each row of out separately.  

I now want to insert R[i] into m before every point that O[i] > 0

Hope this makes sense.

Doug

I don

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create Arrays

Gerrit Eichner
Hi, Doug,

maybe


HH <- c(0.88, 0.72, 0.89, 0.93, 1.23, 0.86, 0.98, 0.85, 1.23)
TT <- c(7.14, 7.14, 7.49, 8.14, 7.14, 7.32, 7.14, 7.14, 7.14)
columnnumbers <- c(0, 0, 0, 3, 0, 0, 0, 2, 0)

TMP <- lapply( seq( columnnumbers),
                function( i, CN, M) {
                 if( CN[i] == 0) as.matrix( M[, i]) else
                  matrix( -1, nrow( M), CN[i])
                 }, CN = columnnumbers, M = rbind( HH, TT))

do.call( cbind, TMP)



gets close to what you want (after some adaptation, of course).


  HTH  --  Gerrit

---------------------------------------------------------------------
AOR Dr. Gerrit Eichner               Mathematical Institute, Room 212
[hidden email]   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109        http://www.uni-giessen.de/cms/eichner

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create Arrays

dpender
Hi Gerrit,

Almost it but I need to insert M[,i] as well as (matrix( -1, nrow( M), CN[i]) when CN[i] = 0

I know this is not correct but can something like the following be done?

HH <- c(0.88, 0.72, 0.89, 0.93, 1.23, 0.86, 0.98, 0.85, 1.23)
TT <- c(7.14, 7.14, 7.49, 8.14, 7.14, 7.32, 7.14, 7.14, 7.14)
c <- c(0, 0, 0, 2, 0, 0, 0, 2, 0)

TMP <- lapply( seq(c),
                function( i, CN, M) {
                 if( CN[i] == 0) as.matrix( M[, i]) else
                  (matrix( -1, nrow( M), CN[i]) && as.matrix( M[, i]))
                 }, CN = c, M = rbind( HH, TT))

do.call( cbind, TMP)

Doug
Loading...