I am new to R and trying to replace some of the loops with fuctions from the apply family. I still don’t understand how they work, but I managed to create working code:
#create some input data tech<-data.frame(cbind(c("p1","p2","p3","p4"),c(15,15,15,100),c(10,8,18,100))) colnames(tech)=c("id","capacity.el","capacity.th") tech$capacity.el<-as.numeric(tech$capacity.el) tech$capacity.th<-as.numeric(tech$capacity.th) heat<-data.frame(cbind(c(2,12,6,20,32,21,25,16,34,0),c(31,18,3,27,30,31,18,4,24,7),c(2,12,6,20,32,21,25,16,34,0),c(31,18,3,27,30,31,18,4,24,7))) colnames(heat)=c("p1","p2","p3","p4") > tech id capacity.el capacity.th 1 p1 2 1 2 p2 2 4 3 p3 2 3 4 p4 1 2 > heat p1 p2 p3 p4 1 2 31 2 31 2 12 18 12 18 3 6 3 6 3 4 20 27 20 27 5 32 30 32 30 6 21 31 21 31 7 25 18 25 18 8 16 4 16 4 9 34 24 34 24 10 0 7 0 7
The idea is to loop around the “heat” columns of data.frame and do some calculations using the values from the “heat” data frame. For this reason, I use the first sapply fuction to obtain the appropriate characteristics for each of the plants in the heat table from the technological table. Then the second sapply performs the calculations. The result "result" is exactly what I wanted.
Now I want to calculate more than the value from each row in "heat" (pel and epr). But I do not know how to extract these values from sapply fuctions. I tried the following with a list, but this retrieves the values as one large matrix with 20 rows. An ideal result would be like a list with two matrix or data.frame objects, each of which has 10 rows and 4 columns with pel / epr values.
result<-sapply(colnames(heat),function(x) { a<-tech$capacity.th[match(x,tech$id)] b<-tech$capacity.el[match(x,tech$id)] sapply(heat[,x],function(y) { pel<-a*y epr<-b*y }) new<-list(pel,epr) return(new) })
I would appreciate any help or comment.