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

  • Home
  • SEARCH
  • 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 9024005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:54:04+00:00 2026-06-16T05:54:04+00:00

I have a vector with varying length, which can sometimes be of length 1.

  • 0

I have a vector with varying length, which can sometimes be of length 1.

I would like to sample from this vector such that if its length is 1 it always samples that 1 number.

sample() won’t do this because it samples from 1 to the digit when sample size is 1.

  • 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-16T05:54:05+00:00Added an answer on June 16, 2026 at 5:54 am

    This is a documented feature:

    If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x. Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x).

    An alternative is to write your own function to avoid the feature:

    sample.vec <- function(x, ...) x[sample(length(x), ...)]
    sample.vec(10)
    # [1] 10
    sample.vec(10, 3, replace = TRUE)
    # [1] 10 10 10
    

    Some functions with similar behavior are listed under seq vs seq_along. When will using seq cause unintended results?

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

Sidebar

Related Questions

I have the vector d<-1:100 I want to sample k=3 times from this vector
I have a vector of pairs representing hops between nodes which I would like
I have a vector<int> container that has integers (e.g. {1,2,3,4}) and I would like
I have a variable b which is of varying length. Eg. it can be
I have multiple vectors of varying lengths that I would like to plot next
I have a vector of strings which are changing its contents for no apparent
I have a vector of dates: Date = seq(from=as.POSIXct(2011-01-01 00:00), to=as.POSIXct(2011-12-31 23:00), length=8760) It
I have two algorithms that I'm implementing: AlgorithmA which works with Vector values and
I have Vector declared in index.jsp page like this: <%! Vector vNumbers = new
I have Vector of elements that i set into BaseAdapter subclass and this adapter

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.