0

For one of my uni reports I have to analyse some data in and I have gotten to the descriptive part but when I enter the following code I get a notice saying:

Error:attempt to apply non-function

I have no idea how to fix it. Any help would be greatly appreciated :)

age_desc <- data %>%
  mutate(AGE_YEARS = all(duplicated(AGE_YEARS)[-1L])(AGE_YEARS)) %>%
  summarise(mean = mean(AGE_YEARS, na.rm = T),
            sd = sd(AGE_YEARS),
            min = min(AGE_YEARS),
            max = max(AGE_YEARS)) %>%
  modify(round, 2) 
massisenergy
  • 1,764
  • 3
  • 14
  • 25
Tabs302
  • 23
  • 1
  • 4

1 Answers1

0

can you describe in words what you are trying to accomplish with the all(duplicated(AGE_YEARS)[-1L])(AGE_YEARS) portion of your code?

Your error is coming from that portion of your code, where it appears you are trying to multiply an object of logical class (the result of all(duplicated(AGE_YEARS)[-1L])) to a (presumably) numeric vector of AGE_YEARS.

I'm not sure if that is what you are trying to accomplish, but if you did for whatever reason need to multiply a single logical value to a numeric vector, you would need to include a multiplication sign (*) between the elements.

e.g.

all(duplicated(iris$Sepal.Length)[-1L])
# [1] FALSE
all(duplicated(iris$Sepal.Length)[-1L])(iris$Sepal.Length)
# Error: attempt to apply non-function
all(duplicated(iris$Sepal.Length)[-1L])*(iris$Sepal.Length)
#  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [40] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [79] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [118] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Good luck!

PS in the future it is helpful to include a reproducible example (How to make a great R reproducible example) of code in your questions!

  • Hi! I used the code "all(duplicated(AGE_YEARS)[-1L])(AGE_YEARS)" because originally it was suggested to us that we use the code "mutate(AGE_YEARS = fct_explicit_na(AGE_YEARS))" however when I entered that it came up with the error "Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : Calling var(x) on a factor x is defunct. Use something like 'all(duplicated(x)[-1L])' to test for a constant vector." I made "AGE_YEARS" numeric in previous code but for some reason I don't think it has been carried on – Tabs302 Mar 14 '20 at 14:18
  • Have you looked at the documentation for `forcats::fct_explicit_na()`? That function should just be used on factors. It will coerce non-factor variables into factors, so if you want `AGE_YEARS` to be numeric that is not the way to go. For all the functions you are using, especially if you aren't familiar with them, I would recommend looking at the documentation and then using them outside the `mutate()` to see what they do, and make sure the result is what you were going for! – Margaret Janiczek Mar 14 '20 at 18:36