如何将多个向量调用到 r 环境中以在另一个函数中使用它们
How to call several vectors into the r environment to use them in another function
我在 R 环境中有一系列不同长度的向量,例如:
> s_aleat_0
[1] "12M08393" "12M08427" "12M08412" "12M08441" "12M08436" "12M08444" "12M08443" "12M08387"
> s_aleat_1
[1] "12M08405" "12M08389" "12M08439" "12M08406" "12M08397" "12M08402"
> s_aleat_2
[1] "12M08422" "12M08441" "12M08410" "12M08391" "12M08396" "12M08420" "12M08409"
我想通过模式将它们调用到另一个函数中(rbind 他的字符串以将初始向量连接到更大的函数中),就像这样:
samples<-ls(pattern = "s_aleat_")
unique(c(rbind(samples)))
[1] "12M08393" "12M08405" "12M08422" "12M08427" "12M08389" "12M08441" "12M08412" "12M08439" "12M08410"
[10] "12M08406" "12M08391" "12M08436" "12M08397" "12M08396" "12M08444" "12M08402" "12M08420" "12M08443"
[19] "12M08409" "12M08387"
但是当我将 'samples' 放入 rbind 时它无法识别向量,如果不是使用 ls 函数获得的文本,如下所示:
c(rbind(samples))
[1] "s_aleat_0" "s_aleat_1" "s_aleat_2" "s_aleat_4" "s_aleat_5"
我该如何修复它?
您可以使用 mget
收集列表中的所有向量,并为长度较短的向量附加 NA
以创建一个矩阵,其中每个向量都是一列。
s_aleat_0 <- c("12M08393","12M08427","12M08412","12M08441","12M08436","12M08444","12M08443","12M08387")
s_aleat_1 <- c("12M08405", "12M08389", "12M08439", "12M08406", "12M08397" ,"12M08402")
s_aleat_2 <- c( "12M08422", "12M08441", "12M08410" ,"12M08391", "12M08396", "12M08420", "12M08409")
tmp <- mget(ls(pattern = "s_aleat_"))
result <- sapply(tmp, `[`, 1:max(lengths(tmp)))
# s_aleat_0 s_aleat_1 s_aleat_2
#[1,] "12M08393" "12M08405" "12M08422"
#[2,] "12M08427" "12M08389" "12M08441"
#[3,] "12M08412" "12M08439" "12M08410"
#[4,] "12M08441" "12M08406" "12M08391"
#[5,] "12M08436" "12M08397" "12M08396"
#[6,] "12M08444" "12M08402" "12M08420"
#[7,] "12M08443" NA "12M08409"
#[8,] "12M08387" NA NA
我们可以使用 stri_list2matrix
来自 stringi
library(stringi)
stri_list2matrix(tmp)
# [,1] [,2] [,3]
#[1,] "12M08393" "12M08405" "12M08422"
#[2,] "12M08427" "12M08389" "12M08441"
#[3,] "12M08412" "12M08439" "12M08410"
#[4,] "12M08441" "12M08406" "12M08391"
#[5,] "12M08436" "12M08397" "12M08396"
#[6,] "12M08444" "12M08402" "12M08420"
#[7,] "12M08443" NA "12M08409"
#[8,] "12M08387" NA NA
数据
s_aleat_0 <- c("12M08393","12M08427","12M08412","12M08441","12M08436","12M08444","12M08443","12M08387")
s_aleat_1 <- c("12M08405", "12M08389", "12M08439", "12M08406", "12M08397" ,"12M08402")
s_aleat_2 <- c( "12M08422", "12M08441", "12M08410" ,"12M08391", "12M08396", "12M08420", "12M08409")
tmp <- mget(ls(pattern = "s_aleat_"))
我在 R 环境中有一系列不同长度的向量,例如:
> s_aleat_0
[1] "12M08393" "12M08427" "12M08412" "12M08441" "12M08436" "12M08444" "12M08443" "12M08387"
> s_aleat_1
[1] "12M08405" "12M08389" "12M08439" "12M08406" "12M08397" "12M08402"
> s_aleat_2
[1] "12M08422" "12M08441" "12M08410" "12M08391" "12M08396" "12M08420" "12M08409"
我想通过模式将它们调用到另一个函数中(rbind 他的字符串以将初始向量连接到更大的函数中),就像这样:
samples<-ls(pattern = "s_aleat_")
unique(c(rbind(samples)))
[1] "12M08393" "12M08405" "12M08422" "12M08427" "12M08389" "12M08441" "12M08412" "12M08439" "12M08410"
[10] "12M08406" "12M08391" "12M08436" "12M08397" "12M08396" "12M08444" "12M08402" "12M08420" "12M08443"
[19] "12M08409" "12M08387"
但是当我将 'samples' 放入 rbind 时它无法识别向量,如果不是使用 ls 函数获得的文本,如下所示:
c(rbind(samples))
[1] "s_aleat_0" "s_aleat_1" "s_aleat_2" "s_aleat_4" "s_aleat_5"
我该如何修复它?
您可以使用 mget
收集列表中的所有向量,并为长度较短的向量附加 NA
以创建一个矩阵,其中每个向量都是一列。
s_aleat_0 <- c("12M08393","12M08427","12M08412","12M08441","12M08436","12M08444","12M08443","12M08387")
s_aleat_1 <- c("12M08405", "12M08389", "12M08439", "12M08406", "12M08397" ,"12M08402")
s_aleat_2 <- c( "12M08422", "12M08441", "12M08410" ,"12M08391", "12M08396", "12M08420", "12M08409")
tmp <- mget(ls(pattern = "s_aleat_"))
result <- sapply(tmp, `[`, 1:max(lengths(tmp)))
# s_aleat_0 s_aleat_1 s_aleat_2
#[1,] "12M08393" "12M08405" "12M08422"
#[2,] "12M08427" "12M08389" "12M08441"
#[3,] "12M08412" "12M08439" "12M08410"
#[4,] "12M08441" "12M08406" "12M08391"
#[5,] "12M08436" "12M08397" "12M08396"
#[6,] "12M08444" "12M08402" "12M08420"
#[7,] "12M08443" NA "12M08409"
#[8,] "12M08387" NA NA
我们可以使用 stri_list2matrix
来自 stringi
library(stringi)
stri_list2matrix(tmp)
# [,1] [,2] [,3]
#[1,] "12M08393" "12M08405" "12M08422"
#[2,] "12M08427" "12M08389" "12M08441"
#[3,] "12M08412" "12M08439" "12M08410"
#[4,] "12M08441" "12M08406" "12M08391"
#[5,] "12M08436" "12M08397" "12M08396"
#[6,] "12M08444" "12M08402" "12M08420"
#[7,] "12M08443" NA "12M08409"
#[8,] "12M08387" NA NA
数据
s_aleat_0 <- c("12M08393","12M08427","12M08412","12M08441","12M08436","12M08444","12M08443","12M08387")
s_aleat_1 <- c("12M08405", "12M08389", "12M08439", "12M08406", "12M08397" ,"12M08402")
s_aleat_2 <- c( "12M08422", "12M08441", "12M08410" ,"12M08391", "12M08396", "12M08420", "12M08409")
tmp <- mget(ls(pattern = "s_aleat_"))