rlistloopslapply

Apply function to one element in a list of lists


I have mylist a list of lists as follows:

list1 <- list(label = c(2,8,9,5,9,10,0,1), code = c(585,652,756,255,365,123), name = c("cow", "sheep", "fox", "dog", "cat"))
list2 <- list(label = c(12,2,0,3,2,11,4,5), code = c(423,912,82,171,812,712), name = c("cow", "sheep", "fox", "dog", "cat"))
list3 <- list(label = c(10,7,9,1,3,14,8,6), code = c(372,465,962,23,702,12), name = c("cow", "sheep", "fox", "dog", "cat"))
mylist <- list(list1, list2, list3)

My actual list is a list of 10000 and more such lists.

I want to randomize the order of values in "name" in all lists without touching "label" and "code". The desired output is as follows

list1 <- list(label = c(2,8,9,5,9,10,0,1), code = c(585,652,756,255,365,123), name = c("sheep", "fox", "cat", "cow", "dog"))
list2 <- list(label = c(12,2,0,3,2,11,4,5), code = c(423,912,82,171,812,712), name = c("sheep", "cow", "cat", "fox", "dog"))
list3 <- list(label = c(10,7,9,1,3,14,8,6), code = c(372,465,962,23,702,12), name = c("cat", "dog", "sheep", "fox", "cow"))
mylist <- list(list1, list2, list3)

All I have managed is to randomize all three elements ("label","code","name") using the following

lapply(mylist, function(x) sapply(x, sample))

Solution

  • You can do

    lapply(mylist, \(l) {l[["name"]] = sample(l[["name"]], length(l[["name"]]), FALSE); l})
    

    which can be written more concise.

    Notice that FALSE refers to the argument replace of sample(). Have a look at help(sample). Ypu might want TRUE (with replacement) instead.

    (I suspect we might be able to write a streamlined solution if you give further details.)