简体   繁体   English

R中相同长度向量的函数

[英]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". 在这里,我将对象全局设置为“ 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. 我想在哪里全局制作一个对象“ stejne”,其中包含相同长度的向量。 But R answer me an issue 但是R回答我一个问题

Error in vector(x[[i]], length(max(delky))) : invalid 'mode' argument 向量错误(x [[i]],length(max(delky))):无效的'mode'参数

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. 假设您一次可以处理整个列表,并且如果要用NA填充较短的向量,这是一种方法。

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: 假设@RomanLuštrik对您要执行的操作是正确的,则可以使用以下命令直接进行以下操作:

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. lengths函数是最近才引入的,因此请确保您正在运行R的最新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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