简体   繁体   中英

Function for same length of vectors in R

I want to make a function, which should remake list of vectors of different lengths to list of vectors with the same lengths. I made two functions, but the second one does not work well. My code is: first function (works well)

delka<-function(x){
  delky<<-NULL
  for(i in 1:length(x)){
    delky[i]<<-length(x[[i]])
  }
}

Here I globally made object "delky". Second function is

uprava<- function(x){
  stejne<<- NULL
  for(i in 1:length(x)){
    stejne[[i]]<<-vector(x[[i]], length(max(delky)))
  }
}    

Where I want to globally make an object "stejne" containing vectors with same lengths. But R answer me an issue

Error in vector(x[[i]], length(max(delky))) : invalid 'mode' argument

Do you have any ideas of what I am doing wrong?

Assuming you can work on whole lists at a time, and if you want to pad the shorter vectors with NAs, here's one way.

my <- list(a = runif(5),
           b = runif(11),
           c = runif(7))

maxl <- max(sapply(my, length))

sapply(my, FUN = function(x, ml) {
  difference <- ml - length(x)
  c(x, rep(NA, difference))
}, ml = maxl, simplify = FALSE)

$a
 [1] 0.91906470 0.68651070 0.07317576 0.52985130 0.27916889         NA         NA         NA         NA         NA         NA

$b
 [1] 0.86384953 0.79707167 0.88226627 0.91590091 0.03181455 0.86493584 0.89597354 0.80890065 0.92418156 0.72947596 0.13847751

$c
 [1] 0.2576621 0.6512487 0.5806530 0.8782730 0.0262019 0.1000885 0.5245472        NA        NA        NA        NA

in another representation

sapply(my, FUN = function(x, ml) {
  difference <- ml - length(x)
  c(x, rep(NA, difference))
}, ml = maxl)

               a          b         c
 [1,] 0.91906470 0.86384953 0.2576621
 [2,] 0.68651070 0.79707167 0.6512487
 [3,] 0.07317576 0.88226627 0.5806530
 [4,] 0.52985130 0.91590091 0.8782730
 [5,] 0.27916889 0.03181455 0.0262019
 [6,]         NA 0.86493584 0.1000885
 [7,]         NA 0.89597354 0.5245472
 [8,]         NA 0.80890065        NA
 [9,]         NA 0.92418156        NA
[10,]         NA 0.72947596        NA
[11,]         NA 0.13847751        NA

Assuming @RomanLuštrik is correct about what you are trying to do, you can do this much more directly using the following:

lapply(my, `length<-`, max(lengths(my)))
## $a
##  [1] 0.8669645 0.9224072 0.2003480 0.9476093 0.1095652        NA
##  [7]        NA        NA        NA        NA        NA
## 
## $b
##  [1] 0.6679763 0.2742245 0.7726615 0.4247057 0.7274648 0.8218540
##  [7] 0.4874759 0.4764729 0.3958279 0.1653358 0.2331573
## 
## $c
##  [1] 0.71882342 0.92852497 0.75134020 0.53098586 0.17515857
##  [6] 0.04997067 0.70350036         NA         NA         NA
## [11]         NA
## 

The lengths function was relatively recently introduced, so make sure you are running the most recent version of R.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM