介绍几个tidyverse处理数据过程中不常用到的有用函数,喜欢的小伙伴欢迎关注个人公众号R语言数据分析指南,在此先行拜谢了
加载所需R包
library(tidyverse)
library(magrittr)
案例1
使用filter( )和str_detect( )按多种模式过滤
df <- data.frame(numbers = 1:52, letters = letters)
df
> df
numbers letters
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
6 6 f
7 7 g
8 8 h
9 9 i
df %>% filter(str_detect(letters, "a|f|o"))
> df %>% filter(str_detect(letters, "a|f|o"))
numbers letters
1 1 a
2 6 f
3 15 o
4 27 a
5 32 f
6 41 o
案例2
使用unite( )对列进行合并
df %>% unite("group",numbers,letters,sep=",")
> df %>% unite("group",numbers,letters,sep=",")
group
1 1,a
2 2,b
3 3,c
4 4,d
案例3
给字符串加“
号
string <- function(...) {
dots <- as.list(substitute(...))
vapply(dots,as.character,FUN.VALUE = character(1))[-1]
}
string(c(a,b,c))
> string(c(a,b,c))
[1] "a" "b" "c"
案例4
对3组重复样本求平均值
data <- data.frame(A1=1:10,A2=11:20,A3=12:21,
B2=80:89,B1=12:21,B3=15:24)
data
> data
A1 A2 A3 B2 B1 B3
1 1 11 12 80 12 15
2 2 12 13 81 13 16
3 3 13 14 82 14 17
4 4 14 15 83 15 18
result <- list(); b <- 3
for (i in 1:(data %>% ncol() / b)) {
result[[i]] <- data %>%
select((b * i - b + 1):(b * i) %>% all_of()) %>%
apply(1, mean)
}
data2 <- result %>% as.data.frame() %>%
set_colnames(c("A","B"))
data2
A B
1 8 35.66667
2 9 36.66667
3 10 37.66667
4 11 38.66667
5 12 39.66667
网友评论