我每年都会在引文上引用一个名为Paper
的数据框,包括出版年份以及一些元文章(期刊,作者)。它看起来像:
Paper = read.table(textConnection("Meta Publication.Year X1999 X2000 X2001 X2002 X2003
A 1999 0 1 1 1 2
B 2000 0 0 3 1 0
C 2000 0 0 1 0 1
C 2001 0 0 0 1 5
D 1999 0 1 0 2 2"), header = TRUE)
我想在发布两年后计算引用总和,并将此列表附加到Paper
。但是,我对每年都不感兴趣,只对列表Years
中指定的那些感兴趣。我的步骤(以下代码)如下:订单Paper
acc。 Publication.Year
,选择Publication.Year
和第一年的X行(即1999年的X2000
和X2001
),计算总和,将总和绑定在一起,cbind到Paper
有(更多)优雅的方法吗?
Years = as.numeric(c(1999, 2000))
Paper <- Paper[with(Paper, order(Paper[["Publication.Year"]])), ]
Two.Year = as.numeric()
for (i in Years){
Mat <- subset(Paper, Paper[["Publication.Year"]]==i, select=c("Publication.Year", paste("X", i+1, sep=""), paste("X", i+2, sep="")))
temp <- rowSums(Mat[,-1])
Two.Year <- c(Two.Year, temp)
rm(temp)
}
Paper <- cbind(Paper, Two.Year)
rm(Two.Year)
rm(Jahre)
Paper <- subset(Paper, select=c("Meta","Publication.Year","Two.Year")) # Because in the end I only need the citation number
答案 0 :(得分:0)
因为您对每行的兴趣年份都会发生变化,所以您必须创建新变量来表示这些年份。然后,您可以使用mapply
对正确的数字求和。
Paper$pubYear1 <- paste0("X", as.character(Paper$Publication.Year + 1))
Paper$pubYear2 <- paste0("X", as.character(Paper$Publication.Year + 2))
Paper$pubCount <- mapply(function(r, y1, y2) Paper[r, y1] + Paper[r, y2],
row.names(Paper), Paper$pubYear1, Paper$pubYear2)
以下是结果数据框:
> Paper
Meta Publication.Year X1999 X2000 X2001 X2002 X2003 pubYear1 pubYear2 pubCount
1 A 1999 0 1 1 1 2 X2000 X2001 2
2 B 2000 0 0 3 1 0 X2001 X2002 4
3 C 2000 0 0 1 0 1 X2001 X2002 1
4 C 2001 0 0 0 1 5 X2002 X2003 6
5 D 1999 0 1 0 2 2 X2000 X2001 1