Calculate mean based on range within a cell

Sherlockolmes

I'm new to R (and Stackoverflow, so sorry about the formatting of the question), and working on an assignment for class.

As part of this, I have a data frame with a chr column of the number of participants in an event. Some of these are whole values, whereas some are ranges.

To proceed, I need to replace the ranges with the midrange based on the values within the cell.

As an example:

df <- data.frame(event=c('Football', 'Basketball', 'Football', 'Tennis', 'Basketball'),
                 num_participants=c(157, 220, 175-189, 190-220, 320-350)

How would I replace the ranges with the mean of the max/min of the range, while maintaining the singular values (e.g., 157 & 220)?

Denny Chen

Expand the column into left-side and right-side using separate. Since the separate is a function used to deal with strings, we have to change the column type to numeric and calculate the mean statisitcs.

library(tidyverse)

df <- data.frame(event=c('Football', 'Basketball', 'Football', 'Tennis', 'Basketball'),
                 num_participants=c("157", "220", "175-189", "190-220", "320-350")) 

df %>% 
  separate(col = num_participants, sep = "-", into = c("min", "max")) %>%
  mutate(max = as.numeric(ifelse(is.na(max), min, max)),
         min = as.numeric(min),
         mean = (min+max)/2) 
#
       event min max mean
1   Football 157 157  157
2 Basketball 220 220  220
3   Football 175 189  182
4     Tennis 190 220  205
5 Basketball 320 350  335

Note that separate will create missing value when there is no delimeter found in the observation. Example showed below :

> df %>% 
+   separate(col = num_participants, sep = "-", into = c("min", "max"))

#
       event min  max
1   Football 157 <NA>
2 Basketball 220 <NA>
3   Football 175  189
4     Tennis 190  220
5 Basketball 320  350

Warning message:
Expected 2 pieces. Missing pieces filled with `NA` in 2 rows [1, 2]. 

I replace the missing in column max to the same value as min.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert columns, within a range, based on cell reference

From Dev

Recognize if a cell is a fractional value within a range based on a certain condition

From Dev

Using VBA, how to calculate a range of cells based on another cell range's value

From Dev

Conditional formatting - coloring a row based on whether a cell within the row matches a cell outside the range

From Dev

Calculate mean for subgroup within group

From Dev

check if cell ref is within a range

From Dev

Check if object is within a cell range

From Dev

Is there a way to calculate the standard deviation of a given range in excel, based on a cell entry in another column?

From Dev

Calculate the value of a cell based on another cell in Excel

From Dev

Calculate days based on date range

From Dev

Calculate Rate based on distance range

From Dev

Calculate mean cell values over different files

From Dev

Chart range based on the value in a cell

From Dev

Calculate a mean, by a condition, within a factor [r]

From Dev

Calculate the mean difference within and between groups

From Dev

Calculate mean of a range of XML Elements in PHP

From Dev

Calculate mean and median by date range in Shiny

From Dev

Calculate sum of a range as per cell condition and value

From Dev

Calculate randomly integers within a range in python

From Dev

Calculate how many items within a range

From Dev

Couldn't calculate prime numbers within a range

From Dev

calculate the percentage of data that are within the range in shiny

From Dev

Select Range within Row of Active Cell

From Dev

Select a specific cell within the range of columns

From Dev

Determine when cell value is within date range

From Dev

Return list of all cell addresses within a Range

From Dev

How to search within a range of numbers in a cell in Excel?

From Dev

Countifs Formula Partial Text within Cell Range

From Dev

Google Sheets: Conditional Formatting - color all non-empty cells in a row within a range based on the value of another cell?

Related Related

  1. 1

    Insert columns, within a range, based on cell reference

  2. 2

    Recognize if a cell is a fractional value within a range based on a certain condition

  3. 3

    Using VBA, how to calculate a range of cells based on another cell range's value

  4. 4

    Conditional formatting - coloring a row based on whether a cell within the row matches a cell outside the range

  5. 5

    Calculate mean for subgroup within group

  6. 6

    check if cell ref is within a range

  7. 7

    Check if object is within a cell range

  8. 8

    Is there a way to calculate the standard deviation of a given range in excel, based on a cell entry in another column?

  9. 9

    Calculate the value of a cell based on another cell in Excel

  10. 10

    Calculate days based on date range

  11. 11

    Calculate Rate based on distance range

  12. 12

    Calculate mean cell values over different files

  13. 13

    Chart range based on the value in a cell

  14. 14

    Calculate a mean, by a condition, within a factor [r]

  15. 15

    Calculate the mean difference within and between groups

  16. 16

    Calculate mean of a range of XML Elements in PHP

  17. 17

    Calculate mean and median by date range in Shiny

  18. 18

    Calculate sum of a range as per cell condition and value

  19. 19

    Calculate randomly integers within a range in python

  20. 20

    Calculate how many items within a range

  21. 21

    Couldn't calculate prime numbers within a range

  22. 22

    calculate the percentage of data that are within the range in shiny

  23. 23

    Select Range within Row of Active Cell

  24. 24

    Select a specific cell within the range of columns

  25. 25

    Determine when cell value is within date range

  26. 26

    Return list of all cell addresses within a Range

  27. 27

    How to search within a range of numbers in a cell in Excel?

  28. 28

    Countifs Formula Partial Text within Cell Range

  29. 29

    Google Sheets: Conditional Formatting - color all non-empty cells in a row within a range based on the value of another cell?

HotTag

Archive