简体   繁体   English

如何根据R中的两个不同列添加行的值?

[英]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用户会告诉您的内容(即使用plyrddply()

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.

相关问题 R-比较不同行中两列中的值 - R - Compare values in two columns in different rows R根据不同列中范围内的值添加新列 - R add new column depending on values in a range in different columns 最后3行的R平均值(不同列中的值)按两列分组 - R average of last 3 rows(values in different columns) grouping by two columns 如何根据r中的两个不同列删除重复的行? - How to remove duplicated row depending on two different columns in r? 如何识别 R 中两个不同列中观察结果相同的行? - How to identify rows where observations are identical in two different columns in R? 如何根据 r 中三列的值对不同的行进行分类 - how to categorized different rows based on values of three columns in r 如何根据使用 R 对数据框中前两列的评估有条件地用 NA 填充行? - How to conditionally fill in rows with NAs depending on an evaluation of the first two columns in a dataframe using R? 有没有一种方法可以将行根据值转换为列 - Is there a way to convert rows to columns depending on values Using R R:如何根据在某一列中应用的计算来删除行,即引用其他列的值? - R: How to get rid of rows depending on a calculation applied in a certain column, that refers to values of other columns? 使用 R 通过不同的行和列交叉链接值 - Crosslink values through different rows and columns with R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM