R语言dplyr包高效处理数据-补充函数

++前言:++

++已经1年多没有更新博客了,++

++似乎工作后时间会越来越少,获取知识的动力和精力有了变化,平凡的普通人大概就是这样吧。++

dplyr包补充用到的实用函数:

1、函数cur_group_:数据分组组内标记

R 复制代码
#使用常见的R语言内置数据集iris
#可以明确的知道当前数据属于哪个分组
iris %>% 
  group_by(Species) %>% 
  mutate(group_tag=cur_group_id())

# A tibble: 150 x 6
# Groups:   Species [3]
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species group_tag
          <dbl>       <dbl>        <dbl>       <dbl> <fct>       <int>
 1          5.1         3.5          1.4         0.2 setosa          1
 2          4.9         3            1.4         0.2 setosa          1
 3          4.7         3.2          1.3         0.2 setosa          1

#可以明确的知道分组后数据在原始数据所在的行,相当于索引
iris %>% 
    group_by(Species) %>% 
    mutate(row_tag= cur_group_rows()) 

# A tibble: 150 x 6
# Groups:   Species [3]
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species row_tag
          <dbl>       <dbl>        <dbl>       <dbl> <fct>     <int>
 1          5.1         3.5          1.4         0.2 setosa        1
 2          4.9         3            1.4         0.2 setosa        2
 3          4.7         3.2          1.3         0.2 setosa        3

#将分组变量以外的数据嵌套起来→相当于dplyr中的group_nest函数
iris %>% 
  group_by(Species) %>% 
  summarise(data_tag = list(cur_data()))

# A tibble: 3 x 2
  Species    data_tag         
  <fct>      <list>           
1 setosa     <tibble [50 x 4]>
2 versicolor <tibble [50 x 4]>
3 virginica  <tibble [50 x 4]>

2、快速统计分组数据count、add_count

R 复制代码
#快速计算分组变量标签的行数
iris %>% count(Species)

     Species  n
1     setosa 50
2 versicolor 50
3  virginica 50

#灵活计算分组变量指定标签值
iris %>% add_count(Species, wt = Petal.Length)

    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species     n
1            5.1         3.5          1.4         0.2     setosa  73.1
2            4.9         3.0          1.4         0.2     setosa  73.1
3            4.7         3.2          1.3         0.2     setosa  73.1
4            4.6         3.1          1.5         0.2     setosa  73.1
5            5.0         3.6          1.4         0.2     setosa  73.1

3、cumall、cumany条件判断为TRUE/FALSE的前后数据过滤

R 复制代码
#计算user_data数据指定条件过滤后按照user_var2排序,再过滤直到第1个user_var3=='y'为TRUE的所有数据
user_data %>% 
  filter(user_var1==x) %>% 
  arrange(user_var2) %>% 
  filter(cumall(!(user_var3=='y'))) %>% 
  summarise(result_tag=max(user_var4))
示例:
iris %>%arrange(Sepal.Width) %>% filter(cumall(!Petal.Length>=5))

  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          5.0         2.0          3.5         1.0 versicolor
2          6.0         2.2          4.0         1.0 versicolor
3          6.2         2.2          4.5         1.5 versicolor

还没写完!!

相关推荐
铁匠匠匠2 分钟前
【C总集篇】第八章 数组和指针
c语言·开发语言·数据结构·经验分享·笔记·学习·算法
编程小白煎堆6 分钟前
C语言:枚举类型
java·开发语言
秋邱11 分钟前
C++: 类和对象(上)
开发语言·c++
好多吃的啊15 分钟前
背景图鼠标放上去切换图片过渡效果
开发语言·javascript·ecmascript
神奇夜光杯20 分钟前
Python酷库之旅-第三方库Pandas(123)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
zhangbin_23726 分钟前
【Python机器学习】NLP信息提取——提取人物/事物关系
开发语言·人工智能·python·机器学习·自然语言处理
GoFly开发者28 分钟前
GoFly快速开发框架/Go语言封装的图像相似性比较插件使用说明
开发语言·后端·golang
_.Switch36 分钟前
构建现代应用的Python Serverless架构详解
运维·开发语言·python·云原生·架构·serverless·restful
苹果酱056737 分钟前
通过springcloud gateway优雅的进行springcloud oauth2认证和权限控制
java·开发语言·spring boot·后端·中间件
Sunny_yiyi40 分钟前
Gateway--服务网关
java·开发语言·gateway