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,680 points
32 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,010 points

Related Questions In Data Analytics

0 votes
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 13, 2018 in Data Analytics by darklord
• 6,140 points
91 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,680 points
403 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,700 points
262 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,680 points
94 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,140 points
37 views
0 votes
1 answer

Randomly subset of data with dplyr

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

answered Jun 18, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
22 views
0 votes
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,700 points
47 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 37,320 points
38 views
0 votes
1 answer
0 votes
1 answer