如何根据条件获取R中多列的中位数(根据另一列)
How do I get the median of multiple columns in R with conditions (according to another column)
我是 R 的初学者,我想知道如何完成以下任务:
我想用数据集中所有列的中位数替换数据集中缺失的值。
但是,对于每一列,我想要某个类别的中位数(取决于另一列)。我的数据集如下
structure(list(Country = structure(1:5, .Label = c("Afghanistan",
"Albania", "Algeria", "Andorra", "Angola"), class = "factor"),
CountryID = 1:5, Continent = c(1L, 2L, 3L, 2L, 3L), Adolescent.fertility.rate.... = c(151L,
27L, 6L, NA, 146L), Adult.literacy.rate.... = c(28, 98.7,
69.9, NA, 67.4)), class = "data.frame", row.names = c(NA,
-5L))
因此,对于每一列,我想用 特定大陆中值的中位数替换缺失值。
这是一个使用库 dplyr
的解决方案。我调用了您的数据框 ww
并重命名了您的列:
library('dplyr')
ww %>%
rename(
lit_rate = Adult.literacy.rate....
) %>%
group_by(
Continent
) %>%
mutate(
lit_rate = replace(
lit_rate,
is.na(lit_rate),
median(lit_rate, na.rm = TRUE)
)
) %>%
ungroup()
我们可以使用 dplyr::mutate_at
将每列中的 NA
替换为 [=14] 的中位数(Continent
和非数字列 Country
除外) =]组
df <- structure(list(Country = structure(1:5, .Label = c("Afghanistan", "Albania", "Algeria", "Andorra", "Angola"), class = "factor"),
CountryID = 1:5, Continent = c(1L, 2L, 3L, 2L, 3L),
Adolescent.fertility.rate.... = c(151L, 27L, 6L, NA, 146L),
Adult.literacy.rate.... = c(28, 98.7, 69.9, NA, 67.4)), class = "data.frame", row.names = c(NA, -5L))
library(dplyr)
df %>%
group_by(Continent) %>%
mutate_at(vars(-group_cols(), -Country), ~ifelse(is.na(.), median(., na.rm = TRUE), .)) %>%
ungroup()
Returns:
# A tibble: 5 x 5
Country CountryID Continent Adolescent.fertility.rate.... Adult.literacy.rate....
<fct> <int> <int> <int> <dbl>
1 Afghanistan 1 1 151 28
2 Albania 2 2 27 98.7
3 Algeria 3 3 6 69.9
4 Andorra 4 2 27 98.7
5 Angola 5 3 146 67.4
说明:
首先,我们将 data.frame df
按 Continent
分组。然后我们通过以下方式改变所有列 除了 分组列(和 Country
不是数字):如果 is.na
为真,我们将其替换为中位数,并且由于我们是分组的,因此它将成为 Continent
组的中位数(如果不是 NA
我们将其替换为自身)。最后我们调用 ungroup
来获得 'normal' tibble.
我是 R 的初学者,我想知道如何完成以下任务:
我想用数据集中所有列的中位数替换数据集中缺失的值。 但是,对于每一列,我想要某个类别的中位数(取决于另一列)。我的数据集如下
structure(list(Country = structure(1:5, .Label = c("Afghanistan",
"Albania", "Algeria", "Andorra", "Angola"), class = "factor"),
CountryID = 1:5, Continent = c(1L, 2L, 3L, 2L, 3L), Adolescent.fertility.rate.... = c(151L,
27L, 6L, NA, 146L), Adult.literacy.rate.... = c(28, 98.7,
69.9, NA, 67.4)), class = "data.frame", row.names = c(NA,
-5L))
因此,对于每一列,我想用 特定大陆中值的中位数替换缺失值。
这是一个使用库 dplyr
的解决方案。我调用了您的数据框 ww
并重命名了您的列:
library('dplyr')
ww %>%
rename(
lit_rate = Adult.literacy.rate....
) %>%
group_by(
Continent
) %>%
mutate(
lit_rate = replace(
lit_rate,
is.na(lit_rate),
median(lit_rate, na.rm = TRUE)
)
) %>%
ungroup()
我们可以使用 dplyr::mutate_at
将每列中的 NA
替换为 [=14] 的中位数(Continent
和非数字列 Country
除外) =]组
df <- structure(list(Country = structure(1:5, .Label = c("Afghanistan", "Albania", "Algeria", "Andorra", "Angola"), class = "factor"),
CountryID = 1:5, Continent = c(1L, 2L, 3L, 2L, 3L),
Adolescent.fertility.rate.... = c(151L, 27L, 6L, NA, 146L),
Adult.literacy.rate.... = c(28, 98.7, 69.9, NA, 67.4)), class = "data.frame", row.names = c(NA, -5L))
library(dplyr)
df %>%
group_by(Continent) %>%
mutate_at(vars(-group_cols(), -Country), ~ifelse(is.na(.), median(., na.rm = TRUE), .)) %>%
ungroup()
Returns:
# A tibble: 5 x 5 Country CountryID Continent Adolescent.fertility.rate.... Adult.literacy.rate.... <fct> <int> <int> <int> <dbl> 1 Afghanistan 1 1 151 28 2 Albania 2 2 27 98.7 3 Algeria 3 3 6 69.9 4 Andorra 4 2 27 98.7 5 Angola 5 3 146 67.4
说明:
首先,我们将 data.frame df
按 Continent
分组。然后我们通过以下方式改变所有列 除了 分组列(和 Country
不是数字):如果 is.na
为真,我们将其替换为中位数,并且由于我们是分组的,因此它将成为 Continent
组的中位数(如果不是 NA
我们将其替换为自身)。最后我们调用 ungroup
来获得 'normal' tibble.