r - Using backticks and operators in apply family functions -


i saw in recent answer apply family function assignments built-in , can't generalize it.

lst <- list(a=1, b=2:3) lst $a [1] 1  $b [1] 2 3 

this can't yet made data.frame because of unequal lengths. coercing max length list, works:

data.frame(lapply(lst, `length<-`, max(lengths(lst))))    b 1  1 2 2 na 3 

that works. i've never used arrow assignments in apply functions. tried understand generalizing like:

lapply(lst, function(x) length(x) <- max(lengths(lst))) $a [1] 2  $b [1] 2 

that's not correct output. nor is

lapply(lst, function(x) length(x) <- max(lengths(x)))  error in lengths(x) : 'x' must list  

this useful technique understand well. there way express assignment in anonymous function form?

by using anonymous functions, returning value of function, , not value of 'x'. have specify return(x) or x.

lapply(lst, function(x) {                   length(x) <- max(lengths(lst))                   x}) #$a #[1]  1 na  #$b #[1] 2 3 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -