1

I need to create a new data frame that excludes dams that appear in "dam1" and "dam2" columns on the same fosdate (fostering date). I tried df <- df[df$dam1!=df$dam2,] but did not work. Dam1 and dam2 are factors which are the id's of mothers.

my df:

fosdate      dam1     dam2
8/09/2009    2Z523    2Z523
30/10/2009   1W509    5C080
30/10/2009   1W509    5C640
30/10/2009   1W509    1W509
1/10/2009    1W311    63927

The new data frame that I need to get is: dfnew:

fosdate      dam1     dam2
30/10/2009   1W509    5C080
30/10/2009   1W509    5C640
1/10/2009    1W311    63927

Would appreciate any help!

Bazon

Michael Todd
  • 16,679
  • 4
  • 49
  • 69
Bazon
  • 13
  • 4
  • Hey Bazon, what's with the two different accounts withthe same user name asking effectively duplicate questions? http://stackoverflow.com/questions/2863316/selecting-rows-with-unidentical-values-appearing-in-two-different-columns-in-r – whybird May 20 '10 at 01:22
  • You may want to clarify what language/framework this question is for, as it stands, the question is pretty ambiguous. – David May 20 '10 at 01:32

3 Answers3

3

The problem is that dam1 and dam2 are factors each with a different number of levels. To get around this you need to convert the factors to "characters" to do that comparison.

dfnew <-df[as.character(df$dam1) != as.character(df$dam2), ]
Marek
  • 49,472
  • 15
  • 99
  • 121
Brian
  • 448
  • 3
  • 4
  • Brian, thanks alot! that did the trick. i understand it better with the explanation you provide! – Bazon May 21 '10 at 00:00
  • As states in help to `factor` comparisons works for factor & character, so `df[as.character(df$dam1) != df$dam2, ]` works as well. You may use `subset`: `subset(df, as.character(dam1) != dam2)`. – Marek May 21 '10 at 07:51
  • thanks marek! its good to know various ways of approaching a problem. it sure builds confidence! – Bazon May 21 '10 at 23:02
0

My guess is that when you imported the data, df$dam1 and df$dam2 became factors

You can check this with

is.factor(df$dam1)

If this is the TRUE, then try something like

df[as.character(df$dam1) != as.character(df$dam2),]
Sameer
  • 1,807
  • 1
  • 15
  • 16
-1

Wild guess based on the idea that you might be using R (since your other questions are about R). Note that I don't know R, I'm just putting 2 and 2 together from the other questions and answers given.

Try

df <- df[df$dam1 != df$dam2,]

i.e. specifiy df$ explicitly on both sides of the comparison clause.

whybird
  • 1,108
  • 8
  • 19
  • i have just edited the question above. the syntax used was df <-df[df$dam1!=df$dam2,]. the message i got was: Error in Ops.factor(fosdetail2$sow, fosdetail2$recipsow) : level sets of factors are different. i am new to R and trying to claw my way through! – Bazon May 20 '10 at 02:32
  • why the two accounts? i came to know this helpful site last weak and so i joined. i somehow logged out and could not log in again to my account and really wanted to get this question across - so created another account. – Bazon May 20 '10 at 02:38
  • Did you try the subset way of doing it as mentioned in http://stackoverflow.com/questions/2854625/select-only-rows-if-its-value-in-a-particular-column-is-less-than-its-value-in-th/2854856#2854856 ? – whybird May 20 '10 at 02:48
  • Sheesh, don't you hate it when someone votes down your answer which is only no longer relevant because the question was edited in response to your answer? – whybird May 20 '10 at 23:48
  • i am so sorry whybird! it was my mistake that the first syntax appeared in my initial question (before you pointed out the correction). i sincerely apologise for my mistake! – Bazon May 21 '10 at 22:51