0

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]
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
peace
  • 299
  • 2
  • 16
  • Incidentally, [it is neither necessary not recommended to use `return()`](https://stackoverflow.com/a/59090751/1968) here. I also strongly recommend not mixing `<-` and `=` for assignment. Decide on one ([they’re both identical](https://stackoverflow.com/a/51564252/1968)) and stick to it. Don’t mix. – Konrad Rudolph Feb 18 '22 at 10:42

1 Answers1

1

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]]
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214