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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:53:20+00:00 2026-05-21T23:53:20+00:00

How can I select all of the rows for a random sample of column

  • 0

How can I select all of the rows for a random sample of column values?

I have a dataframe that looks like this:

tag  weight

R007     10
R007     11
R007      9
J102     11
J102      9
J102     13
J102     10
M942      3
M054      9
M054     12  
V671     12
V671     13
V671      9
V671     12
Z990     10
Z990     11

That you can replicate using…

weights_df <- structure(list(tag = structure(c(4L, 4L, 4L, 1L, 1L, 1L, 1L, 
3L, 2L, 2L, 5L, 5L, 5L, 5L, 6L, 6L), .Label = c("J102", "M054", 
"M942", "R007", "V671", "Z990"), class = "factor"), value = c(10L, 
11L, 9L, 11L, 9L, 13L, 10L, 3L, 9L, 12L, 12L, 14L, 5L, 12L, 11L, 
15L)), .Names = c("tag", "value"), class = "data.frame", row.names = c(NA, 
-16L))

I need to create a dataframe containing all of the rows from the above dataframe for two randomly sampled tags. Let’s say tags R007and M942 get selected at random, my new dataframe needs to look like this:

tag  weight

R007     10
R007     11
R007      9
M942      3

How do I do this?

I know I can create a list of two random tags like this:

library(plyr)
tags <- ddply(weights_df, .(tag), summarise, count = length(tag))
set.seed(5464)
tag_sample <- tags[sample(nrow(tags),2),]
tag_sample

Resulting in…

   tag count
4 R007     3
3 M942     1

But I just don’t know how to use that to subset my original dataframe.

  • 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-21T23:53:21+00:00Added an answer on May 21, 2026 at 11:53 pm

    is this what you want?

    subset(weights_df, tag%in%sample(levels(tag),2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table storing millions of rows. It looks something like this: Table_Docs
how can i select all the rows of buydate that are in a date
I have a datagrid(asp.net) on my page. I would like to select all rows
How can i select all rows where the month is 04 from 2010 where
SELECT name FROM sys.databases -- this can list all database name in the server
I have two tables which I need to select all rows from where the
Is it possible in MySQL to select all rows where ANY column contains a
I started by asking this question I wish to Select all rows which contain
I have two dates: 10-11-2010 and 17-11-2010 Now i would like to SELECT all
I have a terrible doubt. I have a query like this: SELECT id,fatherID FROM

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.