0

私は、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)

出版から 2 年後の引用の合計を計算し、このリストを に追加したいと思いますPaper。ただし、私は毎年ではなく、リストに指定されているものだけに興味がありますYears。私の手順 (以下のコード) は次のとおりですPaperPublication.Year、選択し、最初の年 (つまり、 1999 年)Publication.Yearの X 行を選択し、合計を計算し、合計をバインドし、cbind to にバインドします。X2000X2001Paper

これを行う(より)エレガントな方法はありますか?

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
4

1 に答える 1

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
于 2014-08-02T23:17:06.323 に答える