1
votes

So I am using this code below. maindata is a table of players, their home towns, and their points per game. I am trying to make a new table that has the cities in one column, and the average points per game in the second (as shown in table A). I keep getting a table with the cities listed and the rows for AvgPoints all showing NA. I get this error "argument is not numeric or logical: returning NAargument is not numeric or logical", and it repeats itself hundreds of times. Main data has 3561 observations.

point <- maindata%>%
  group_by(City, State) %>%
    summarise(AvgPoints = mean(PTS.1, na.rm = TRUE))

Table A

   City            AvgPoints
----------------------------
New York City        19.9
Los Angeles          22.1
Boston               13.3
1
Please check the str(maindata) for the type of columns. or specifically class(maindata$PTS.1) May be the 'PTS.1' is not numericakrun
@akrun it says they are factors. I am not exactly sure why.NickA
If there are some elements which are non-numeric i.e. it will read as character. In the new version of R, it should be character class instead of factor because stringsAsFactors = FALSE by default. My point is that if your column have c(1.5, 1.2, 'a1'), it is not a numeric column because one of the element is not numeric. you can convert to numeric from factor with maindata %>% mutate(PTS.1 = as.numeric(as.character(PTS.1))) and then do the group by. There would be some friendly warning while converting the non-numeric to NAakrun
@akrun Wonderful, I understand your point and I have applied the function and have got it to work. Thank you!NickA

1 Answers

1
votes

We convert the column to numeric first if it is not numeric and then do the group by operation

library(dplyr)
maindata %>%
      mutate(PTS.1 = as.numeric(as.character(PTS.1))) %>%
      group_by(City, State) %>%
      summarise(AvgPoints = mean(PTS.1, na.rm = TRUE), .groups = 'drop')