当前位置:网站首页>Use group_ Dplyr issues when using group_ by(multiple variables)
Use group_ Dplyr issues when using group_ by(multiple variables)
2022-06-12 21:54:00 【Rich in starch】
problem :
I want to start using dplyr in place of ddply but I can't get a handle on how it works (I've read the documentation). I want to start using dplyr Instead of ddply But I can't understand how it works ( I have read the document ).
For example, why when I try to mutate() something does the "group_by" function not work as it's supposed to? for example , Why when I try mutate() when ,“group_by” Function does not work properly ?
Looking at mtcars: see mtcars:
library(car) The library ( automobile )
Say I make a data.frame which is a summary of mtcars, grouped by "cyl" and "gear": Suppose I make a data.frame, It is mtcars A summary of the , Press “cyl” and “gear” grouping :
df1 <- mtcars %.% group_by(cyl, gear) %.% summarise( newvar = sum(wt) )Then say I want to further summarise this dataframe. Then say I want to summarize this data frame further .With ddply, it'd be straightforward, but when I try to do with with dplyr, it's not actually "grouping by": Use ddply, It would be simple , But when I try to use dplyr when , It's not really “ Group by ”:
df2 <- df1 %.% group_by(cyl) %.% mutate( newvar2 = newvar + 5 )Still yields an ungrouped output: Still produce ungrouped output :
cyl gear newvar newvar21 6 3 6.675 11.6752 4 4 19.025 24.0253 6 4 12.375 17.3754 6 5 2.770 7.7705 4 3 2.465 7.4656 8 3 49.249 54.2497 4 5 3.653 8.6538 8 5 6.740 11.740Am I doing something wrong with the syntax? Did I make a mistake in grammar ?
Edit: edit :
If I were to do this with plyr and ddply: If I want to use plyr and ddply Do this :
df1 <- ddply(mtcars, .(cyl, gear), summarise, newvar = sum(wt))and then to get the second df: Then get the second df:
df2 <- ddply(df1, .(cyl), summarise, newvar2 = sum(newvar) + 5)But that same approach, with sum(newvar) + 5 in the summarise() function doesn't work with dplyr... But the same way , stay summarise() Use in a function sum(newvar) + 5 Do not apply to dplyr ...
Solution :
Reference resources : https://stackoom.com/en/question/1Sr0h边栏推荐
- Icml2022 | galaxy: active learning of polarization map
- My struggle: my years in foreign enterprises (1)
- Thread safe level
- Jdbctemplate inserts and returns the primary key
- MySQL architecture and basic management (II)
- PCB封装下载网站推荐及其详细使用方法
- 回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
- Logstash timestamp converted to UNIX nanosecond nano second time
- selenium操作元素遇到的异常
- 经济学人聚焦WTO MC12:数字经济或成重要议题
猜你喜欢

建立高可用的数据库

SQL调优指南笔记6:Explaining and Displaying Execution Plans

利用ADG Standby克隆PDB

Ansible基础和常用模块(一)

SQL调优指南笔记10:Optimizer Statistics Concepts

“Oracle数据库并行执行”技术白皮书读书笔记

About the solution to "the application cannot start normally 0xc00000022" after qt5.15.2 is installed and qtcreator is started

关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法

SQL调优指南笔记11:Histograms

2021 rust survey results released: 9354 questionnaires collected
随机推荐
Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
Lambda expression and flow optimization code
talent showing itself! Oceanbase was selected into the 2021 "sci tech innovation China" open source innovation list
有向图深拷贝
2023届校园招聘正式开启!OceanBase 想和你在这个春天约一场面试
【QNX Hypervisor 2.2 用户手册】4.4 构建Host
回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
Ansible foundation and common modules (I)
How to implement a simple publish subscribe mode
linux备份mysql
[target detection] |dive detector into box for object detection new training method based on fcos
Build a highly available database
How to prevent phishing emails? S/mime certificate to help!
OceanBase 社区版 OCP 功能解读
What is the difference between volatile variables and atomic variables?
Ansible roles project case (IV)
Libmysqlclient A static library
CVPR 2022 | 应对噪声标签,西安大略大学、字节跳动等提出对比正则化方法
How do I create a daemon thread? And where to use it?
[simple] 155 Minimum stack
