0

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:

# 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

# 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.

How can I append the second step's list elements names appropriately?

Pål Bjartan
  • 793
  • 1
  • 6
  • 18
  • What library are you using for the cluster function? – Desmond Jan 05 '22 at 01:27
  • 3
    This is not reproducible. Edit your code to have a reproducible example – Onyambu Jan 05 '22 at 01:58
  • 1
    https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example agree with Onyambu. It would also help if you showed an example of your desired output. – Brandon Bertelsen Jan 05 '22 at 02:48
  • 1
    Looks like you forgot the error message – camille Jan 05 '22 at 03:59
  • Apologies to all of you. I was writing up the post at 3 am in the morning. I had been working for hours on this problem and I was falling asleep while typing. Starting again today I realized what I had been writing was incoherent nonsense. I have edited, and changed the code a bit based on progress made today. The example and the added troubleshooting steps should be reproducible now. – Pål Bjartan Jan 05 '22 at 12:24
  • @Desmond Sorry for the confusion. "cluster" was just the name of my original list variable that had gotten mixed up in my code sample. – Pål Bjartan Jan 05 '22 at 12:29

1 Answers1

1

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)

Seb
  • 332
  • 1
  • 3