[R] array manipulation
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
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.
Re: [R] array manipulation
You seem to be looking for 'aperm'. There is a chapter in 'S Poetry' (available on http://www.burns-stat.com) that talks about working with higher dimensional arrays. I don't think any changes need to be made for R. On 02/11/2011 16:16, Simone Salvadei wrote: 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 -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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
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.
Re: [R] array manipulation
On Nov 2, 2011, at 12:16 PM, Simone Salvadei wrote: 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]) ?aperm 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* * * David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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
Hello listeRs, I'm trying to make a square radius around a given reference point. So given the following array, how can I manipulate it so that x0 - array(1,dim=c(5,5)) x0 1 1 1 1 1 1 1 1 1 1 1 1 *1* 1 1 1 1 1 1 1 1 1 1 1 1 becomes into 3 3 3 3 3 3 2 2 2 3 3 2 *1* 2 3 3 2 2 2 3 3 3 3 3 3 Thanks. Muhammad __ 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.
Re: [R] array manipulation
1+pmax(abs(row(z)-3),abs(col(z)-3)) ?row ?col ?pmax for details. Bert Gunter Genentech Nonclinical Statistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Muhammad Rahiz Sent: Wednesday, April 14, 2010 9:44 AM To: r-help@r-project.org Subject: [R] array manipulation Hello listeRs, I'm trying to make a square radius around a given reference point. So given the following array, how can I manipulate it so that x0 - array(1,dim=c(5,5)) x0 1 1 1 1 1 1 1 1 1 1 1 1 *1* 1 1 1 1 1 1 1 1 1 1 1 1 becomes into 3 3 3 3 3 3 2 2 2 3 3 2 *1* 2 3 3 2 2 2 3 3 3 3 3 3 Thanks. Muhammad __ 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-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 simple questions
Hi there, I am pretty new to R. Actually I started using it yesterday. I have two questions: 1. Suppose I have a-c(1:10) (or a-array(c(1:10),dim=c(1,10))) and I want to end up with vector b=[0 0 0 0 0 1 1 1 1 1]. i.e. I want to substitute alla elements that are 5 with 0 and 5 with 1. I did this by using a for loop. Is there another function to use so as to avoid the for? 2. Suppose I have a-c(1,1,2,2,3,3) (or array again) And I want to place a 4,5 before every 2 and end up with a new bigger vector b: b=[1 1 4 5 2 4 5 2 3 3] Also I want to find where the 2's in array a (if it was an array) are located i.e. positions 3,4. Thanx very much in advance!! P.S: Will you inform me via mail or should I visit any blog and post there my questions? ___ ×ñçóéìïðïéåßôå Yahoo!; ÂáñåèÞêáôå ôá åíï÷ëçôéêÜ ìçíýìáôá (spam); Ôï Yahoo! Mail äéáèÝôåé ôçí êáëýôåñç äõíáôÞ ðñïóôáóßá êáôÜ ôùí åíï÷ëçôéêþí ìçíõìÜôùí http://login.yahoo.com/config/mail?.intl=gr [[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.
Re: [R] array manipulation simple questions
Λεωνίδας Μπαντής wrote: 1. Suppose I have a-c(1:10) (or a-array(c(1:10),dim=c(1,10))) and I want to end up with vector b=[0 0 0 0 0 1 1 1 1 1]. i.e. I want to substitute alla elements that are 5 with 0 and 5 with 1. I think you mean =5, not 5. In that case, try this: b - ifelse( a 5, 0, 1) Λεωνίδας Μπαντής wrote: 2. Suppose I have a-c(1,1,2,2,3,3) (or array again) And I want to place a 4,5 before every 2 and end up with a new bigger vector b: b=[1 1 4 5 2 4 5 2 3 3] Also I want to find where the 2's in array a (if it was an array) are located i.e. positions 3,4. Not sure how to insert the 4,5, but this will find the where the 2's are: which(a == 2) -- View this message in context: http://www.nabble.com/array-manipulation-simple-questions-tp22173710p22175864.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] array manipulation simple questions
2009/2/23 Λεωνίδας Μπαντής bleonida...@yahoo.gr: Hi there, I am pretty new to R. Actually I started using it yesterday. I have two questions: 1. Suppose I have a-c(1:10) (or a-array(c(1:10),dim=c(1,10))) and I want to end up with vector b=[0 0 0 0 0 1 1 1 1 1]. i.e. I want to substitute alla elements that are 5 with 0 and 5 with 1. I did this by using a for loop. Is there another function to use so as to avoid the for? (a5)+0 # or out - as.numeric(a 5) if (is.matrix(a)) out - matrix(a, 1) out 2. Suppose I have a-c(1,1,2,2,3,3) (or array again) And I want to place a 4,5 before every 2 and end up with a new bigger vector b: b=[1 1 4 5 2 4 5 2 3 3] # return c(4, 5, 2) or x f - function(x) if (x==2) c(4:5, x) else x # apply f to each element and # if a is a matrix make output a matrix too out - unlist(sapply(a, f)) if (is.matrix(a)) out - matrix(out, nrow(a)) out Also I want to find where the 2's in array a (if it was an array) are located i.e. positions 3,4. which(a == 2) __ 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.
Re: [R] array manipulation simple questions
On Feb 24, 3:57 pm, jdeisenberg catc...@catcode.com wrote: Λεωνίδας Μπαντής wrote: 1. Suppose I have a-c(1:10) (or a-array(c(1:10),dim=c(1,10))) and I want to end up with vector b=[0 0 0 0 0 1 1 1 1 1]. i.e. I want to substitute alla elements that are 5 with 0 and 5 with 1. I think you mean =5, not 5. In that case, try this: b - ifelse( a 5, 0, 1) you could also use b - 1*(x = 5) #the logical elements get converted to numeric Λεωνίδας Μπαντής wrote: 2. Suppose I have a-c(1,1,2,2,3,3) (or array again) And I want to place a 4,5 before every 2 and end up with a new bigger vector b: b=[1 1 4 5 2 4 5 2 3 3] Also I want to find where the 2's in array a (if it was an array) are located i.e. positions 3,4. Not sure how to insert the 4,5, but this will find the where the 2's are: which(a == 2) you could use b - rep(a,times = 2*(a==2) + 1) replace(b, which(b==2), c(4,5,2)) -- View this message in context:http://www.nabble.com/array-manipulation-simple-questions-tp22173710p... Sent from the R help mailing list archive at Nabble.com. __ r-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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.