R中数据框中每年的组别雌雄同体名称和总和数 [英] Group androgynous names and sum amount for each year in a data frame in R

查看:222
本文介绍了R中数据框中每年的组别雌雄同体名称和总和数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4列的数据框,标题为'年''name''sex''amount'。这是一个样本数据集

  set.seed(1)
data = data.frame(year = sample 1950:2000,50,replace = TRUE),name = sample(LETTERS,50,replace = TRUE),
sex = sample(c(M,F),50,replace = TRUE) amount = sample(40:100,50,replace = TRUE))

这两个都是'm'和'f',并且每年的金额相加。



任何帮助将不胜感激

解决方案

我改变了一些数据,所以有时候会有通用的名字。在目视检查中,这似乎是有效的。如果您不熟悉 data.table ,但其简明扼要且非常有效:



$($)$($)$($)$($)$($)
set.seed(1)
data = data.frame TRUE),name = sample(LETTERS,100,replace = TRUE),
sex = sample(c(M,F),100,replace = TRUE),amount = sample(40: 100,replace = TRUE))
setDT(data)##更改为data.table格式
data [,common = sum(amount [%%in%intersect(name [sex ==M ],name [sex ==F])]),by = year]


I have a data frame with 4 columns titled 'year' 'name' 'sex' 'amount'. Here is a sample data set

set.seed(1)
    data = data.frame(year=sample(1950:2000, 50, replace=TRUE),name=sample(LETTERS, 50, replace=TRUE),
                       sex=sample(c("M", "F"), 50, replace=TRUE), amount=sample(40:100, 50, replace=TRUE))

I want to find only names that occur as both an ‘m’ and an ‘f’ and sum the amount for each year.

Any help would be greatly appreciated

解决方案

I changed the data a bit, so that there would be common names in some years. On visual inspection this seems to work. The syntax may appear a bit cryptic if you are not familiar with data.table but it's concise and should be quite efficient:

require(data.table)
set.seed(1)
data = data.frame(year=sample(1950:1960, 100, replace=TRUE),name=sample(LETTERS, 100, replace=TRUE),
                  sex=sample(c("M", "F"), 100, replace=TRUE), amount=sample(40:100, 100,  replace=TRUE))
setDT(data) ## change to data.table format
data[, common=sum(amount[name %in% intersect(name[sex=="M"], name[sex=="F"])]), by=year]

这篇关于R中数据框中每年的组别雌雄同体名称和总和数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆