当前位置:网站首页>Count number of rows per group and add result to original data frame
Count number of rows per group and add result to original data frame
2022-07-01 08:14:00 【javail】
problem :
Say I have a data.frame object: Let's say I have one data.frame object :
df <- data.frame(name=c('black','black','black','red','red'), type=c('chair','chair','sofa','sofa','plate'), num=c(4,5,12,4,3))Now I want to count the number of rows (observations) of for each combination of name and type . Now I want to calculate name and type Rows per combination ( Observe ).This can be done like so: This can be done like this :
table(df[ , c("name","type")])or possibly also with plyr , (though I am not sure how). Or you can use plyr ,( Although I'm not sure how ).
However, how do I get the results incorporated into the original data frame? however , How to merge the results into the original data frame ?So that the results will look like this: The result will be as follows :
df# name type num count# 1 black chair 4 2# 2 black chair 5 2# 3 black sofa 12 1# 4 red sofa 4 1# 5 red plate 3 1where count now stores the results from the aggregation. Now? count Store aggregated results .
A solution with plyr could be interesting to learn as well, though I would like to see how this is done with base R. Use plyr Your solution can also be interesting , Although I want to see how this is used on the basis R Accomplished .
Solution :
Reference resources : https://stackoom.com/en/question/VGEy边栏推荐
- 【Redis】一气呵成,带你了解Redis安装与连接
- Sqlalchemy creating MySQL_ Table
- 力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
- Instead of houses, another kind of capital in China is rising
- 2022.6.30 省赛+蓝桥国赛记录
- Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
- The Windows C disk is full
- Implementation and encapsulation of go universal dynamic retry mechanism
- Contenttype comparison of all types
- 軟鍵盤高度報錯
猜你喜欢

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

【入门】输入n个整数,输出其中最小的k个

SQL number injection and character injection

使用beef劫持用户浏览器
![[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions](/img/2c/07d729d49b1d74553decac4588074e.png)
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions

软键盘高度报错

web254

On several key issues of digital transformation
![[getting started] extract non repeating integers](/img/88/3e96df88e980bd98ac112b18a8678c.png)
[getting started] extract non repeating integers

凸印的印刷原理及工艺介绍
随机推荐
数字转excel的字符串坐标
【入门】输入n个整数,输出其中最小的k个
手工挖XSS漏洞
【力扣10天SQL入门】Day10 控制流
web254
SQL number injection and character injection
使用beef劫持用戶瀏覽器
SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
Erreur de hauteur du clavier souple
一套十万级TPS的IM综合消息系统的架构实践与思考
Provincial selection + noi Part II string
Programmer's regimen
Li Kou daily question - day 31 -1502 Judge whether an arithmetic sequence can be formed
5大组合拳,解决校园6大难题,护航教育信息化建设
Source code analysis of open source API gateway APIs IX
How to troubleshoot SharePoint online map network drive failure?
PHP laravel wechat payment
How to use layui to display the data in the database in the form of tables
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
【入门】提取不重复的整数