Calculating average in function

Hi I am trying to calculate average in my function but its giving error, please help how can calculate.
getting error "In mean.default(var) : argument is not numeric or logical: returning NA"
but its numeric value.

data <- data[!is.na(data[[var]]), ]
T1 <- as.data.frame(table(data[[var]]))
all <- sum(T1[, 2])
T1 <- T1 %>% mutate(
average_score <- mean(var),
!!Name_of_variable := as.character(Var1),
"Percent" = round(Freq * 100 / all,digits = 1),
"N" = as.numeric(Freq)
) %>%
select(!!Name_of_variable,"Percent","N")
T1 <- T1 %>% arrange(desc(Percent))

}

This is apparently non-standard evaluation.
Nights of sleep have been lost because of this.
My advice is to be very careful/precise:

  • you are talking about 'function' but I see no function definition
  • you say var is numeric but I see no function call

The best thing to do in a case like this is to make a reprex with the definition and call.

So it looks like data is a list or dataframe, and var is a string or integer? In any case the first line is probably wrong. Maybe you want

data <- data[[var]][!is.na(data[[var]])] # the non NA elements of data[[var]], or NULL if none exist

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