简体   繁体   English

如何在字符向量之后命名列表并将原子向量添加到列表中

[英]How to name list after character vector and add atomic vectors to list

I have a four atomic vectors that I want to use to create a named list of atomic vectors where the names of the list are based on the first two, and the content of these list elements are the last two vectors repeated for each of the two first vectors.我有一个四个原子向量,我想用它来创建一个命名的原子向量列表,其中列表的名称基于前两个,这些列表元素的内容是最后两个重复的两个向量第一个向量。

# Naming vectors
v1 <- c("a", "b", "c")
v2 <- c("g", "h", "i")

# Content vectors
a <- 1:5
b <- LETTERS[1:5]

# I want to add each of the vectors a and b as repeated list elements 
# in list l and name the list element after vectors v1 and v2, respectively.


l <- list()

# Step 1: Adding vector 'a' to list
l <- lapply(v1, function(x) {
        append(l, a) %>% 
        unlist()
        })
# Step 2: Adding vector 'b' to list
l <- lapply(v2, function(x) {
        append(l, b) %>% 
        unlist()
        })

# Step 3: Naming vector        
names(l) <- c(v1, v2)
l

# The desired output (not working)
$a
  1 2 3 4 5
$b
  1 2 3 4 5
$c
  1 2 3 4 5 
$g
  'A' 'B' 'C' 'D' 'E'
$h
  'A' 'B' 'C' 'D' 'E'
$i
  'A' 'B' 'C' 'D' 'E'

The last sentence should have added names to the list, but it didn't work.最后一句应该在列表中添加了名称,但它不起作用。 Instead I get the error message:相反,我收到错误消息:

Error in names(l) <- c(v1, v2): 'names' attribute [6] must be the same length as the vector [3]

Troubleshooting故障排除

Commenting out step 2 and removing 'v2' from step 3, I get the desired output:注释掉第 2 步并从第 3 步中删除“v2”,我得到了所需的 output:

# Step 1: Adding vector 'a' to list
> l <- lapply(v1, function(x) {
        append(l, a) %>% 
        unlist()
        })
> names(l) <- v1
> l

$a
  1 2 3 4 5
$b
  1 2 3 4 5
$c
  1 2 3 4 5 

Commenting out step 3 and adding step 2 returns注释掉第 3 步并添加第 2 步返回

# Step 1: Adding vector 'a' to list
> l <- lapply(v1, function(x) {
        append(l, a) %>% 
        unlist()
        })
# Step 2: Adding vector 'b' to list
> l <- lapply(v2, function(x) {
        append(l, b) %>% 
        unlist()
        })
> l

1. '1''2''3''4''5''1''2''3''4''5''1''2''3''4''5''A''B''C''D''E'
2. '1''2''3''4''5''1''2''3''4''5''1''2''3''4''5''A''B''C''D''E'
3. '1''2''3''4''5''1''2''3''4''5''1''2''3''4''5''A''B''C''D''E'

For some reason, adding step 2 will append the output into the list elements created in step 1 instead of creating new ones, and the list elements in step 1 are repeated 3 times.由于某种原因,将步骤 2 中的 append 和 output 添加到步骤 1 中创建的列表元素中,而不是创建新的列表元素,并且将步骤 1 中的列表元素重复 3 次。

How can I append the second step's list elements names appropriately?如何正确地 append 第二步的列表元素名称?

Is this what you are looking for?这是你想要的?

# Naming vectors
v1 <- c("a", "b", "c")
v2 <- c("g", "h", "i")

# Content vectors
a <- 1:5
b <- LETTERS[1:5]

# Create lists of length v1/v2 and put in contents
v1_list <- lapply(v1, function(x) a)
v2_list <- lapply(v2, function(x) b)

# Combine the lists
l <- c(v1_list, v2_list)

#Rename
names(l) <- c(v1, v2)
l
#> $a
#> [1] 1 2 3 4 5
#> 
#> $b
#> [1] 1 2 3 4 5
#> 
#> $c
#> [1] 1 2 3 4 5
#> 
#> $g
#> [1] "A" "B" "C" "D" "E"
#> 
#> $h
#> [1] "A" "B" "C" "D" "E"
#> 
#> $i
#> [1] "A" "B" "C" "D" "E"

Created on 2022-01-05 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 1 月 5 日创建

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

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