Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Why r shows an error when using dplyr and ifelse together?

$
0
0

I am trying to use the "ifelse" function in R grouping but it doesn't work. My data is something like this:

Breed Animal
NOR    1  
NOR    1
SWE    1
HOL    2
NOR    2
NOR    3
NOR    3

So I want to create a new variable, called "comp" for composition in which if the breed for the animal is always NOR, it is purebred, if not, it is admixed. The data.frame is called NOR and the code I used is:

NOR %>%`
group_by (animal) %>%%`
mutate(comp= ifelse(NOR$breed == "NOR", "purebred","admixed")`

But then I have this error: Error: Column 'comp' must be length 28 (the group size) or one, not 1104.

The output that I need is:

Breed Animal comp  
NOR     1     admixed
NOR     1     admixed
SWE     1     admixed
HOL     2     admixed
NOR     2     admixed
NOR     3     purebred
NOR     3     pubebred

Thank you in advance! :)


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>