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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:49:49+00:00 2026-05-20T04:49:49+00:00

I am trying to accomplish two things. First if I have a vector 1:5

  • 0

I am trying to accomplish two things. First if I have a vector 1:5 I want to get a matrix (or two vectors) indicating the unique combinations of these elements including twice the same number but excluding repetitions.

Right now I can do this using a matrix:

foo <- matrix(1:5,5,5)
cbind(foo[upper.tri(foo,diag=TRUE)],foo[lower.tri(foo,diag=TRUE)])
      [,1] [,2]
 [1,]    1    1
 [2,]    1    2
 [3,]    2    3
 [4,]    1    4
 [5,]    2    5
 [6,]    3    2
 [7,]    1    3
 [8,]    2    4
 [9,]    3    5
[10,]    4    3
[11,]    1    4
[12,]    2    5
[13,]    3    4
[14,]    4    5
[15,]    5    5

But there has to be a simpler way. I tried to use Vectorize on seq but this gives me an error:

cbind(Vectorize(seq,"from")(1:5,5),Vectorize(seq,"to")(5,1:5))
    Error in Vectorize(seq, "from") : 
      must specify formal argument names to vectorize

A second thing I want to do is if I have a list containing vectors, bar, to get a vector containing the elements of the list repeated equal to the number of elements in that element. I can do this with:

unlist(apply(rbind(1:length(bar),sapply(bar,length)),2,function(x)rep(x[1],x[2])))
 [1] 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

But again there must be an easier way. I tried Vectorize again here but with the same error:

Vectorize(rep,"each")(1:length(bar),each=sapply(bar,length))
 in Vectorize(rep, "each") : 
  must specify formal argument names to vectorize
  • 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-20T04:49:49+00:00Added an answer on May 20, 2026 at 4:49 am

    To your first question: what about the simple combn() function in base:

    > combn(1:5,2)
         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    [1,]    1    1    1    1    2    2    2    3    3     4
    [2,]    2    3    4    5    3    4    5    4    5     5
    

    If you need a matrix arranged the one you made up, just transpose it with t(), like t(combn(1:5,2))

    Note: this will not give you back the combinations of repeated elements of your seq, but you may add those easily to the matrix.

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

Sidebar

Related Questions

This is what I'm trying to accomplish: I have two applications. One is a
Hi im trying to accomplish the following layout. I have two RelativeLayout, each one
What I'm trying to accomplish: I have two base files on my server that
What I am trying to accomplish: HTTP GET the contents of a site (say
I am trying to accomplish the following: 1. On click, have a div with
I'm trying to join two tables: Rooms and Room_Types (which have a relationship already).
I am trying to accomplish the atypical library learning a language application. I have
I have created these two files: class myDbClass { function dbConnect($db) { $dbhost =
I am trying to create a rewrite rule that accomplishes two things: Redirect (www.)?domain.com
Let me begin by explaining what I'm trying to accomplish. Essentially there are two

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.