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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:26:10+00:00 2026-06-11T09:26:10+00:00

I have a vector of values (numbers only). I want to split up this

  • 0

I have a vector of values (numbers only). I want to split up this vector into two vectors. One vector will contain values less than the average of the original vector, the other will contain values more than the average of the original vector. I have the following as a test R script:

v <- c(1,1,4,6,3,67,10,194,847)

#Initialize
v.in<- c(rep(0),length(v))
v.out<- c(rep(0),length(v))

for (i in 1:length(v))
{
   if (v < 0.68 * mean(v))
   {
      v.in[i] <- v[i]
   }
   else
   {
      v.out[i] <- v[i]
   }
}
v.in
v.out

## <https://gist.github.com/8a6747ea9b7421161c43>

I get the following result:

9: In if (v < 0.68 * mean(v)) { :
  the condition has length > 1 and only the first element will be used
> v.in
[1]   1   1   4   6   3  67  10 194 847
> v.out
[1] 0 9
> v
[1]   1   1   4   6   3  67  10 194 847
> 

Clearly, 0 and 9 are not values of any of the elements in v.

Any suggestions what is going on and how to fix this?

Thanks,
Ed

  • 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-11T09:26:11+00:00Added an answer on June 11, 2026 at 9:26 am

    @BenBolker pointed out in the comment why you code doesn’t work: you need to select a single element from v when using if. However, you might find split a better function for a task like this:

    split(v,v<0.68*mean(v))
    $`FALSE`
    [1] 194 847
    
    $`TRUE`
    [1]  1  1  4  6  3 67 10
    

    The answer to the mystery of v.out is that its branch doesn’t get selected so it doesn’t get changed. It therefore retains its inital value, which is (presumably) erroneously given the value of a single 0 and the length of the vector (9) rather than nine copies of zero as I suspect you intended.

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

Sidebar

Related Questions

I have two floating-point number vectors which contain the same values up to a
I have the vector d<-1:100 I want to sample k=3 times from this vector
I have a vector a = 1111000011100001110000100100 and I have to compute two values
I have a vector of values I need to add to a second vector
I have a vector containing the values 0, 1, 2 and 3. What I
I have a __m256d vector packed with four 64-bit floating-point values. I need to
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I have vector with values between 1 and N > 1 . Some values
I have a class called SparseMatrix which contain a private vector of type Cell.
I have a vector that looks like this: mass<-c(-2, -6, -79, 31, -28, 198,

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.