tags:

views:

338

answers:

3

Hi Guys, 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

A: 

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
i have just edited the question above. the syntax used wasdf <-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
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
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
that could not work too!
Bazon
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
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
HA, it's ok. 2 rep points won't kill me :)
whybird
A: 

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
Correct, except != instead of ==.
neilfws
sameer,thanks to you too! thats right - "!=" instead of "=="
Bazon
Ah, thanks for pointing out the typo
Sameer
+3  A: 

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), ]
Brian
This is the correct answer.
neilfws
Brian, thanks alot! that did the trick. i understand it better with the explanation you provide!
Bazon
Marek
thanks marek! its good to know various ways of approaching a problem. it sure builds confidence!
Bazon