user2927720
user2927720

Reputation: 1

using rbind to create dataframe is not working

I am trying to write a script to get some specific values for the equation 25a+20b=1600 with a in the range between 24:60 and b in 20:50

I need to get the pairs of a and b satisfying the equation.

My first problem was how to define a and b with a single digit decimal place (a=24.0,24.1,24.2...etc.) but I overcame that defining a<-c(240:600)/10, so my first question is: Is there any direct method to do that?

Now, I did a couple of nested loops and I am able to get each time the equation is satisfied in a vector, I want to use rbind() to attach this vector to a matrix or a dataframe but it is not working without any error or warning. it just takes the value of the first vector and that's it !

Here is my code, can someone help me define where the problem is?

solve_ms <- function() {  
  index<-1  
  sol<-data.frame()  
  temp<-vector("numeric")  
  a<-c(240:600)/10  
  b<-c(200:500)/10  

 for (i in 1:length(a)){   
    for (j in 1:length(b)) {   
      c <- 25*a[i]+20*b[j]  
      if(c == 1600) {  
        temp<-c(a[i], b[j]) 
        if(index == 1) {  
          sol<-temp 
          index<-0  
        }  
        else   rbind(sol,temp)  
      }  
     }  
   }  

 return(sol)  
}

I found our where my code problem is, it is using rbind without assigning its return to a dataframe. I had to do {sol<-rbind(sol,temp)} and it will work. I will check other suggestions as well.. thanks.

Upvotes: 0

Views: 1126

Answers (2)

Roland
Roland

Reputation: 132864

Try this instead:

#define a function
fun <- function(a,b) (25*a+20*b) == 1600

Since floating point precision could be an issue:

#alternative function
fun <- function(a,b,tol=.Machine$double.eps ^ 0.5) abs(25*a+20*b-1600) < tol

#create all possible combinations
paras <- expand.grid(a=c(240:600)/10, b=20:50)
paras[fun(paras$a,paras$b),]

         a  b
241   48.0 20
594   47.2 21
947   46.4 22
1300  45.6 23
1653  44.8 24
2006  44.0 25
2359  43.2 26
2712  42.4 27
3065  41.6 28
3418  40.8 29
3771  40.0 30
4124  39.2 31
4477  38.4 32
4830  37.6 33
5183  36.8 34
5536  36.0 35
5889  35.2 36
6242  34.4 37
6595  33.6 38
6948  32.8 39
7301  32.0 40
7654  31.2 41
8007  30.4 42
8360  29.6 43
8713  28.8 44
9066  28.0 45
9419  27.2 46
9772  26.4 47
10125 25.6 48
10478 24.8 49
10831 24.0 50

Upvotes: 2

CHP
CHP

Reputation: 17189

If the problem is really this simple i.e. solving for roots of 2 variable linear equation, you can always rearrange the equation to write b in terms of a i.e. b = (1600-25*a)/20 and get all the values of b for corresponding values of a and filter the combinations by b

e.g.

a = c(240:600)/10
b = 20:50

RESULTS <- data.frame(a, b = (1600 - 25 * a)/20)[((1600 - 25 * a)/20) %in% b, ]
RESULTS
##        a  b
## 1   24.0 50
## 9   24.8 49
## 17  25.6 48
## 25  26.4 47
## 33  27.2 46
## 41  28.0 45
## 49  28.8 44
## 57  29.6 43
## 65  30.4 42
## 73  31.2 41
## 81  32.0 40
## 97  33.6 38
## 105 34.4 37
## 121 36.0 35
## 137 37.6 33
## 145 38.4 32
## 161 40.0 30
## 177 41.6 28
## 185 42.4 27
## 193 43.2 26
## 201 44.0 25
## 209 44.8 24
## 217 45.6 23
## 225 46.4 22
## 233 47.2 21
## 241 48.0 20

Upvotes: 1

Related Questions