# Median values in R

Median values in R, the methods listed below can be used to determine the median value of rows in R:

## Median values in R

**Example 1: Calculate the Median of Rows Using Base R**

Assume we have the data frame in R below, which displays the points earned by different basketball players throughout three distinct games:

df <- data.frame(game1=c(110, 112, 141, 115, 106, 128, 119), game2=c(104, 109, 113, 18, 105, 105, 127), game3=c(19, NA, 215, 251, 226, 320, 119)) df

game1 game2 game3 1 110 104 19 2 112 109 NA 3 141 113 215 4 115 18 251 5 106 105 226 6 128 105 320 7 119 127 119

The apply() function in base R can be used to make a new column that displays the median value for each row:

```
df$row_median = apply(df, 1, median, na.rm=TRUE)
df
```

game1 game2 game3 row_median 1 110 104 19 104.0 2 112 109 NA 110.5 3 141 113 215 141.0 4 115 18 251 115.0 5 106 105 226 106.0 6 128 105 320 128.0 7 119 127 119 119.0

The median value of each row in the data frame is stored in the new column row_median.

**Example 2: Calculate Median of Rows Using dplyr**

Assume we have the following R data frame containing the points scored by various basketball players throughout three distinct games:

ChatGPT Coursera Review » Data Science Tutorials

We can use the dplyr package’s modify() method to create a new column that displays the median value of each row for numeric columns only:

library(dplyr) df %>% rowwise() %>% mutate(row_median = median(c_across(where(is.numeric)), na.rm=TRUE))

game1 game2 game3 row_median 1 110 104 19 104 2 112 109 NA 110. 3 141 113 215 141 4 115 18 251 115 5 106 105 226 106 6 128 105 320 128 7 119 127 119 119

The new row_median column simply holds the median value of each row in the data frame for the numeric columns.