mapvin vin
mapvin vin

Reputation: 81

Loops using Lapply

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"

Upvotes: 1

Views: 49

Answers (2)

r2evans
r2evans

Reputation: 160407

There's no need for *apply:

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

Upvotes: 0

Kra.P
Kra.P

Reputation: 15123

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"

Upvotes: 3

Related Questions