tidyverse_logo()
* __ _ __ . o * .
/ /_(_)__/ /_ ___ _____ _______ ___
/ __/ / _ / // / |/ / -_) __(_-</ -_)
\__/_/\_,_/\_, /|___/\__/_/ /___/\__/
* . /___/ o . *
我發(fā)現(xiàn)以下命令在任何數(shù)據(jù)科學項目的EDA部分中都非常有用媒熊。我們將使用tidyverse包奇适,實際上只需要dplyr和ggplot2和用iris數(shù)據(jù)集,來演示它們芦鳍。
select_if | rename_if
select_if
函數(shù)屬于dply
嚷往,在需要根據(jù)某些條件選擇某些列時非常有用。我們還可以添加一個應用于列名的函數(shù)柠衅。
library(tidyverse)
iris%>%select_if(is.numeric, list(~ paste0("numeric_", .)))%>%head()
numeric_Sepal.Length numeric_Sepal.Width numeric_Petal.Length numeric_Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
注意皮仁,我們也可以以同樣的方式使用rename_if
。需要注意的是rename_if()菲宴、rename_at()和rename_all()已經(jīng)被rename_with()取代了贷祈。匹配的select語句已被select() + rename_with()的組合取代。
這些函數(shù)被替換喝峦,因為mutate_if()和friends被across()取代势誊。select_if()和rename_if()已經(jīng)使用了整齊選擇,所以它們不能被across()替換谣蠢,相反键科,我們需要一個新函數(shù)闻丑。
everything
在許多數(shù)據(jù)科學項目中,我們希望一個特定的列(通常是因變量y)出現(xiàn)在數(shù)據(jù)集中的第一個或最后一個勋颖。我們可以使用dplyr包中的everything()
來實現(xiàn)這一點嗦嗡。
iris%>%select(Species, everything()) %>%head()
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 5.1 3.5 1.4 0.2
2 setosa 4.9 3.0 1.4 0.2
3 setosa 4.7 3.2 1.3 0.2
4 setosa 4.6 3.1 1.5 0.2
5 setosa 5.0 3.6 1.4 0.2
6 setosa 5.4 3.9 1.7 0.4
mydataset <- iris%>%select(Species, everything())
mydataset%>%select(-Species, everything())%>%head()
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
relocate
relocate()是dplyr 1.0.0中新增的一個功能。您可以指定將.before或.after的列位置饭玲。
iris%>%relocate(Petal.Width, .after=Sepal.Width)%>%head()
Sepal.Length Sepal.Width Petal.Width Petal.Length Species
1 5.1 3.5 0.2 1.4 setosa
2 4.9 3.0 0.2 1.4 setosa
3 4.7 3.2 0.2 1.3 setosa
4 4.6 3.1 0.2 1.5 setosa
5 5.0 3.6 0.2 1.4 setosa
6 5.4 3.9 0.4 1.7 setosa
iris%>%relocate(Petal.Width, .after=last_col())%>%head()
Sepal.Length Sepal.Width Petal.Length Species Petal.Width
1 5.1 3.5 1.4 setosa 0.2
2 4.9 3.0 1.4 setosa 0.2
3 4.7 3.2 1.3 setosa 0.2
4 4.6 3.1 1.5 setosa 0.2
5 5.0 3.6 1.4 setosa 0.2
6 5.4 3.9 1.7 setosa 0.4
pull
當我們處理數(shù)據(jù)幀時侥祭,我們選擇單個列,有時我們輸出為as.vector茄厘。我們可以使用作為dplyr一部分的pull()來實現(xiàn)這一點
setosa_sepal_length<-iris%>%filter(Species=='setosa')%>%select(Sepal.Length)%>%pull()
setosa_sepal_length
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8
[32] 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0
virginica_sepal_length<-iris%>%filter(Species=='virginica')%>%select(Sepal.Length)%>%pull()
t.test(setosa_sepal_length,virginica_sepal_length)
Welch Two Sample t-test
data: setosa_sepal_length and virginica_sepal_length
t = -15.386, df = 76.516, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.78676 -1.37724
sample estimates:
mean of x mean of y
reorder
當您使用ggplot2時矮冬,有時會感到沮喪,因為您必須根據(jù)某些條件重新排序這些因素次哈。假設我們想要展示萼片的箱形圖胎署。寬度的物種。
iris%>%ggplot(aes(x=Species, y=Sepal.Width))+geom_boxplot() + theme_bw()
iris%>%ggplot(aes(x=reorder(Species,Sepal.Width, FUN = median), y=Sepal.Width))+geom_boxplot()+xlab("Species") + theme_bw()
across
令人驚訝的是窑滞,使across()起作用的關(guān)鍵思想來自于我們在vctrs包上的底層工作琼牧,在那里我們了解到數(shù)據(jù)幀的一列本身就是數(shù)據(jù)幀
mpg %>% head()
# A tibble: 6 x 11
manufacturer model displ year cyl trans drv cty hwy fl class
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
3 audi a4 2 2008 4 manual(m6) f 20 31 p compact
4 audi a4 2 2008 4 auto(av) f 21 30 p compact
5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
mpg %>%
group_by(class) %>%
summarise(
across(
c(cty, hwy),
.fns = list(
"mean" = ~ mean(.x),
"range lo" = ~ (mean(.x) - 2*sd(.x)),
"range hi" = ~ (mean(.x) + 2*sd(.x))
),
.names = "{.fn} {.col}"
),
.groups = "drop"
) %>%
rename_with(.fn = str_to_upper)
# A tibble: 7 x 7
CLASS `MEAN CTY` `RANGE LO CTY` `RANGE HI CTY` `MEAN HWY` `RANGE LO HWY` `RANGE HI HWY`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2seater 15.4 14.3 16.5 24.8 22.2 27.4
2 compact 20.1 13.4 26.9 28.3 20.7 35.9
3 midsize 18.8 14.9 22.6 27.3 23.0 31.6
4 minivan 15.8 12.2 19.5 22.4 18.2 26.5
5 pickup 13 8.91 17.1 16.9 12.3 21.4
6 subcompact 20.4 11.2 29.6 28.1 17.4 38.9
7 suv 13.5 8.66 18.3 18.1 12.2 24.1
https://www.r-bloggers.com/2020/11/tidyverse-tips/
https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-colwise/