Substitution Encryption/Decryption in R - r

Using the variables alpha and key, encrypt ptext into a variable named ctext. Using substitution cipher
So I have a text file separated in a vector
ptext <- strsplit(ptext,split = "", fixed = TRUE)
ptext <- unlist(ptext)
I also created a key for this cipher
key <- "ZGYHXIWJVKULTMSARBQCPDOENF"
key <- unlist(strsplit(key,""))
and an Alphabet vector for the key
alpha <- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
alpha <= toupper(alpha)
alpha <- unlist(strsplit(alpha,""))
Now my goal is to try to replace all the character in the ptext vector corresponding to the letters in the key in relation to alpha (Example: A in alpha in relation to Z in the key. So all A's in the text would be replaced by a Z)
I know I am supposed to match the alpha in key
cipher <- match(key,alpha)
Now my issue is, the ptext file is over 1000 characters in it. How would I be able to replace all the letters in that vector?

You could use chartr which will avoid splitting the string and pasting back.
ptext <- 'REQWDSFFFSLK'
alpha <- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
key <- 'ZGYHXIWJVKULTMSARBQCPDOENF'
chartr(alpha, key, ptext)
#[1] "BXROHQIIIQLU"
Here, all R is replaced with B, E with X and so on for every character value.

Basically, you need to do what you are doing with the cipher but apply that to each letter of ptext. You can either leave as a vector of single strings or put back together as desired, for example using paste0 below.
set.seed(123)
ptext <- strsplit(LETTERS[sample(26, 100, T)],split = "", fixed = TRUE)
ptext <- unlist(ptext)
key <- "ZGYHXIWJVKULTMSARBQCPDOENF"
key <- unlist(strsplit(key,""))
alpha <- unlist(strsplit(LETTERS,""))
encoded <- sapply(ptext, function(x) key[match(x, alpha)])
encoded
#> O S N C J R V K E T N V Y Z E S Y Y I C
#> "S" "Q" "M" "Y" "K" "B" "D" "U" "X" "C" "M" "D" "N" "F" "X" "Q" "N" "N" "V" "Y"
#> H Z G J I S D N Q K G U L O J M G I I J
#> "J" "F" "W" "K" "V" "Q" "H" "M" "R" "U" "W" "P" "L" "S" "K" "T" "W" "V" "V" "K"
#> W U G U F Y B E H L M R A Y Y F U O I O
#> "O" "P" "W" "P" "I" "N" "G" "X" "J" "L" "T" "B" "Z" "N" "N" "I" "P" "S" "V" "S"
#> Z P T F K H V V G P Q V R Q B D M E V S
#> "F" "A" "C" "I" "U" "J" "D" "D" "W" "A" "R" "D" "B" "R" "G" "H" "T" "X" "D" "Q"
#> Y T V Y N Y W C H P L Y N C N G C W V Z
#> "N" "C" "D" "N" "M" "N" "O" "Y" "J" "A" "L" "N" "M" "Y" "M" "W" "Y" "O" "D" "F"
paste0(encoded, collapse = "")
#> [1] "SQMYKBDUXCMDNFXQNNVYJFWKVQHMRUWPLSKTWVVKOPWPINGXJLTBZNNIPSVSFACIUJDDWARDBRGHTXDQNCDNMNOYJALNMYMWYODF"

Related

improve efficiency of filling dataset with sampling

Given myletters:
library(tidyverse)
myletters <- letters
myletters
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
I want to sample 4 letters at a time (without replacement) from myletters, repeat this X multiple times and find the probability of having sampled all letters at least once in X = 1:100 draws.
For example if X = 10 we could get:
set.seed(10)
X <- unlist(rerun(10, sample(myletters, 4, replace = F)))
X
# [1] "k" "i" "j" "p" "l" "w" "h" "v" "g" "s" "x" "o" "o" "j" "g" "y" "b" "x" "m" "h" "n" "g" "f" "y" "v" "r" "u" "y" "m" "e" "a" "g" "z" "r" "d" "y" "x" "s" "v"
# [40] "r"
#test if X contains all 26 letters
n_distinct(X) == 26 #26 = no of letters
#FALSE
The following approach does what I want in a simulation but doesn't scale very well as it fills a dataframe column with up to 400 letters in a cell so is awkward and inefficient:
output <- crossing(drawsX = 1:100,
trial = 1:100) %>%
mutate(draws_output = map(drawsX, ~ unlist(rerun(., sample(myletters, 4, replace = F)))),
all_letters = map_lgl(draws_output, ~ n_distinct(.) == 26))
output
#plot
output %>%
group_by(drawsX) %>%
summarise(prob_of_all_letters = mean(all_letters)) %>%
ggplot(., aes(drawsX, prob_of_all_letters)) +
geom_line() +
scale_y_continuous(labels = scales::percent_format()) +
labs(y = "Probability")
Ideally I would like to simulate more times e.g. trial = 1:100000 but the approach above is inefficient if I wanted to do this.
1) Is there a more efficient way to fill my dataset (or using a matrix) with samples?
2) Also, is there an analytic way to solve this problem in R instead of simulation. e.g. what is probability of get 26 letters from 10 draws of 4 samples each?
thanks
Here's a somewhat improved version. The code is a bit more efficient and certainly cleaner:
sample_sets = function(replicates, k, set = letters) {
draws = vapply(1:replicates, function(z, ...) sample.int(...), FUN.VALUE = integer(k), n = length(set), size = k, replace = FALSE)
all(seq_along(set) %in% draws)
}
## example use
output <- crossing(
drawsX = 1:100,
trial = 1:100
) %>%
mutate(
outcome = map_lgl(drawsX, sample_sets, set = letters, k = 4),
)
## timing
system.time({output <- crossing(
drawsX = 1:100,
trial = 1:100
) %>%
mutate(
outcome = map_lgl(drawsX, sample_sets, set = letters, k = 4),
)
})
# user system elapsed
# 2.79 0.04 2.95
## original way
system.time({output <- crossing(drawsX = 1:100,
trial = 1:100) %>%
mutate(draws_output = map(drawsX, ~ unlist(rerun(., sample(letters, 4, replace = F)))),
all_letters = map_lgl(draws_output, ~ n_distinct(.) == 26))})
# user system elapsed
# 4.96 0.06 5.18
So it's about 40% faster on this data - hopefully that performance gain will continue as draws increases.

Extract vectors from nested loops in R as vectors

I'm learning to use R and I'm trying to extract cont, p0 and pf variables from nested loop as 3 different vectors from this code.
v<-c("a","b","c","d","e","f","g","h")
n<-length(v)
mv<-5
a<-n-(mv-1)
cont<-0
for (num in (a-1):0){
for (i in 0:num){
cont<-cont+1
p0<-v[a-num]
pf<-v[n-i]
}
}
The expected result should be:
> print(cont)
[1] 1 2 3 4 5 6 7 8 9 10
> print (p0)
[1] "a" "a" "a" "a" "b" "b" "b" "c" "c" "d"
> print (pf)
[1] "h" "g" "f" "e" "h" "g" "f" "h" "g" "h"
I would keep cont as an index variable and store the other variables in vectors.
v<-c("a","b","c","d","e","f","g","h")
n<-length(v)
mv<-5
a<-n-(mv-1)
cont = 0
cont_stored = vector();
p0 = vector();
pf = vector();
for (num in (a-1):0){
for (i in 0:num){
cont <- cont+1
cat("cont = ", cont, "\n"); ## useful function for printing stuff out in loops
cont_stored[cont] = cont;
p0[cont] = v[a-num]
pf[cont] = v[n-i]
}
}
cont_stored
p0
pf
You can do this without explicit for loop :
v <- c("a","b","c","d","e","f","g","h")
n <- length(v)
mv <- 5
a <- n-(mv-1)
cont <- 0
p0 <- rep(v[1:a], a:1)
pf <- v[unlist(sapply((n-a + 1):n, function(x) n:x))]
p0
# [1] "a" "a" "a" "a" "b" "b" "b" "c" "c" "d"
pf
# [1] "h" "g" "f" "e" "h" "g" "f" "h" "g" "h"
If you need cont you could use p0 or pf with seq_along.
cont <- seq_along(p0)
cont
#[1] 1 2 3 4 5 6 7 8 9 10

Sampling a vector by equal split

If I have a vector of strings of any length say;
vec <- c("a","b","c","d","e","f","g","h","i")
I am looking for a function to sample n number of values from this vector using a strategy shown below. Showing figures since it's hard to explain.
function call:
result:
schematic diagram:
fn(vector=vec,n=1)
"a"
|
a b c d e f g h i
fn(vector=vec,n=2)
"a" "i"
_______________
| |
a b c d e f g h i
fn(vector=vec,n=3)
"a" "e" "i"
_______________
| | |
a b c d e f g h i
fn(vector=vec,n=4)
"a" "c" "g" "i"
_______________
| | | |
a b c d e f g h i
fn(vector=vec,n=5)
"a" "c" "e" "g" "i"
_______________
| | | | |
a b c d e f g h i
The sampling does not have to be accurate. The values can be from roughly the correct region but must be consistent. The string vector can be even or odd.
One way would be to use seq(), taking advantage of it's length.out= argument to get the evenly spaced indices that you seek:
fn <- function(x, n) {
x[round(seq(1,length(x), length.out=n))]
}
## Check that it works
fn(vec, 1)
# [1] "a"
fn(vec, 2)
# [1] "a" "i"
fn(vec, 4)
# [1] "a" "d" "f" "i"
fn(vec, 8)
# [1] "a" "b" "c" "d" "f" "g" "h" "i"
This should do what you are looking for:
fn <- function(myVector, n) {
# check for valid n
if(n > length(myVector)) stop("invalid n")
if(n == 1) return(myVector[1])
if(n == 2) return(myVector[c(1, length(myVector))])
middleSpots <- ceiling(length(vec) * (1:(n-2) / (n-1)))
return(myVector[c(1, middleSpots, length(myVector))])
}

Error in x[-1L] != x[-n] : comparison of these types is not implemented

I am stuck in finding the source of the problem. According to traceback I guess the problem is with four.in.a.row function even though I have tested it.
four.in.a.row = function(player, v, debug=FALSE) {
if (debug) {
cat(sep="", "four.in.a.row(player=", player, ", v=", v, ")\n")
}
with(rle(v), any(lengths== 4 & values == player))
}
# Returns TRUE if (matrix) board (of character strings)
# contains at least four in a row of (string) player, who
# just played in position (r, c). (Here "r" means "row" and
# "c" means "column").
#
# Hint: this function should call four.in.a.row() four times.
won = function(player, board, r, c, debug=FALSE) {
if (debug) {
cat(sep="", "won(player=", player, ", board=\n")
print(board)
cat(sep="", ", r=", r, ", c=", c, ")\n")
}
row_w=board[r,]
cat("row is = ", row_w, "\n")
col_w=board[,c]
cat("col is = ", col_w, "\n")
reverse_diag_w=board[row(board) + col(board) == r + c]
cat("reverse diag is = ", reverse_diag_w, "\n")
diag_w=x[row(board) - col(board) == r - c]
cat("diag is = ", diag_w, "\n")
four.in.a.row(player,row_w,debug=FALSE)
four.in.a.row(player,col_w,debug=FALSE)
four.in.a.row(player,diag_w,debug=FALSE)
four.in.a.row(player,reverse_diag_w,debug=FALSE)
return(FALSE) # correct this return() statement
}
x = matrix(data=c(
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","X",
"X","X","X","X","O","E","X"
), nrow=6, ncol=7, byrow=TRUE)
stopifnot( won(player="X", board=x, r=6, c=1, debug=TRUE))
stopifnot(!won(player="O", board=x, r=6, c=1, debug=TRUE))
stopifnot(!won(player="X", board=x, r=1, c=7, debug=TRUE))
stopifnot( won(player="O", board=x, r=1, c=7, debug=TRUE))
Here's the run information:
> source("rle.R")
won(player=X, board=
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E" "E" "E" "E" "E" "E" "O"
[2,] "E" "E" "E" "E" "E" "E" "O"
[3,] "E" "E" "E" "E" "E" "E" "O"
[4,] "E" "E" "E" "E" "E" "E" "O"
[5,] "E" "E" "E" "E" "E" "E" "X"
[6,] "X" "X" "X" "X" "O" "E" "X"
, r=6, c=1)
row is = X X X X O E X
col is = E E E E E X
reverse diag is = X E E E E E
diag is = X
Error: won(player = "X", board = x, r = 6, c = 1, debug = TRUE) is not TRUE
My main problem is when I run the four.in.a.row function separately it returns TRUE as in the following:
> x = matrix(data=c(
+ "E","E","E","E","E","E","O",
+ "E","E","E","E","E","E","O",
+ "E","E","E","E","E","E","O",
+ "E","E","E","E","E","E","O",
+ "E","E","E","E","E","E","X",
+ "X","X","X","X","O","E","X"
+ ), nrow=6, ncol=7, byrow=TRUE)
> row_x=x[6,]
> row_x
[1] "X" "X" "X" "X" "O" "E" "X"
> four.in.a.row("X",row_x,debug=FALSE)
[1] TRUE
After changing the code to what #Flodel told me I have:
four.in.a.row = function(player, v, debug=FALSE) {
if (debug) {
cat(sep="", "four.in.a.row(player=", player, ", v=", v, ")\n")
}
with(rle(v), any(lengths== 4 & values == player))
}
# Returns TRUE if (matrix) board (of character strings)
# contains at least four in a row of (string) player, who
# just played in position (r, c). (Here "r" means "row" and
# "c" means "column").
#
# Hint: this function should call four.in.a.row() four times.
won = function(player, board, r, c, debug=FALSE) {
if (debug) {
cat(sep="", "won(player=", player, ", board=\n")
print(board)
cat(sep="", ", r=", r, ", c=", c, ")\n")
}
row_w=board[r,]
cat("row is = ", row_w, "\n")
col_w=board[,c]
cat("col is = ", col_w, "\n")
reverse_diag_w=board[row(board) + col(board) == r + c]
cat("reverse diag is = ", reverse_diag_w, "\n")
diag_w=board[row(board) - col(board) == r - c]
cat("diag is = ", diag_w, "\n")
#four.in.a.row(player,row_w,debug=FALSE)
#four.in.a.row(player,col_w,debug=FALSE)
#four.in.a.row(player,diag_w,debug=FALSE)
#four.in.a.row(player,reverse_diag_w,debug=FALSE)
#return(FALSE) # correct this return() statement
return(four.in.a.row(player,row_w,debug=FALSE) ||
four.in.a.row(player,col_w,debug=FALSE) ||
four.in.a.row(player,diag_w,debug=FALSE) ||
four.in.a.row(player,reverse_diag_w,debug=FALSE))
}
x = matrix(data=c(
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","X",
"X","X","X","X","O","E","X"
), nrow=6, ncol=7, byrow=TRUE)
stopifnot( won(player="X", board=x, r=6, c=1, debug=TRUE))
stopifnot(!won(player="O", board=x, r=6, c=1, debug=TRUE))
stopifnot(!won(player="X", board=x, r=1, c=7, debug=TRUE))
stopifnot( won(player="O", board=x, r=1, c=7, debug=TRUE))
And here's the error I receive:
> source("rle.R")
won(player=X, board=
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E" "E" "E" "E" "E" "E" "O"
[2,] "E" "E" "E" "E" "E" "E" "O"
[3,] "E" "E" "E" "E" "E" "E" "O"
[4,] "E" "E" "E" "E" "E" "E" "O"
[5,] "E" "E" "E" "E" "E" "E" "X"
[6,] "X" "X" "X" "X" "O" "E" "X"
, r=6, c=1)
row is = X X X X O E X
col is = E E E E E X
reverse diag is = X E E E E E
diag is = X
Show Traceback
Rerun with Debug
Error in x[-1L] != x[-n] : comparison of these types is not implemented
Here's the result of the traceback:
Error in x[-1L] != x[-n] : comparison of these types is not implemented
9 rle(v)
8 with(rle(v), any(lengths == 4 & values == player)) at rle.R#5
7 four.in.a.row(player, diag, debug = FALSE) at rle.R#32
6 won(player = "X", board = x, r = 6, c = 1, debug = TRUE)
5 stopifnot(won(player = "X", board = x, r = 6, c = 1, debug = TRUE)) at rle.R#46
4 eval(expr, envir, enclos)
3 eval(ei, envir)
2 withVisible(eval(ei, envir))
1 source("rle.R")
Here's the answer thanks to Flodel's guide:
four.in.a.row = function(player, v, debug=FALSE) {
if (debug) {
cat(sep="", "four.in.a.row(player=", player, ", v=", v, ")\n")
}
with(rle(v), any(lengths== 4 & values == player))
}
# Returns TRUE if (matrix) board (of character strings)
# contains at least four in a row of (string) player, who
# just played in position (r, c). (Here "r" means "row" and
# "c" means "column").
#
# Hint: this function should call four.in.a.row() four times.
won = function(player, board, r, c, debug=FALSE) {
if (debug) {
cat(sep="", "won(player=", player, ", board=\n")
print(board)
cat(sep="", ", r=", r, ", c=", c, ")\n")
}
row_w=board[r,]
cat("row is = ", row_w, "\n")
col_w=board[,c]
cat("col is = ", col_w, "\n")
reverse_diag_w=board[row(board) + col(board) == r + c]
cat("reverse diag is = ", reverse_diag_w, "\n")
diag_w=x[row(board) - col(board) == r - c]
cat("diag is = ", diag_w, "\n")
#four.in.a.row(player,row_w,debug=FALSE)
#four.in.a.row(player,col_w,debug=FALSE)
#four.in.a.row(player,diag_w,debug=FALSE)
#four.in.a.row(player,reverse_diag_w,debug=FALSE)
#return(FALSE) # correct this return() statement
return(four.in.a.row(player,row_w,debug=debug) ||
four.in.a.row(player,col_w,debug=debug) ||
four.in.a.row(player,diag_w,debug=debug) ||
four.in.a.row(player,reverse_diag_w,debug=debug))
}
x = matrix(data=c(
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","O",
"E","E","E","E","E","E","X",
"X","X","X","X","O","E","X"
), nrow=6, ncol=7, byrow=TRUE)
stopifnot( won(player="X", board=x, r=6, c=1, debug=TRUE))
stopifnot(!won(player="O", board=x, r=6, c=1, debug=TRUE))
stopifnot(!won(player="X", board=x, r=1, c=7, debug=TRUE))
stopifnot( won(player="O", board=x, r=1, c=7, debug=TRUE))
Here's what I get :
> source("rle.R")
won(player=X, board=
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E" "E" "E" "E" "E" "E" "O"
[2,] "E" "E" "E" "E" "E" "E" "O"
[3,] "E" "E" "E" "E" "E" "E" "O"
[4,] "E" "E" "E" "E" "E" "E" "O"
[5,] "E" "E" "E" "E" "E" "E" "X"
[6,] "X" "X" "X" "X" "O" "E" "X"
, r=6, c=1)
row is = X X X X O E X
col is = E E E E E X
reverse diag is = X E E E E E
diag is = X
four.in.a.row(player=X, v=XXXXOEX)
won(player=O, board=
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E" "E" "E" "E" "E" "E" "O"
[2,] "E" "E" "E" "E" "E" "E" "O"
[3,] "E" "E" "E" "E" "E" "E" "O"
[4,] "E" "E" "E" "E" "E" "E" "O"
[5,] "E" "E" "E" "E" "E" "E" "X"
[6,] "X" "X" "X" "X" "O" "E" "X"
, r=6, c=1)
row is = X X X X O E X
col is = E E E E E X
reverse diag is = X E E E E E
diag is = X
four.in.a.row(player=O, v=XXXXOEX)
four.in.a.row(player=O, v=EEEEEX)
four.in.a.row(player=O, v=X)
four.in.a.row(player=O, v=XEEEEE)
won(player=X, board=
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E" "E" "E" "E" "E" "E" "O"
[2,] "E" "E" "E" "E" "E" "E" "O"
[3,] "E" "E" "E" "E" "E" "E" "O"
[4,] "E" "E" "E" "E" "E" "E" "O"
[5,] "E" "E" "E" "E" "E" "E" "X"
[6,] "X" "X" "X" "X" "O" "E" "X"
, r=1, c=7)
row is = E E E E E E O
col is = O O O O X X
reverse diag is = X E E E E O
diag is = O
four.in.a.row(player=X, v=EEEEEEO)
four.in.a.row(player=X, v=OOOOXX)
four.in.a.row(player=X, v=O)
four.in.a.row(player=X, v=XEEEEO)
won(player=O, board=
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] "E" "E" "E" "E" "E" "E" "O"
[2,] "E" "E" "E" "E" "E" "E" "O"
[3,] "E" "E" "E" "E" "E" "E" "O"
[4,] "E" "E" "E" "E" "E" "E" "O"
[5,] "E" "E" "E" "E" "E" "E" "X"
[6,] "X" "X" "X" "X" "O" "E" "X"
, r=1, c=7)
row is = E E E E E E O
col is = O O O O X X
reverse diag is = X E E E E O
diag is = O
four.in.a.row(player=O, v=EEEEEEO)
four.in.a.row(player=O, v=OOOOXX)
I was mostly worried that rle won't work when I give a vector less that length 4 to it and then compare it to see if it contains a vector of length 4 of X or Os . Please let me know if the answer is not correct.

Define names of objects in multiple lists within a list (using lapply?)

My apologies for the somewhat confusing title (any suggestion for improvement are welcome)..
Suppose I have a list which contains several (e.g. four) lists in which I would like to store 20 objects later on:
mylist <- vector(mode="list",length=4)
names(mylist) <- c("One","Two","Three","Four")
mylist$One <- mylist$Two <- mylist$Three <- mylist$Four <- vector(mode="list",
length=20)
I would like to define the names of those objects beforehand. Of course, I can do that as following:
names(mylist$One) <- c("A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T")
names(mylist$Two) <- names(mylist$Three) <- names(mylist$Four) <- names(mylist$One)
But if the number of the lists would increase (as is the case in my actual data), this becomes rather cumbersome, so I was trying to do this with a function such as lapply :
mylist <- lapply(mylist,FUN=function(x) {names(x) <-
c("A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T")})
This, however, does not give me the same result, but I can not seem to figure out what I am overlooking here. Any suggestions?
Thanks!
You need to return a value in your lapply call:
mylist <- lapply(mylist,FUN=function(x) {names(x) <-
c("A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T")
x ## <- note the x here; you could also use return(x)
})
mylist
# $One
# A B C D E F G H I J K L M N O P Q R S T
# "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
#
# $Two
# A B C D E F G H I J K L M N O P Q R S T
# "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
#
# $Three
# A B C D E F G H I J K L M N O P Q R S T
# "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
#
# $Four
# A B C D E F G H I J K L M N O P Q R S T
# "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
This is my implementation, which I think it produces the results you are expecting
mylist <- vector(mode="list",length=4)
names(mylist) <- c("One","Two","Three","Four")
mylist$One <- mylist$Two <- mylist$Three <- mylist$Four <- vector(mode="list",length=20)
renameList <- function(mylist,k){
names(mylist) <- LETTERS[1:k]
return(mylist)
}
mylist2 <- lapply(mylist, function(x) renameList(x,20))
# > str(mylist2)
# List of 4
# $ One :List of 20
# ..$ A: NULL
# ..$ B: NULL
# ..$ C: NULL

Resources