R for statistics

R数据处理案例(1) tidyverse合并统计数据

2021-01-04  本文已影响0人  R语言数据分析指南

tidyverse作为一套处理数据的工具真是异常强大,如果好好学习定能大幅度的提高我们的数据处理效率,下面一一列举一些常见的案例,喜欢的小伙伴可以关注公众号 R语言数据分析指南将分享更多实用文档,先行拜谢了

1. 替代for循环的方案

crossing(i=1:3,j=1:3,k=c("a","b"))
# A tibble: 18 x 3
       i     j k    
   <int> <int> <chr>
 1     1     1 a    
 2     1     1 b    
 3     1     2 a    
 4     1     2 b    
 5     1     3 a    
 6     1     3 b 

2.计算数据中所有成对组合的均值

df <- structure(list(x = c("A","B","C"),
                     y = c(0.401, 0.486,0.581)), class = "data.frame",
                row.names = c(NA, -3L))
head(df)

df %>% summarise(x = combn(x,2,paste0,collapse = '-'), 
          y = combn(y,2,mean))
    x      y
1 A-B 0.4435
2 A-C 0.4910
3 B-C 0.5335

3.合并数据并统计频率数据

df_raw <- data.frame(name = c("C", "A", "B", "A", "B", "C"), 
                     start = c(2, 1, 3, 4, 5, 2),  
                     end = c(7, 6, 7, 8, 10, 9)) 
df_raw %>% 
  rowwise %>% 
  transmute(name, value = list(start:end)) %>%
  unnest(c(value)) %>% 
  count(name, value)
   name  value     n
   <chr> <int> <int>
 1 A         1     1
 2 A         2     1
 3 A         3     1
 4 A         4     2
 5 A         5     2
 6 A         6     2

4.指定行添加数据

tbl1 = tibble(V1=c(1,2), V2=c(3,4))

tbl1
# A tibble: 2 x 2
     V1    V2
  <dbl> <dbl>
1     1     3
2     2     4
tbl1 %>% 
  add_row(!!!setNames(list(1.5, 3.5),names(.)), .before = 2)
     V1    V2
  <dbl> <dbl>
1   1     3  
2   1.5   3.5
3   2     4  
5. 给分面图添加标签
devtools::install_github("eliocamp/tagger")
library(tagger)
ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  facet_grid(cyl ~ vs) +
  tag_facets()+theme_bw()

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  facet_grid(cyl ~ vs) +
  tag_facets(tag = "cr")+theme_bw()
上一篇下一篇

猜你喜欢

热点阅读