Hi,
As the R beginner, I met a practice that make me confused.
I want to know if the elment in the same position in three dataframe(d1,d2,d3) has the relationship d1<d3<d2. if yes, we add the element,d1+d2. if no, we substract,d1-d2. And write the result in same position in d4.
For example, when rownumber=2,column number=1, this element in d3=3.4. 3(same position in d1) <3.4<4.0(same position in d2). So in d4(2nd row, 1st colnumn), we write 3+4=7.
While, I just complete 1 column. I don't know how to complete the rest. Maybe based on loop function or else? Hope that not combine the d1,d2,d3 into a new dataframe, and think when there are many columns(>3).
Thank you for help in advance.
d1<-data.frame(c1<-c(2,3,4),
c2<-c(3,8,6),
c3<-c(1,9,6))
colnames(d1)<-c("c1","c2","c3")
d2<-data.frame(c4<-c(1,4,5),
c5<-c(2,5.5,7),
c6<-c(3.1,6.3,2))
colnames(d2)<-c("c4","c5","c6")
d3<-data.frame(c7<-c(0.1,3.4,3),
c8<-c(3,8,6.9),
c9<-c(2.8,6.9,6))
colnames(d3)<-c("c7","c8","c9")
d4<-data.frame(matrix(nrow=3,ncol=3))
for(i in 1:nrow(d1)){
if(d3[i,1]<d2[i,1] & d3[i,1]>d1[i,1]){
d4[i,1]<-d1[i,1]+d2[i,1]
} else{
d4[i,1]<-d1[i,1]-d2[i,1]
}
}
Created on 2022-11-19 with reprex v2.0.2