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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:43:19+00:00 2026-06-15T17:43:19+00:00

I have been working on some code to carry out some analysis on data

  • 0

I have been working on some code to carry out some analysis on data from an astrophysics model I have been using (sph specfically). I am currently having a problem whereby the graphs the code below outputs (ten of them), while they appear to be correct, are not in the correct order, ie. the one calling itself the graph for 30 degrees looks like the one 50 degrees should output, 80 should be 40 and so on, with no apparent pattern. Any help that could be offered to fix this would be much appreciated.

#Set inclincations of observation
id <- c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90)
i <- (id * pi) / 180

#Determine sign of velocity vector in direction of observer, then magnitude of said vector (using mask)
for (i in 1:length(i)) {
GD$vecdir <- (GD$Xvel * sin(i) + GD$Zvel * cos(i))
mask <- (GD$vecdir >= 0)
if (sum(mask) > 0) { 
  GD$vecmag[mask] <- sqrt((GD$Xvel^2 * sin(i)^2 ) + (GD$Zvel^2 * cos(i)^2))
}
if (sum(mask) < length(mask)) { 
  GD$vecmag[!mask] <- -(sqrt((GD$Xvel^2 * sin(i)^2 ) + (GD$Zvel^2 * cos(i)^2)))
}  
h2 <- hist(GD$vecmag, breaks=720,  plot=FALSE)
jpeg(paste("VMLOS", (i-1)*10, ".jpeg", sep = ""), width = 1280, height = 720)
print(plot(h2$mids, h2$counts, type="p", pch=4, lty=3, xlab="Velocity Vector Magnitude", ylab="counts", main=paste("VMLOS", (i-1)*10, sep="")))
print(lines(lowess(h2$mids, h2$counts, f=0.05), lwd=4, col="red"))
dev.off()
}

For context, i is the inclination in the xz plane from which the object at the origin is being observed, in radians, with the conversion from degrees shown in the code, then the vecdir section works out whether the vector magnitude of a set or particles moving away from the origin should be positive or negative with reference to the observer, then the mask section calculates the magnitudes of such. Additionally, the mask part of the code returns the warning:

1: In GD$vecmag[mask] <- sqrt((GD$Xvel^2 * sin(i)^2) + (GD$Zvel^2 *  ... :
  number of items to replace is not a multiple of replacement length
2: In GD$vecmag[!mask] <- -(sqrt((GD$Xvel^2 * sin(i)^2) +  ... :
  number of items to replace is not a multiple of replacement length

for each usage (so 20 warnings), which doesn’t appear to negatively effect the output of the code, but is irritating, and I have been unable to fix it thus far.

Any help will be gratefully received.

  • 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-15T17:43:20+00:00Added an answer on June 15, 2026 at 5:43 pm

    For the warnings, you probably want this (note the indexing on the right-hand expression):

    GD$vecmag[mask] <- sqrt((GD$Xvel^2 * sin(i)^2 ) + (GD$Zvel^2 * cos(i)^2))[mask]
    
    # ...
    
    GD$vecmag[!mask] <- -(sqrt((GD$Xvel^2 * sin(i)^2 ) + (GD$Zvel^2 * cos(i)^2)))[!mask]
    

    This will change the results.

    In addition, you are not iterating over the correct values. 1:length(i) are not the values in i.
    Change this:

    i <- (id * pi) / 180
    
    #Determine sign of velocity vector in direction of observer, then magnitude of said vector (using mask)
    for (i in 1:length(i)) {
    

    to this:

    angles <- (id * pi) / 180
    
    #Determine sign of velocity vector in direction of observer, then magnitude of said vector (using mask)
    for (i in angles) {
    

    The original code is using sin(1), sin(2), …, rather than the values in the vector. That will also shuffle the results (and it may not be obvious that the wrong angles are being used).

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

Sidebar

Related Questions

I have some working code that I have been using on a test O365
I have been working through some code and I would like to work out
I have been working on some code to submit an uploaded image to PHP
I have been working on some code that is similar to the following: typedef
I have been working on some code that prints a textbox that lists contact
I have been working on some legacy C++ code that uses variable length structures
I have been looking for a some working code for the last couple of
I have been working on a project which is about data copy from one
I have been working on some jQuery code for an interactive map. I've already
I have some code I wrote a few years ago. It has been working

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.