How to limit output of a dataframe in R

0 votes

I'm using a data frame of transactions.

Presently I'm using the dplyr package to filter the transaction by gender.

I have considered gender as 0/1 for F/M respectively.

How do I filter 2 rows, one with gender == 0 and one with gender == 1?

I tried doing as follows:

The closest was to do it like this

data %>% arrange(gender) 

and then select the two transactions in middle; in which one would be ==1 and other would be ==0

Can someone suggest a better way?

Apr 18, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
3,425 views

1 answer to this question.

0 votes

For randomly sampling a row/cell where a condition is in another cell, you can use sample as follows:

# Dummy data: data = value of interest, g = gender (0,1)
data1 <- data.frame("data" = rnorm(10, 0, 1), "g" = sample(c(0,1), replace = T, size = 10))

Then you can use the dplyr package in the following way:.

pull(data1 %>% group_by(g) %>% sample_n(1), data)
answered Apr 18, 2018 by kappa3010
• 2,090 points

Related Questions In Data Analytics

+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 14, 2018 in Data Analytics by Sahiti
• 6,370 points
7,067 views
0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,634 views
0 votes
1 answer

How to save a DataFrame in R??

Suppose your DataFrame is named as df: write.csv(df,file="exmp.csv") Then ...READ MORE

answered Apr 20, 2018 in Data Analytics by DeepCoder786
• 1,720 points
5,192 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,750 points
4,567 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 Sahiti
• 6,370 points
3,550 views
0 votes
1 answer

Randomly subset of data with dplyr

Maybe this is what you want: # sample ...READ MORE

answered Jun 19, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
861 views
0 votes
1 answer

How to sort substring from a dataframe

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

answered Nov 4, 2019 in Data Analytics by Cherukuri
• 33,030 points
1,126 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
1,681 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP