I started using R to solve a LP-Problem using the simplex funciton from library("boot"). When i try the following code I only get the target vector as string but not as a vector.
library("boot")
# This example is taken from Exercise 7.5 of Gill, Murray and Wright (1991).
enj <- c(200, 6000, 3000, -200)
fat <- c(800, 6000, 1000, 400)
vitx <- c(50, 3, 150, 100)
vity <- c(10, 10, 75, 100)
vitz <- c(150, 35, 75, 5)
j<-simplex(a = enj, A1 = fat, b1 = 13800, A2 = rbind(vitx, vity, vitz),
b2 = c(600, 300, 550), maxi = TRUE)
xx<-(j["soln"])
print(xx)
From this get the folling output
$soln
x1 x2 x3 x4
0.0 0.0 13.8 0.0
However, this is not a vector object so i can't get the Element of the first Dimension by using x[1]
. Can you please help me to get the result as a vector?
Your variable xx
is a list so it's quite easy to return a specific value within it.
For example, if you want x1, you can do :
xx[["soln"]]["x1"]
or xx[[1]][1]
But if you want to transform xx
into a vector, you can do :
xx2 <- unlist(xx)
and then you have a vector :
> xx2
soln.x1 soln.x2 soln.x3 soln.x4
0.0 0.0 13.8 0.0
> is.vector(xx2)
[1] TRUE
If you don't want to keep the names for each element in the vector, put the arg use.names
to FALSE
:
> xx2 <- unlist(xx, use.names = FALSE)
> xx2
[1] 0.0 0.0 13.8 0.0
> is.vector(xx2)
[1] TRUE