[R] searching for elements

2009-07-15 Thread Chyden Finance

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@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] searching for elements

2009-07-15 Thread Steve Lianoglou

Hi,

On Jul 15, 2009, at 1:00 PM, Chyden Finance wrote:


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


It seems that this particular problem is due to the fact that you are  
comparing two sets of factors with different levels, which is what the  
Ops.factor error is saying. But let's make it more clear:


R f1 - factor(c('a','b','c'))
R f1
[1] a b c
 f2 - factor(c('c','d','e'))
[1] c d e
Levels: c d e
 f1[3] == f2[1]
Error in Ops.factor(f1[3], f2[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?



Probably better ways, but how about starting by first nuking the inner  
loop? Something like this should work:


for (j in 1:nrow(p0_recent)) {
  found - match(p0_recent[j,1], sigs[,1])
  ...
}

In each iteration found will be NA if the p0_recent element does not  
appear in sigs[,1], otherwise it will be a vector of indices into  
sigs[,1] that equal the p0_recent element you're querying with.


(Assuming you fix your factor/level issue)

HTH,
-steve

--
Steve Lianoglou
Graduate Student: Physiology, Biophysics and Systems Biology
Weill Medical College of Cornell University

Contact Info: http://cbio.mskcc.org/~lianos/contact

__
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] searching for elements

2009-07-15 Thread Steve Lianoglou

One more thing,


Error in Ops.factor(sigs[i, 1], p0_recent[j, 1]) :
level sets of factors are different


It seems that this particular problem is due to the fact that you  
are comparing two sets of factors with different levels, which is  
what the Ops.factor error is saying. But let's make it more clear:


R f1 - factor(c('a','b','c'))
R f1
[1] a b c
 f2 - factor(c('c','d','e'))
[1] c d e
Levels: c d e
 f1[3] == f2[1]
Error in Ops.factor(f1[3], f2[1]) : level sets of factors are  
different



One way to fix would be to ensure all factors have the same levels  
from the get go, like:


It seems that your first problem you're facing looks like you are  
comparing two sets of factors with different levels, which is what the  
Ops.factor error is saying. But let's make it more clear:


R f1 - factor(c('a','b','c'), levels=c('a','b','c','d','e'))
R f2 - factor(c('c','d','e'), levels=c('a','b','c','d','e'))

R f1
[1] a b c
Levels: a b c d e
R f2
[1] c d e
Levels: a b c d e

R f1[3] == f2[1]
[1] TRUE

--
Steve Lianoglou
Graduate Student: Physiology, Biophysics and Systems Biology
Weill Medical College of Cornell University

Contact Info: http://cbio.mskcc.org/~lianos/contact

__
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] searching for elements

2009-07-15 Thread Moshe Olshansky

?outer

--- On Thu, 16/7/09, Chyden Finance fina...@chyden.net wrote:

 From: Chyden Finance fina...@chyden.net
 Subject: [R] searching for elements
 To: r-help@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@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.