I am trying to generate a data frame based on a user-defined function. My problem is that in the output only the first row is being filled. Here is an example of the function I am using:
df <- data.frame(cs=rep(c("T1","T2","T3","T4"),each=16),yr=rep(c(1:4), times = 4, each = 4))
sp.df <- data.frame(matrix(sample.int(100, size = 20*64,replace=T), ncol = 20, nrow = 64))
myfunc<-function(X, system, Title)
{
for(i in 1:4){
Col_T <- data.frame(matrix(NA, ncol = length(X), nrow = 4))
Col_T[i,] <- colSums(X[which(df$yr==i & df$cs==system),])
return (Col_T)}}
myfunc(X=sp.df, system="T1", Title="T1")
I would welcome any suggestion to resolve this issue. Thank you very much.