How to calculate the mean value of the column inside the function

Sin

I want to calculate the mean value of the column inside the function. Let's try in this example:

test = data.frame(value = c(231,43,12,342,123,543,56,122,321,222),
              category = c("A","B","A","B","A","A","A","B","B","B"))


  fun <- function(AorB) {
  example <- test %>% 
   filter(category == AorB) %>%
   select(value) 
   ## %>%           (here I want to add mean calculation)
   ## mean()        (it is not possible)
   ## sum()/nrow    (it is not possible)
  }

  solve <- fun("A")

The whole problem with 'mean()' and 'sum()/nrow()' is that the 'mean()' and 'nrow()' need to specify the data.frame. All calculations are inside the function so I can't specify the data.frame like this:

mean(example$value)
Ryan Morton

Per Sin's suggestion in the comments...

I think you need to use:

example <- test %>% filter(category == AorB) %>% summarise( mean = mean(value))

For dplyr, you need to call (and name) those summary functions within the summarise() function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to calculate mean of matrix based on column value

From Dev

How to calculate a mean inside a window where the range of the window depends on the value of a column?

From Dev

How to calculate mean by skipping String Value in Numeric Column?

From Dev

How to calculate mean value of values on column which go comma?

From Dev

Using apply function to calculate the mean of a column

From Dev

Pandas - calculate mean and add value in new column

From Dev

Pandas: calculate mean by specific value in column with a for loop

From Dev

Calculate median or mean depending on the value of a column

From Dev

in R, how to calculate mean of all column, by group?

From Dev

How to calculate rolling mean for each column

From Dev

How to calculate the mean for a column subsetted from the data

From Java

How do I calculate the mean of a column

From Dev

How to calculate mean of years of a specific column?

From Dev

How to group specific items in a column and calculate the mean

From Dev

Calculate the mean value of a dataframe column twice based on a valid other column

From Dev

How can i calculate mean value of numpy matrix by column using for loop?

From Dev

How to calculate the mean of a specific value in columns in Python?

From Dev

how to calculate the mean value of an incomplete matrix with maxima?

From Dev

How to calculate the mean for each pixel value?

From Dev

How to ignore the zero value to calculate the mean in the dataframe

From Dev

How do I calculate the x value that corresponds to the mean density of a continuous function?

From Dev

R calculate how many values used to calculate mean in aggregate function

From

How to calculate a total for a column inside a golang template?

From Dev

R: Apply function to calculate mean of a single column of dataframe across a list

From Dev

pandas calculate mean of column that has lists instead of single value

From Dev

How can I loop over rows in my DataFrame, calculate a value and put that value in a new column with this lambda function

From Dev

how to calculate mean of data frame inside in series object (pandas)?

From Dev

Function calculate the mean

From Dev

How to calculate percentage of value inside arbitrary range?

Related Related

  1. 1

    How to calculate mean of matrix based on column value

  2. 2

    How to calculate a mean inside a window where the range of the window depends on the value of a column?

  3. 3

    How to calculate mean by skipping String Value in Numeric Column?

  4. 4

    How to calculate mean value of values on column which go comma?

  5. 5

    Using apply function to calculate the mean of a column

  6. 6

    Pandas - calculate mean and add value in new column

  7. 7

    Pandas: calculate mean by specific value in column with a for loop

  8. 8

    Calculate median or mean depending on the value of a column

  9. 9

    in R, how to calculate mean of all column, by group?

  10. 10

    How to calculate rolling mean for each column

  11. 11

    How to calculate the mean for a column subsetted from the data

  12. 12

    How do I calculate the mean of a column

  13. 13

    How to calculate mean of years of a specific column?

  14. 14

    How to group specific items in a column and calculate the mean

  15. 15

    Calculate the mean value of a dataframe column twice based on a valid other column

  16. 16

    How can i calculate mean value of numpy matrix by column using for loop?

  17. 17

    How to calculate the mean of a specific value in columns in Python?

  18. 18

    how to calculate the mean value of an incomplete matrix with maxima?

  19. 19

    How to calculate the mean for each pixel value?

  20. 20

    How to ignore the zero value to calculate the mean in the dataframe

  21. 21

    How do I calculate the x value that corresponds to the mean density of a continuous function?

  22. 22

    R calculate how many values used to calculate mean in aggregate function

  23. 23

    How to calculate a total for a column inside a golang template?

  24. 24

    R: Apply function to calculate mean of a single column of dataframe across a list

  25. 25

    pandas calculate mean of column that has lists instead of single value

  26. 26

    How can I loop over rows in my DataFrame, calculate a value and put that value in a new column with this lambda function

  27. 27

    how to calculate mean of data frame inside in series object (pandas)?

  28. 28

    Function calculate the mean

  29. 29

    How to calculate percentage of value inside arbitrary range?

HotTag

Archive