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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:00:43+00:00 2026-06-18T01:00:43+00:00

My data frame has the following four columns: type(A or B), xvar, longitude, and

  • 0

My data frame has the following four columns: type(“A” or “B”), xvar, longitude, and latitude. It looks like:

      type    xvar    longitude    latitude
[1,]   A       20      -87.81        40.11
[2,]   A       12      -87.82        40.12
[3,]   A       50      -87.85        40.22
....
[21,]  B       24      -87.79        40.04
[22,]  B       30      -87.88        40.10
[23,]  B       12      -87.67        40.32
[24,]  B       66      -87.66        40.44
....

I have 20 rows for type=”A”, and 25,000 rows for type=”B”. My task is to randomly assign the values of xvar for 20 “A” data points onto the X-Y space of type “B” without replacement. For example, the xvar=20 as in the first observation of type=”A” can be randomly located in [22,] that is (-87.88,40.10) . Because I am doing that without replacement, in theory, I can do this replication 25,000/20 = 1,250 times. I want a 1,000 replication.

And I have a function (say, myfunc(xvar,longitude,latitude)) that returns one statistical value from one randome sample. I first create an empty matrix (say, myresult) of 1,000×1.

myresult <- array(0,dim=c(1000,1))

Then, for each random sample, I apply my function (myfunc) to calculate the statistic.

for (i in seq(1:1000)) {
  draw one sample, that has three variables: xvar, longitude, latitude.
  apply my function to this selected sample.
  store the calculated statistic in the myresult[i,]
}

I wonder how to do this in R. (And may be in Matlab??) Thanks!

=============================================================

Update: @user. Borrowing your idea, the following is what I want:

dd1 <- df[df$type == "B" ,] 
dd2 <- df[df$type == "A" ,]
v   <- dd2[sample(nrow(dd2), nrow(dd2)), ]
randomXvarOfA <- as.matrix(v[,c("xvar")])  
cols <- c("longitude","latitude")
B_shuffled_XY <- dd1[,cols][sample(nrow(dd1), nrow(dd2)), ]
dimnames(randomXvarOfA)=list(NULL,c("xvar"))
sampledData <- cbind(randomXvarOfA,B_shuffled_XY)
sampledData

   xvar longitude latitude
4   20    -87.79    40.04
7   12    -87.66    40.44
5   50    -87.88    40.10
  • 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-18T01:00:45+00:00Added an answer on June 18, 2026 at 1:00 am

    Read in your data:

      df<- read.table( text="
          type    xvar    longitude    latitude
          A       20      -87.81        40.11
          A       12      -87.82        40.12
          A       50      -87.85        40.22
          B       24      -87.79        40.04
          B       30      -87.88        40.10
           B       12      -87.67        40.32
          B       66      -87.66        40.44", header = TRUE)
    

    I was writing this without splitting and it looked so messy.
    So I decided just to split your data.frame.

        dd1 <- df[df$type == "B" ,]  # get all rows of just type A
        dd2 <- df[df$type == "A" ,]  # get all rows of just type B
    
        v   <- dd2[sample(nrow(dd2), 2), ] #sample two rows at random that are type A
        # if you want to sample 20 rows change the 2 to a 20
    
        cols <- c("longitude", "latitude")
        dd1[,cols][sample(nrow(dd1), 2), ] <- v[,cols] 
        #Add the random long/lat selected from type As into 2 random long/lat of B
    
    
    # put the As and Bs back together
    rbind(dd2,dd1)
    #  type xvar longitude latitude
    # 1    A   20    -87.81    40.11
    # 2    A   12    -87.82    40.12
    # 3    A   50    -87.85    40.22
    # 4    B   24    -87.79    40.04
    # 5    B   30    -87.85    40.22
    # 6    B   12    -87.81    40.11
    # 7    B   66    -87.66    40.44
    

    As you can see rows 5 and 6 of B have new randomly selected lat and long values from A types. I did not change the xvar values though. I don’t know if you want this. If you did want to change the xvars too then you would change cols to cols <- c("xvar","longitude", "latitude").

    Inside a function it would look like:

    changestuff <-  function(x){
    
            dd1 <- x[x$type == "B" ,]  # get just A
            dd2 <- x[x$type == "A" ,]  # get just B
            v   <- dd2[sample(nrow(dd2), 2), ]
            cols <- c("longitude", "latitude")
            dd1[,cols][sample(nrow(dd1), 2), ] <- v[,cols] 
            rbind(dd2,dd1)
                                }
    
    changestuff(df)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data frame that is some 35,000 rows, by 7 columns. it
SQLDF newbie here. I have a data frame which has about 15,000 rows and
I have the following question: I have data frame which looks like this. I
I have a data frame that has a format like the following: Month Frequency
I have the following data frame in R: >AcceptData Mean.Rank Sentence.Type 1 2.5 An+Sp+a
I have a data frame with 72 columns and 409 rows which is called:
I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have a data.frame that has a number of duplicate rows, akin to something
I have the following variables in a data frame: [1] Type I.alt idx06 idx07

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.