Search
 
SCRIPT & CODE EXAMPLE
 

R

cbind vectors of different lengths r

x <- 1:2
y <- 1:10
n <- max(length(x), length(y))
length(x) <- n                      
length(y) <- n
Comment

cbind vectors of different lengths r

bind.pad <- function(l, side="r", len=max(sapply(l,length)))
{
  if (side %in% c("b", "r")) {
    out <- sapply(l, 'length<-', value=len)
  } else {
    out <- sapply(sapply(sapply(l, rev), 'length<-', value=len, simplify=F), rev)}
  if (side %in% c("r", "l")) out <- t(out)
  out
}
Comment

cbind vectors of different lengths r

> l <- lapply(c(3,2,1,2,3),seq)
> lapply(c("t","l","b","r"), bind.pad, l=l, len=4)
[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]   NA   NA   NA   NA   NA
[2,]    1   NA   NA   NA    1
[3,]    2    1   NA    1    2
[4,]    3    2    1    2    3

[[2]]
     [,1] [,2] [,3] [,4]
[1,]   NA    1    2    3
[2,]   NA   NA    1    2
[3,]   NA   NA   NA    1
[4,]   NA   NA    1    2
[5,]   NA    1    2    3

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    2    2   NA    2    2
[3,]    3   NA   NA   NA    3
[4,]   NA   NA   NA   NA   NA

[[4]]
     [,1] [,2] [,3] [,4]
[1,]    1    2    3   NA
[2,]    1    2   NA   NA
[3,]    1   NA   NA   NA
[4,]    1    2   NA   NA
[5,]    1    2    3   NA
Comment

PREVIOUS NEXT
Code Example
R :: r read.csv tab delimited 
R :: pairwise combinations r 
R :: distance matrix in r 
R :: switch variable position in r dplyr 
R :: required in r 
R :: gsub function r 
R :: two letter country code in r 
R :: store list in data.frame R 
R :: how to count the number of non NA values in R 
R :: geom_abline vertical line 
R :: rename vector in r 
R :: concatenate r 
Rust :: rust get current directory 
Rust :: how to cahce clean cargo 
Rust :: rust timestamp 
Rust :: add element to vec rust 
Rust :: actix web hello world 
Rust :: new rust project with cargo 
Rust :: rust•armanriazi•borrowchecker•lifetime 
Rust :: armanriazi•rust•syntax•names 
Rust :: get function name rust 
Rust :: armanriazi•rust•oop 
Rust :: allow dead code 
Rust :: armanriazi•rust•ref•move 
Rust :: primitive data types in rust 
Lua :: how to save to a file lua 
Lua :: Lua array add item 
Lua :: forever loop in lua 
Lua :: roblox how to get character from player 
Lua :: for loop roblox 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =