Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to assign the returned two values in a function

i have a function which return two values.

definePosition <- function(nodeList){
#  nodeList = node_names
  # unique name endings
  endings = unique(sub('.*_', '', nodeList))
  # define intervals
  steps = 1/length(endings)
  # x-values for each unique name ending
  # for input as node position
  nodes_x = {}
  xVal = 0
  for (e in endings) {
    nodes_x[e] = xVal
    xVal = xVal + steps

  }
  # x and y values in list form
  x_values <- 0
  y_values <- 0
  i =0
  for (n in nodeList) {
   last = sub('.*_', '', n)
    x_values[i] = nodes_x[last]
    y_values[i] = 0.1 * length(x_values)
    i = i + 1
 }
  
  return(list(x_values, y_values))
  
}

position = definePosition(node_names)

Now i want to assign x_values to node_x, and y_values to node_y by below code, but doesn’t work. what should be the correct assign?

node_x = position[0]
node_y = position[1]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since you are returning a list, you need to perform list extraction. Furthermore, indexing in R is 1-based:

node_x = position[[1L]]
node_y = position[[2L]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading