在R数据帧中匹配和添加因子计数 [英] Matching and Adding Factor Counts in R Data Frames

查看:183
本文介绍了在R数据帧中匹配和添加因子计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题源于这个问题先前提出,与我们认为完全不同。想象一下,我有一个最小的数据集(鸟),其中每行代表在给定时间和地点观察鸟类如下:

My question stems out of this and this question asked previously and sufficiently different from them I think. Imagine that I have a minimal dataset (bird) where every row represents an observation of birds at a given time and place as follows:

id,obs,country
A,4,USA
B,3,CAN
A,5,USA
C,4,MEX
C,1,USA
A,3,CAN
D,1,null

理想的情况是将此数据集转换成像这样的形式,从数据集中删除null:

What I ideally want is a conversion of this dataset into a form like this removing the nulls from the dataset:

id,tot_obs,country_tot
A,12,2
B,3,1
C,5,2

我知道我可以通过以下方式获得一系列因素:

I know that I can get a count of factors using:

table(bird$country)

但是,是否有一个更智能的,也许是一行删除空值的方式,加起来总计数,找到国家的计数,然后将其重新配置成这种形式?如果有一个这样的包,那么我也可以接受这个建议。谢谢!

but, is there a smarter, perhaps, one line way of removing the nulls, adding up the total counts, finding the counts of the countries and then reconfiguring them into this form? If there is a package which does this, then I am open to that suggestion as well. Thanks !

推荐答案

使用加载数据stringsAsFactors = FALSE

df <- read.csv(header=TRUE, text="id,obs,country
A,4,USA
B,3,CAN
A,5,USA
C,4,MEX
C,1,USA
A,3,CAN
D,1,null", stringsAsFactors=FALSE)

# check to see if columns are factors
sapply(df, class)
#          id         obs     country 
# "character"   "integer" "character" 

删除带有 country = null的所有行

df <- df[df$country != "null", ]

然后,您可以使用 plyr c>总结以获得所需的结果如下:

Then you can use plyr package with summarise to get the desired result as follows:

ddply(df, .(id), summarise, tot_obs=sum(obs), tot_country=length(unique(country)))
#   id tot_obs tot_country
# 1  A      12           2
# 2  B       3           1
# 3  C       5           2

这篇关于在R数据帧中匹配和添加因子计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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