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
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 differentIt 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
?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. >
Maybe Matching Threads
- CVE-2021-33515: SMTP Submission service STARTTLS injection
- CVE-2021-33515: SMTP Submission service STARTTLS injection
- CVE-2020-24386: IMAP hibernation allows accessing other peoples mail
- CVE-2020-24386: IMAP hibernation allows accessing other peoples mail
- CVE-2019-19722: Critical vulnerability in Dovecot