peace
peace

Reputation: 389

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]

Upvotes: 0

Views: 34

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 546083

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]]

Upvotes: 1

Related Questions