returning a list in R and functional programming behavior -


I have a basic question about functional programming in R.

A function that returns a list, such as:

  myF & lt; Why is it that the list is returned when calling a function with a range or vector? Returns (list (a = 11, b = x)}}   

Always have the same length for 'a':

  myF (1:10)   

returns:

  $ A [1] 11 $ b [1] 1 2 3 4 5 6 7 8 9 10   

How can one change behavior so that the sample length B in the 'A' list? Be in

I'm actually working with a group of S4 objects which I can not easily convert to the list (using as.list) therefore _apply is not my first choice.

Thanks for any info or help!

Edit (further explanation added) I'm not necessarily looking to make pads 'A' equal to its length B. However the solution padts 'A' with the same value for the first time using as.list (data.frame (a = myA, b = x)).

  myF & lt; - Function (x) {myA = roof (runif (1, max = 100)) Returns (as.list (data.frame (a = myA, b = x))} MF (1: 5) $ one [1 ] 79 79 79 79 79 79 79 79 79 79 $ B [1] 1 2 3 4 5 6 7 8 9 10   

I still can not believe why this happens!

Thanks

Do you just repeat the 11 so that the same length Be like B? If so:

  & gt; MyF & lt; - function (x) {+ return (list (A = representative (11, length (x)), B = x)) +}> Based on my F. (1:10) $ one [11] 11 11 11 11 11 11 11 11 11 $ $ b [1] 1 2 3 4 5 6 7 8 9 10   

Explanation of OP / Explanation If you want to be random vector with length equal to 'B' instead of 'a':

  & gt; MyF & lt; -function (x) {+ return (list (a = roof (runif (length (x), max = 100)), b = x)) +}> MyF (1:10) $ a [1] 4 31 8 45 25 74 36 95 64 32 $ b [1] 1 2 3 4 5 6 7 8 9 10    

Comments