使用R按类别条件

时间:2017-06-05 01:50:41

标签: r

我有一个数据框,按年显示出版物的数量。但我对会议和期刊出版物感兴趣。我想在其他类型中总结所有其他类别。

数据框示例:

year    type                n    
1994    Conference          2    
1994    Journal             3    
1995    Conference         10    
1995    Editorship          3    
1996    Conference         20    
1996    Editorship          2    
1996    Books and Thesis    3    

结果将是:

year type             n    
1994    Conference    2    
1994    Journal       3    
1995    Conference   10    
1995    Other         3    
1996    Conference   20    
1996    Other         5    

3 个答案:

答案 0 :(得分:4)

使用dplyr我们可以replace除“日记”或“会议”以外的任何内容,然后sumyear {会员}。 。

type

答案 1 :(得分:1)

我们可以使用data.table

library(data.table)
library(stringr)
setDT(df1)[, .(n = sum(n)), .(year, type = str_replace(type, 
       '(Journal|Conference)', 'Other'))]
#   year             type  n
#1: 1994            Other  5
#2: 1995            Other 10
#3: 1995       Editorship  3
#4: 1996            Other 20
#5: 1996       Editorship  2
#6: 1996 Books and Thesis  3

答案 2 :(得分:0)

<script>
$('#date_field').datepicker({
        format: 'dd/mm/yyyy'
});
</script>

输入数据:

levels(df$type)[levels(df$type) %in% c("Editorship", "Books_and_Thesis")] <- "Other"
aggregate(n ~ type + year, data=df, sum)

#         type year  n
# 1 Conference 1994  2
# 2    Journal 1994  3
# 3      Other 1995  3
# 4 Conference 1995 10
# 5      Other 1996  5
# 6 Conference 1996 20