如何在R中编写代码,为两个不同变量的所有相同组合添加一个变量的值?例如,我要分别添加cd: 403县: 4017 /and的所有pop 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如果已经回答了关于这个话题的问题。我应该用什么关键词来搜索它?
提前感谢!
发布于 2013-12-07 07:41:40
require(plyr)
ddply(df,.(cd,county),summarize,total=sum(pop))
cd county total
1 403 4017 6276
2 406 4017 15616发布于 2013-12-07 08:13:13
@Troy给出的答案可能是大多数R用户会告诉您什么(即使用plyr和ddply() )。
但是,由于我第一次接触数据分析是通过数据库脚本编写的,所以对于这类任务,我仍然倾向于使用sqldf包。
我还发现SQL对非R用户更加透明(我在社会科学社区中经常遇到这种情况,我大部分的工作都是在社会科学社区完成的)。
下面是使用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 15616https://stackoverflow.com/questions/20438917
复制相似问题