Randomly subset of data with dplyr

0 votes

This is not what I want. I want to extract groups randomly from a data frame not the random rows from each subset.

For example,

xx <- rep(rep(seq(0,800,200),each=10),times=2)
       yy<-c(replicate(2,sort(10^runif(10,-1,0),decreasing=TRUE)),replicate(2,sort(10^runif(10,-1,0),decreasing=TRUE)), replicate(2,sort(10^runif(10,-2,0),decreasing=TRUE)),replicate(2,sort(10^runif(10,-3,0),decreasing=TRUE)), replicate(2,sort(10^runif(10,-4,0), decreasing=TRUE)))    
       V <- rep(seq(100,2500,length.out=10),times=2)
       No <- rep(1:10,each=10)
       df <- data.frame(V,xx,yy,No)
library(dplyr)
    random <-  df %>%
            group_by(No)%>%
        sample_n(5,replace=T)  
Jun 18, 2018 in Data Analytics by darklord
• 6,170 points
47 views

1 answer to this question.

0 votes

Maybe this is what you want:

# sample from distinct values of No
my_groups <- 
  df %>% 
  select(No) %>% 
  distinct %>% 
  sample_n(5)

# merge the two datasets
my_df <-
  left_join(my_groups, df)
answered Jun 18, 2018 by CodingByHeart77
• 3,690 points

Related Questions In Data Analytics

0 votes
1 answer

How to use dplyr functions such as filter() inside nested data frames with map()

You can use map() call as follows:  map(full, ...READ MORE

answered Apr 6, 2018 in Data Analytics by darklord
• 6,170 points
317 views
0 votes
2 answers

How to subset rows containing NA in a chosen column of a data frame?

You can give this a try. subset(dataframe, is.na(dataframe$col2)) ...READ MORE

answered Aug 21 in Data Analytics by anonymous
• 31,840 points
123 views
0 votes
1 answer

How to filter a data frame with dplyr and tidy evaluation in R?

Requires the use of map_df to run each model, ...READ MORE

answered May 16, 2018 in Data Analytics by DataKing99
• 8,130 points
135 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
58 views
0 votes
1 answer

How to group all columns exculding a single column?

You can either use group_by or group_by_at Using ...READ MORE

answered Apr 12, 2018 in Data Analytics by darklord
• 6,170 points
90 views
0 votes
1 answer

How to limit output of a dataframe in R?

For randomly sampling a row/cell where a ...READ MORE

answered Apr 18, 2018 in Data Analytics by kappa3010
• 2,020 points
64 views
0 votes
1 answer

How to sort substring from a dataframe

First, extract the substring from the data ...READ MORE

answered Nov 4 in Data Analytics by Cherukuri
• 31,840 points
33 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
130 views
0 votes
1 answer

Extract a subset of a data frame based on a condition involving a field

Here are the two main approaches. I ...READ MORE

answered Jun 18, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
1,077 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
192 views