Calculate mean of calculated values

BERA

I want to calculate the mean of the resulting values returned by abs(((column A - column B)/column A)*100)

So for example on mtcars data i try:

> mtcars
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
...

mean((abs(mtcars['cyl']-mtcars['mpg'])/mtcars['mpg'])*100)

Which gives me error:

Warning message: In mean.default((abs(mtcars["cyl"] - mtcars["mpg"])/mtcars["mpg"]) * : argument is not numeric or logical: returning NA

How can i fix this?

Sotos

You need to use $ operator to extract the values as vectors or use double brackets, i.e.

mean((abs(mtcars[['cyl']]-mtcars[['mpg']])/mtcars[['mpg']])*100)
#[1] 64.13455

#or

 mean((abs(mtcars$cyl-mtcars$mpg)/mtcars$mpg)*100)
#[1] 64.13455

You can see the difference in structure,

str(mtcars['cyl'])
'data.frame':   32 obs. of  1 variable:
 $ cyl: num  6 6 4 6 8 6 8 4 4 6 ...

str(mtcars[['cyl']])
 num [1:32] 6 6 4 6 8 6 8 4 4 6 ...

str(mtcars$cyl)
 num [1:32] 6 6 4 6 8 6 8 4 4 6 ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Python

Calculate mean of df, BUT if =>1 of the values differs >20% from this mean, the mean is set to NaN

From Java

How to calculate mean values grouped on another column in Pandas

From Dev

Calculate dataframe mean by skipping certain values in Python / Pandas

From Dev

Pythonic way to calculate the mean and variance of values in Counters

From Dev

Calculate mean on values in python collections.Counter

From Dev

Calculate mean cell values over different files

From Dev

Calculate mean change in values one day later

From Dev

r - Calculated mean and sum values group by the first row

From Dev

How to calculate mean of every three values of a list

From Dev

Getting the values calculated by stat_summary with mean_cl_boot

From Dev

Calculate mean for each row containing lists of values

From Dev

Calculate mean with a filter on a column's values

From Dev

Calculate mean of HashMap values after insertion

From Dev

Calculate mean for only three values per row

From Dev

How to delete some of the mean weekly values calculated by pandas (.resample)?

From Dev

JavaScript; calculate a mean, excluding certain values

From Dev

Calculate and submit form values with jquery and ajax and add calculated values to specific div

From Dev

Excel: calculate rank avoiding duplicated values and using a calculated range

From Dev

Sum tuples values to calculate mean - RDD

From Dev

how can i calculated values after Worksheet.Calculate()?

From Dev

Calculate the mean values if the numbers are same

From Dev

How to calculate Mean of specific values in each row of a data frame?

From Dev

Subset / group by pandas Data Frame to calculate mean and apply to missing values

From Dev

How to calculate mean of values per unique class

From Dev

Calculate mean difference values

From Dev

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

From Dev

Calculate max, min and mean values of element in an array

From Dev

Calculate the mean of values in a loop

From Dev

Calculate the mean across rows excluding min and max values?

Related Related

  1. 1

    Calculate mean of df, BUT if =>1 of the values differs >20% from this mean, the mean is set to NaN

  2. 2

    How to calculate mean values grouped on another column in Pandas

  3. 3

    Calculate dataframe mean by skipping certain values in Python / Pandas

  4. 4

    Pythonic way to calculate the mean and variance of values in Counters

  5. 5

    Calculate mean on values in python collections.Counter

  6. 6

    Calculate mean cell values over different files

  7. 7

    Calculate mean change in values one day later

  8. 8

    r - Calculated mean and sum values group by the first row

  9. 9

    How to calculate mean of every three values of a list

  10. 10

    Getting the values calculated by stat_summary with mean_cl_boot

  11. 11

    Calculate mean for each row containing lists of values

  12. 12

    Calculate mean with a filter on a column's values

  13. 13

    Calculate mean of HashMap values after insertion

  14. 14

    Calculate mean for only three values per row

  15. 15

    How to delete some of the mean weekly values calculated by pandas (.resample)?

  16. 16

    JavaScript; calculate a mean, excluding certain values

  17. 17

    Calculate and submit form values with jquery and ajax and add calculated values to specific div

  18. 18

    Excel: calculate rank avoiding duplicated values and using a calculated range

  19. 19

    Sum tuples values to calculate mean - RDD

  20. 20

    how can i calculated values after Worksheet.Calculate()?

  21. 21

    Calculate the mean values if the numbers are same

  22. 22

    How to calculate Mean of specific values in each row of a data frame?

  23. 23

    Subset / group by pandas Data Frame to calculate mean and apply to missing values

  24. 24

    How to calculate mean of values per unique class

  25. 25

    Calculate mean difference values

  26. 26

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

  27. 27

    Calculate max, min and mean values of element in an array

  28. 28

    Calculate the mean of values in a loop

  29. 29

    Calculate the mean across rows excluding min and max values?

HotTag

Archive