[英]How to add values of rows depending on two different columns in R?
How do I make a code in R to add values of one variable for all the same composition of two different variables? 如何在R中编写代码以为两个不同变量的所有相同组成添加一个变量的值? For example, I want add all pop of cd: 403 county: 4017 /and all pop of cd :406 and county: 4017 separately.
例如,我要添加cd的所有流行音乐:403县:4017 /和cd的所有流行音乐:406和县:4017。
cd county pop
403 4017 1474
403 4017 0
403 4017 869
403 4017 393
403 4017 773
403 4017 1108
403 4017 929
403 4017 730
403 4017 0
406 4017 0
406 4017 2982
406 4017 1254
406 4017 752
406 4017 153
406 4017 0
406 4017 0
406 4017 3775
406 4017 0
406 4017 777
406 4017 5923
If there is already answered question on this topic. 有关此主题的问题是否已经回答。 What keyword should I use to google it?
我应该使用什么关键字来搜索它?
Thanks in advance! 提前致谢!
require(plyr)
ddply(df,.(cd,county),summarize,total=sum(pop))
cd county total
1 403 4017 6276
2 406 4017 15616
The answer given by @Troy is probably what most R users would tell you (ie using plyr
and ddply()
. @Troy给出的答案可能是大多数R用户会告诉您的内容(即使用
plyr
和ddply()
。
However, as my first exposure to data analysis was through database scripting, I remain partial to the sqldf
package for these sorts of tasks. 但是,由于我第一次接触数据分析是通过数据库脚本编写的,因此我仍然不
sqldf
软件包来执行这些任务。
I also find SQL to be more transparent to non-R users (something I frequently encounter in the social science community where I do most of my work). 我还发现SQL对非R用户更加透明(在我从事大部分工作的社会科学社区中经常遇到这种情况)。
Here is a solution to your problem producing identical output using sqldf
: 这是使用
sqldf
产生相同输出的问题的解决方案:
#your data assigned to dat
pop <- c(1474,0,869,393,773,1108,929,730,0
,0,2982,1254,752,153,0,0,3775,0
,777,5923)
cd <- c(rep(403, 9), rep(406, 11))
county <- rep(4017, 20)
dat <- as.data.frame(cbind(cd, county, pop))
#load sqldf
require(sqldf)
#write a simple SQL aggregate query
#i.e. "select" your fields specifying the aggregate function for the
#relevant field, "from" a table called dat, and "group by" cd and county
sqldf('select
cd
,county
,sum(pop) as total
from dat
group by
cd
,county')
cd county total
1 403 4017 6276
2 406 4017 15616
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.