Reputation: 619
I am trying to create a loop to calculate relative elemental levels for each fish in this dataset (relative elemental levels will be calculated by x/mean(x) in order to divide each data point by the mean of the entire column. I have all the fish in one dataframe but they can be filtered out by ID (see head of data frame) 1st fish is Ring10_1_2. Here is my current code and the current error I am getting.
raw_data <- read_csv("Hatch5Reg.csv")
fishIDs = unique(raw_data$ID)
Hatch5_Rel_El = list()
for(ID in fishIDs) {
Hatch5_Rel_El[[ID]] = lapply(raw_data %>% filter(ID == ID_) %>% mutate_if(is.numeric),funs(mod = x / mean(x)))
}
#Error in match.fun(FUN) : 'funs(mod = x/mean(x))' is not a function, character or symbol
Upvotes: 0
Views: 73
Reputation: 6132
Since no sample data is provided, please see How to make a great R reproducible example, the following would work on iris data:
iris %>%
mutate(across(where(is.numeric), ~.x /mean(.x)))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 0.8727895 1.1447885 0.3725386 0.16675931 setosa
2 0.8385625 0.9812473 0.3725386 0.16675931 setosa
3 0.8043354 1.0466638 0.3459287 0.16675931 setosa
4 0.7872219 1.0139555 0.3991485 0.16675931 setosa
5 0.8556760 1.1774967 0.3725386 0.16675931 setosa
6 0.9241301 1.2756215 0.4523683 0.33351862 setosa
BTW, mutate_if
has been superseded by using across
in the more recent dplyr
versions.
Upvotes: 1