I have a dataframe called Paper
on citations of papers for every year including their publication year as well as some meta stuff (journal, authors). It looks like:
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)
I want to calculate the sum of citations two years after publication and append this list to Paper
. However, I am not interested in every year, only those specified in a list Years
. My steps (code below) were the following: Order Paper
acc. Publication.Year
, select Publication.Year
and X-rows for first year (i.e. X2000
and X2001
for 1999), calculate sums, bind sums together, cbind to Paper
.
Is there (more) elegant way to do this?
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