dplyr filter and summarize

Hi. Please help me understand why I am getting error message:
In mean.default(amount) :
argument is not numeric or logical: returning NA

library(dplyr)
amount <- c(1,2,3,4,5)
part <- c("arm","eye", "brain", "brain", "eye")
df <- data.frame(cbind(part, amount))

serious <- c("brain","eye","heart")

df %>%
dplyr::group_by(part) %>%
dplyr::filter(part %in% serious) %>%
dplyr::summarize(avg_amount= mean(amount)) %>%
arrange(desc(avg_amount))

cbind returns a character matrix, since part is a character vector, so the amount column in df is a character column. Try this

library(dplyr)
amount <- c(1,2,3,4,5)
part <- c("arm","eye", "brain", "brain", "eye")
df <- data.frame(part, amount)

serious <- c("brain","eye","heart")

df %>%
  dplyr::group_by(part) %>%
  dplyr::filter(part %in% serious) %>%
  dplyr::summarize(avg_amount= mean(amount)) %>%
  arrange(desc(avg_amount))

FJCC, thank you.

cbind returns a character matrix, since part is a character vector, so the amount column in df is a character column.

Why is this so? Doesn't declaring df <- data.frame(cbind(... tell R I want a data frame, not a matrix, and so a mixture of variable types is ok?

cbind() has already changed amount to characters before data.frame sees it. When I pass the part and amount vectors separately to data.frame(), their data type is preserved.

cbind documentation covers its return value.

For the default method, a matrix combining the arguments column-wise or row-wise. (Exception: if there are no inputs or all the inputs are NULL , the value is NULL .)

The type of a matrix result determined from the highest type of any of the inputs in the hierarchy raw < logical < integer < double < complex < character < list .

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.