美文网首页
2020-11-02Tidyverse Tips

2020-11-02Tidyverse Tips

作者: iColors | 来源:发表于2020-11-02 16:34 被阅读0次

原文链接:https://predictivehacks.com/tidyverse-tips/

tidyverse包对于数据处理非常有用,是个好的生产力工具。有些小窍门可以mark一下,以备日后使用。

(1) select_if|rename if

例子:如果你想在数据变量的列名前加前缀“numeric_”。

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达到同样的目的。

(2)everything

有时候想让指定的列放到第一列或最后一列,可以用everything()来实现。例如:要让iris数据中Species放到数据的第一列:

mydataset<-iris%>%select(Species, everything())
mydataset%>%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%>%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

(3)relocate

可以精确的把某一列放到另一列之前或之后。举例:把Petal.Width放到 Sepal.Width之后。

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

把Petal.Width放到最后

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

(4)pull

有时候我们用数据框,需要选择一列作为输出向量,可以用pull做到这一点。例子:如果想要用t.test比较两种鸢尾花setosa 和 virginica的Sepal.Length,可以这么做。

setosa_sepal_length<-iris%>%filter(Species=='setosa')%>%select(Sepal.Length)%>%pull()
virginica_sepal_length<-iris%>%filter(Species=='virginica')%>%select(Sepal.Length)%>%pull()

t.test(setosa_sepal_length,virginica_sepal_length)

 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 
    5.006     6.588 

(5) reorder

用ggplot2的时候有时根据特定的条件排序某些因子让人沮丧。比如我们以Species来绘制Sepal.Width的箱线图:

iris%>%ggplot(aes(x=Species, y=Sepal.Width))+geom_boxplot()
image.png

假如以Species的中位数重新排序,可以用reorder()函数:

iris%>%ggplot(aes(x=reorder(Species,Sepal.Width, FUN = median), y=Sepal.Width))+geom_boxplot()+xlab("Species")
image.png

是不是很方便!!!

相关文章

网友评论

      本文标题:2020-11-02Tidyverse Tips

      本文链接:https://www.haomeiwen.com/subject/tuymvktx.html