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 23 views

## Your answer

 Your name to display (optional): Email me at this address if my answer is selected or commented on: Privacy: Your email address will only be used for sending these notifications.
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
• 2,010 points

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

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, ...READ MORE

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

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

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

0 votes
1 answer

## Randomly subset of data with dplyr

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

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

0 votes
2 answers

## In data frame how to spilt strings into values?

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

0 votes
1 answer

## What is the difference between [] and [[]] notations to access the elements of a list or dataframe in R?

R provides 3 basic indexing operators. Refer ...READ MORE

0 votes
1 answer

## How to use a function to repeat a set of procedures on specific set of columns in a data frame?

You can parse the strings to symbols. ...READ MORE