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 6832175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:49:36+00:00 2026-05-26T22:49:36+00:00

In R, how do you replace the following code using functions like apply ,

  • 0

In R, how do you replace the following code using functions like apply, lapply, rapply, do.call, etc.?

u <- 10:12
slist <- list()

for (i in 1:length(u)) {
  p <- combn(u, i) 
  for (j in 1:ncol(p)) {
    s <- paste(p[,j], collapse=",")
    slist[[s]] <- 0
  }
}

For this part:

  for (j in 1:ncol(p)) {
    s <- paste(p[,j], collapse=",")

I tried something like:

  s <- apply(p, 2, function(x) paste(x, collapse=","))

Which works. But then for that slist[[s]] <- 0 part inside that same for-loop, I don’t know what to do.

Edit: This is what I’m trying to do. For the vector u, I’m producing a list of all the subsets in that vector. Then for each subset, I’m assigning it to s, then using the string s as the name of an element in slist. Kind of strange, I know, but it’s for a homework assignment. For the code above, this would be the first 5 elements of slist:

 > slist
 $`10`
 [1] 0

 $`11`
 [1] 0

 $`12`
 [1] 0

 $`10,11`
 [1] 0

 $`10,12`
 [1] 0

Yeah, I’m just trying to learn how to use apply and stuff properly.

  • 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-05-26T22:49:37+00:00Added an answer on May 26, 2026 at 10:49 pm

    Here’s one solution:

    n <- unlist(lapply(seq_along(u), function(i) {
      apply(combn(length(u),i),2, function(x) paste(u[x], collapse=','))
    }
    ))
    
    slist <- list()
    slist[n] <- 0
    

    UPDATE Posted at the same time as @djhurio, it is very similar, but I took the liberty of changing the use of combn so it handles u of length 1, as @djhurio pointed out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently using the following JavaScript code: concatedSubstring.replace(/\//g, '-').replace(/[A-Za-z]/g, function(c){ return c.toUpperCase().charCodeAt(0)-64; });
I'm using the following code for fetching a latest tweet but I would like
Anybody have a regular expression to replace the following code: <a href=originalLink>hi</a> with: <a
In the following code I want to replace every occurrence of U.S.A with united
I've got the following code: Regex.Replace(text, words.ToString(), <dfn title=\ + this.FindDefinition($0) + \>$0</dfn>, RegexOptions.IgnoreCase
I'm currently refactoring code to replace Convert.To's to TryParse. I've come across the following
I would like to do the following, but I'm not sure I'm using the
I am using the following code to send out emails, the problem I am
I'm having problems running the following code snippet using JavaScript and JQuery. listOfDisplayCategories is
I have some code which is designed to replace a value in a list

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.