[R] function sum for array

2011-11-17 Thread Simone Salvadei
I'm looking for a function that allows to sum the elements of an array
along a dimension that can be different from the classical ones (rows or
columns).

Let's suppose for example that:

- A is an array with dimensions 2 x 3 x 4
- I want to compute B, a 2 x 3 matrix with elements equal to the sum of the
corrensponding elements on each of the 3 strata.

I've tried to use   apply(A,3,sum) but the result is a vector, not a matrix.
Another solution is a less elegant

B=matrix(rep(0,6),ncol=3)
for(t in 1:4) B = B + A[ , , t]

May anybody help?
S

-- 
---

Simone Salvadei

Faculty of Economics
Department of Financial and Economic Studies and Quantitative Methods
University of Rome Tor Vergata
e-mail: simone.salva...@uniroma2.it federico.belo...@uniroma2.it
url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
http://www.econometrics.it/
---

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] array manipulation

2011-11-04 Thread Simone Salvadei
This worked example, hoping to be helpful, has been requested after a (my)
further enquiry about array manipulation.

I was looking for a command that is equivalent to repmat() in matlab and
that could also be applied to array.

(for Matlab users)
The Matlal code was the following:


temp_u=zeros(d,c,T);  -creation of an array of dimensions d x
c x T full of zeroes
temp_u(1,:,:)=m_u;-filling the first row of each 'stratum'
with the rows of
the matrix 'm_u'
temp_u=repmat(temp_u(1,:,:),d,1); -filling the remaining rows (full of
zeroes) of 'temp_u' with
copies of the corrensponding 1st row

-- 
---

Simone Salvadei

Faculty of Economics
Department of Financial and Economic Studies and Quantitative Methods
University of Rome Tor Vergata
e-mail: simone.salva...@uniroma2.it federico.belo...@uniroma2.it
url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
http://www.econometrics.it/
---

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] array manipulation

2011-11-04 Thread Simone Salvadei
This worked example, hoping to be helpful, has been requested after a (my)
further enquiry about array manipulation.

I was looking for a command that is equivalent to repmat() in matlab and
that could also be applied to array.

(for Matlab users)
The Matlal code was the following:


1)temp_u=zeros(d,c,T);  -creation of an array of dimensions d
x c x T full of zeroes
2)temp_u(1,:,:)=m_u;-filling the first row of each
'stratum' with the rows of
the matrix 'm_u'
3)temp_u=repmat(temp_u(1,:,:),d,1); -filling the remaining rows (full of
zeroes) of 'temp_u' with
copies of the corrensponding 1st row

(what's happening if you are not a Matlab users)
(numerical example d=2,c=4,T=4)
1)temp_u=zeros(d,c,T)
 temp_u(:,:,1) =

 0 0 0 0
 0 0 0 0


temp_u(:,:,2) =

 0 0 0 0
 0 0 0 0


temp_u(:,:,3) =

 0 0 0 0
 0 0 0 0


temp_u(:,:,4) =

 0 0 0 0
 0 0 0 0

2)temp_u(1,:,:)=m_u
temp_u(:,:,1) =

0.96040.01560.02300.0009
 0 0 0 0


temp_u(:,:,2) =

0.39060.29480.09810.2165
 0 0 0 0


temp_u(:,:,3) =

0.53900.24820.11400.0988
 0 0 0 0


temp_u(:,:,4) =

0.45460.26410.07940.2019
 0 0 0 0

3)temp_u=repmat(temp_u(1,:,:),d,1)

temp_u(:,:,1) =

0.96040.01560.02300.0009
0.96040.01560.02300.0009


temp_u(:,:,2) =

0.39060.29480.09810.2165
0.39060.29480.09810.2165


temp_u(:,:,3) =

0.53900.24820.11400.0988
0.53900.24820.11400.0988


temp_u(:,:,4) =

0.45460.26410.07940.2019
0.45460.26410.07940.2019


Now, in order to reply this exercise in R I used the following code:

temp_u=array(0,dim=c(1,c,T))
temp_u[1,,]=m_u
temp_u=kronecker(temp_u,matrix(rep(1,d),nr=d))


A special thank to David Winsemius,William Dunlap and Patrick Burns.
I hope I have been helpful.





---

Simone Salvadei

Faculty of Economics
Department of Financial and Economic Studies and Quantitative Methods
University of Rome Tor Vergata
e-mail: simone.salva...@uniroma2.it federico.belo...@uniroma2.it
url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
http://www.econometrics.it/
---

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] array manipulation

2011-11-02 Thread Simone Salvadei
Hello,
I'm at the very beginning of the learning process of this language.
Sorry in advance for the (possible but plausible) stupidity of my question.

I would like to find a way to permute the DIMENSIONS of an array.
Something that sounds like the function permute() in matlab.

Given an array C of dimensions c x d x T , for instance, the command

permute(C, [2 1 3])

would provide (in Matlab) an array very similar to C, but this time each
one of the T matrices c x d has changed into its transposed.
Any alternatives to the following (and primitive) 'for' cycle?

*# (previously defined) phi=array with dimensions c(c,d,T)*
*
*
*temp=array(0,dim=c(c,d,T))*
* for(i in 1:T)*
* {*
* temp[,,i]=t(phi[,,i])*
* }*
* phi=temp*
*
*

Thank you very much!
S

-- 
---

Simone Salvadei

Faculty of Economics
Department of Financial and Economic Studies and Quantitative Methods
University of Rome Tor Vergata
e-mail: simone.salva...@uniroma2.it federico.belo...@uniroma2.it
url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
http://www.econometrics.it/
---

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.