[R] Ask for help: find corresponding elements between matrix
Jose Iparraguirre
Jose.Iparraguirre at ageuk.org.uk
Thu Feb 21 16:21:46 CET 2013
Hi Zhengyu
Surely not the most elegant piece of coding, but this should do the trick:
> m=rep(NA,nrow(seq)*ncol(seq))
> mat1=matrix(m,3)
> for(i in 1:nrow(seq)){
+ for(j in 1:ncol(seq)){
+ ifelse((seq[i,j]==1 | seq[i,j]==-1),
+ mat1[i,j]<-mat[i,j],mat1[i,j]<-NA)
+ }
+ }
> mat1
e.g. With
> mat
[,1] [,2] [,3] [,4]
[1,] 1.8937156 0.9461121 0.9446139 0.1662869
[2,] 1.0417071 1.5253203 -0.1476443 0.2010656
[3,] 0.8777728 2.2808083 -0.6486065 -0.6618795
and
>seq
[,1] [,2] [,3] [,4]
[1,] 1 1 0 1
[2,] -1 1 0 1
[3,] 0 -1 -1 NA
You get
[,1] [,2] [,3] [,4]
[1,] 1.893716 0.9461121 NA 0.1662869
[2,] 1.041707 1.5253203 NA 0.2010656
[3,] NA 2.2808083 -0.6486065 NA
Regards,
José
José Iparraguirre
Chief Economist
Age UK
T 020 303 31482
E Jose.Iparraguirre at ageuk.org.uk
Twitter @jose.iparraguirre at ageuk
Tavis House, 1- 6 Tavistock Square
London, WC1H 9NB
www.ageuk.org.uk | ageukblog.org.uk | @ageukcampaigns
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of JiangZhengyu
Sent: 21 February 2013 14:39
To: r-help at r-project.org
Subject: [R] Ask for help: find corresponding elements between matrix
Dear R experts,
I have two matrix (seq & mat) & I want to retrieve in a new matrix all the numbers from mat that =1 (corresponding to the same row/ column position) in seq, or all the numbers in mat that =-1 in seq. - Replace all the numbers with NA if it's not 1/-1 in seq. There are some "NA"s in seq.
seq=matrix(c(1,-1,0,1,1,-1,0,0,-1,1,1,NA),3,4)
mat=matrix(rnorm(12),3)
I made this code but I don't know where's the problem..
seq=matrix(c(1,-1,0,1,1,-1,0,0,-1,1,1,NA),3,4)
mat=matrix(rnorm(12),3)
m=rep(NA,nrow(seq))
for(i in 1:nrow(seq))
{m[i]=mat[seq[i]==1]
}
Error in m[i] = mat[seq[i] == 1] : replacement has length zero
In addition: Warning message:
In m[i] = mat[seq[i] == 1] :
number of items to replace is not a multiple of replacement length
>
I was wondering if anyone can help me with this.
Thanks in advance,
Zhengyu
eg. here is what I want
> head(seq)
[,1] [,2] [,3] [,4]
[1,] 1 1 0 1
[2,] -1 1 0 1
[3,] 0 -1 -1 NA
> head(mat)
[,1] [,2] [,3] [,4]
[1,] 0.1006950 0.3354360 0.1796888 1.3198761
[2,] 1.0948252 0.5350797 -0.5739454 0.2744478
[3,] -0.5409936 -0.3820084 -0.5601096 0.9239680
This is a new new matrix.
> head(mat1)
[,1] [,2] [,3] [,4]
[1,] 0.1006950 0.3354360 NA 1.3198761
[2,] NA 0.5350797 NA 0.2744478
[3,] NA NA NA NA
[[alternative HTML version deleted]]
______________________________________________
R-help at 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.
Wrap Up and Run 10k is back!
Also, new for 2013 – 2km intergenerational walks at selected venues. So recruit a buddy, dust off the trainers and beat the winter blues by
signing up now:
http://www.ageuk.org.uk/10k
Milton Keynes | Oxford | Sheffield | Crystal Palace | Exeter | Harewood House, Leeds |
Tatton Park, Cheshire | Southampton | Coventry
Age UK Improving later life
http://www.ageuk.org.uk
-------------------------------
Age UK is a registered charity and company limited by guarantee, (registered charity number 1128267, registered company number 6825798).
Registered office: Tavis House, 1-6 Tavistock Square, London WC1H 9NA.
For the purposes of promoting Age UK Insurance, Age UK is an Appointed Representative of Age UK Enterprises Limited, Age UK is an Introducer
Appointed Representative of JLT Benefit Solutions Limited and Simplyhealth Access for the purposes of introducing potential annuity and health
cash plans customers respectively. Age UK Enterprises Limited, JLT Benefit Solutions Limited and Simplyhealth Access are all authorised and
regulated by the Financial Services Authority.
------------------------------
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are
addressed. If you receive a message in error, please advise the sender and delete immediately.
Except where this email is sent in the usual course of our business, any opinions expressed in this email are those of the author and do not
necessarily reflect the opinions of Age UK or its subsidiaries and associated companies. Age UK monitors all e-mail transmissions passing
through its network and may block or modify mails which are deemed to be unsuitable.
Age Concern England (charity number 261794) and Help the Aged (charity number 272786) and their trading and other associated companies merged
on 1st April 2009. Together they have formed the Age UK Group, dedicated to improving the lives of people in later life. The three national
Age Concerns in Scotland, Northern Ireland and Wales have also merged with Help the Aged in these nations to form three registered charities:
Age Scotland, Age NI, Age Cymru.
More information about the R-help
mailing list