mercredi 18 novembre 2020

Checking if all factors within a factor are unique, then if so, returning that factor. If not, returning a third value. R

First time posting here! Been struggling with this for about two days but I have a dataframe that looks like this:

code.1 <- factor(c(rep("x",3), rep("y",2), rep("z",3)))
type.1 <- factor(c(rep("small", 2), rep("medium", 2), rep("large", 4)))
df <- cbind.data.frame(type.1, code.1)
df

And am trying to get it to return this:

code.2 <- factor(c("x", "y", "z"))
type.2 <- factor(c("multiple", "multiple", "large"))
df2 <- cbind.data.frame(type.2, code.2)
df2

I've tried all manner of If/Else and apply functions grouping by "code" to return these results but am stuck. Any help appreciated!

Aucun commentaire:

Enregistrer un commentaire