Eliminating a data frame in R that has only one row for each category

If I have the following data frame called petsin R:

> pets
     name animal
1     Amy    dog
2     Bob    cat
3   Carol    dog
4   Danny    cat
5 Eustace  horse
6 Frances  horse

I can unstackset the animal categories as column headers in the data frame as follows:

> unstack(pets, pets$name ~ pets$animal)
    cat   dog   horse
1   Bob   Amy Eustace
2 Danny Carol Frances

If, however, the data frame petshas only one instance of each animal:

> pets
     name animal
1     Amy    dog
2     Bob    cat

then executing the same code produces this result:

> unstack(pets, pets$name ~ pets$animal)
    res
cat Bob
dog Amy

I need something that puts these animal categories in the column headers, no matter how many rows fall into each category. Anyone have any ideas please?

+4
source share
3 answers

data.frameand splitdo it:

pets <- data.frame(
 name=c("Amy", "Bob", "Carol", "Danny", "Eustace", "Frances"),
 animal=c("dog", "cat", "dog", "cat", "horse", "horse")
)

data.frame(split(pets$name,pets$animal,drop=TRUE))

#    cat   dog   horse
#1   Bob   Amy Eustace
#2 Danny Carol Frances

pets2 <- pets[1:2,]

data.frame(split(pets2$name,pets2$animal,drop=TRUE))

#  cat dog
#1 Bob Amy
+2
source

:

> t(unstack(pets))
    cat   dog  
res "Bob" "Amy"

, :

unstack <- function(..., drop=FALSE) {
  u <- utils::unstack(...)
  if (!drop && ncol(u) == 1) {
    u <- t(u)
    rownames(u) <- 1
    return(u)
  } else {
    return(u)
  }
}

unstack. drop=TRUE , drop=FALSE .

+2

Another alternative:

> p2 <- pets[!duplicated(pets$animal), ]
> data.frame(matrix(p2$name, nrow = 1, ncol = nrow(p2), 
                    dimnames = list(NULL, p2$animal)))
#   dog cat   horse
# 1 Amy Bob Eustace
0
source

Source: https://habr.com/ru/post/1540611/


All Articles