[R] searching for elements

Moshe Olshansky m_olshansky at yahoo.com
Thu Jul 16 01:48:45 CEST 2009


?outer

--- On Thu, 16/7/09, Chyden Finance <finance at chyden.net> wrote:

> From: Chyden Finance <finance at chyden.net>
> Subject: [R] searching for elements
> To: r-help at r-project.org
> Received: Thursday, 16 July, 2009, 3:00 AM
> Hello!
> 
> For the past three years, I have been using R extensively
> in my PhD program in Finance for statistical work. 
> Normally, I can figure this kind of thing out on my own, but
> I am completely stumped as to why the following code does
> not work.
> 
> I have two variables: sigs and p0_recent.
> 
> dim(sigs) = 296 3
> dim(p0_recent) = 504 7
> 
> I want to compare each element in the first column of sigs
> with the elements in the first column of p0_recent.
> 
> In other words, does sigs[i,1] == p0_recent[j,1], where i =
> 1:dim(sigs)[1] and j = 1:dim(p0_recent)[1].
> 
> I've been trying:
> 
> > for (j in 1:dim(p0_recent)[1]) {
> + for (i in 1:dim(sigs)[1]) {
> + if (sigs[i,1] == p0_recent[j,1]) {
> + print(sigs[i,1])
> + }}}
> 
> But, I get:
> 
> Error in Ops.factor(sigs[i, 1], p0_recent[j, 1]) :
>   level sets of factors are different
> 
> Is there a better way than for loops to compare each
> element in one column to each element in another column of
> different length?  If not, can anyone suggest a
> solution?
> 
> CWE
> 
> ______________________________________________
> 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.
>




More information about the R-help mailing list