Home > Software design >  Loops using Lapply
Loops using Lapply

Time:12-29

I am trying to create a for loop using lapply. So basically, as and when the vector columns_s gets added, same vector gets created with its own value. For example, refer below

columns_s <- c("kingdom", "family")   ## user may add more elements to the vector
lapply(
  columns_to_consider,
  FUN = function(i){
   i = i
  })

Expected output

>kingdom
[1] "kingdom"

>family
[1] "family"

CodePudding user response:

Using sapply,

x <- sapply(
  c("kingdom", "family"),
  FUN = function(i){
    i = i
  }, simplify = F)
x

will give you result

$kingdom
[1] "kingdom"

$family
[1] "family"

CodePudding user response:

There's no need for *apply:

columns_s <- c("kingdom", "family")
as.list(setNames(nm = columns_s))
# $kingdom
# [1] "kingdom"
# $family
# [1] "family"
  • Related