Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8914303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:41:02+00:00 2026-06-15T04:41:02+00:00

When creating nested lists R has, what I consider, helpful naming structure for the

  • 0

When creating nested lists R has, what I consider, helpful naming structure for the elements of the list. I have a list of lists and want to apply a function of every single vector contained within any list. lapply does this but then strips the naming structure of the list. How can I lapply of every vector element of the nested list without stripping the names? I tried passing the index instead but that seems to do the same thing (strip names).

TEST <- list(n1=list(a="5", b="65"), n2=list(a="f", b= "f6"))
TEST
lapply(TEST, function(x) gsub("6", "foo", x))
lapply(seq_along(TEST), function(i) gsub("6", "foo", TEST[[i]]))

Console Output:

> TEST
$n1
$n1$a
[1] "5"

$n1$b
[1] "65"


$n2
$n2$a
[1] "f"

$n2$b
[1] "f6"


> lapply(TEST, function(x) gsub("6", "foo", x))
$n1
[1] "5"    "foo5"

$n2
[1] "f"    "ffoo"

Desired outcome:

$n1
$n1$a
[1] "5"

$n1$b
[1] "foo5"


$n2
$n2$a
[1] "f"

$n2$b
[1] "ffoo"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:41:04+00:00Added an answer on June 15, 2026 at 4:41 am

    It seems you want the rapply variant of the *apply family

    rapply(TEST, function(x){gsub("6", "foo", x)}, how = "list")
    
    $n1
    $n1$a
    [1] "5"
    
    $n1$b
    [1] "foo5"
    
    
    $n2
    $n2$a
    [1] "f"
    
    $n2$b
    [1] "ffoo"
    

    You could also do it like this to avoid writing an anonymous function

    rapply(TEST, gsub, pattern = "6", replacement = "foo", how = "list")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that i'm reading in, then creating nested lists that i
I have a nested, mutual recursive data structure , and want to associate computational
I am trying to create a list with the following (nested) structure: l <-
I am creating a nested list using sencha. Now on tap i get a
I have a web app that has to do something every, let's say first
I'm new in rails and need some help creating a Nested form. I have
I am unable to remove bullets in nested unordered lists after sIFR has been
I had a question, re: creating nested html tags in Rails, since I am
I am having some trouble creating a nested association and I can't quite spot
I'm creating a child by my nested form and I know the routes work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.