1

I am writing a program that returns two variables in a list such as:

returnTwo <- function(){
    results <- c(a = A, b = B)
    return(results)
}

list[a, b] <- returnTwo()
x <- list[a]
y <- list[b]

I get:

[1] NA
attr(,"class")
[1] "result"

Any suggestions?

Ethaan
  • 11,291
  • 5
  • 35
  • 45
BenLar
  • 609
  • 1
  • 6
  • 7
  • Sorry, I don't understand the desired output. –  Apr 16 '15 at 02:54
  • 2
    This looks like an attempt at a Pythonic idiom in R. R does not support that sort of multiple assignment. Additionally, list[a, b] is just sort of syntactically nonsense, given what I think you intend. – joran Apr 16 '15 at 02:58
  • would it work if I just make it: result <- list(a = A, b = B)? My end result is I want to separate the two variables in the list after running the function – BenLar Apr 16 '15 at 02:59

1 Answers1

2

Really not sure if that's what you're after, but this would work:

returnTwo <- function(){
    results <- c(a = 2, b = 4)
    return(results)
}

x <- returnTwo()["a"]
y <- returnTwo()["b"]

Or using a list:

returnTwo <- function(){
    results <- list(a = 2, b = 4)
    return(results)
}

x <- returnTwo()$a
y <- returnTwo()$b
Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61