In a dpylr pipline how to use sample and seq

0 votes

 I have a dataframe consisting of two columns set1 & set2, create a new variable that is a randomly selected value between set1 and set2 (inclusive and equal probability) using dplyr.

I got an error

library(tidyverse)

data_frame(set1 = 1:10, set2 = 11) %>% mutate(rand_btwn = base::sample(seq(set1, set2, by = 1), size = 1))

Apr 6, 2018 in Data Analytics by DataKing99
• 8,250 points
1,181 views

1 answer to this question.

0 votes

For avoiding rowwise(), I prefer to use map() in mutate(), because for rowwise() there will be an unnecessary grouping creates.

set.seed(123)
data_frame(set1 = 1:10, set2 = 11) %>%
  mutate(rand_btwn = map_int(map2(set1, set2, seq), sample, size = 1))

 

## # A tibble: 10 x 3

#      set1  set2 rand_btwn
#    <int> <dbl>     <int>
#  1     1    11         4
#  2     2    11         9
#  3     3    11         6
#  4     4    11        11
#  5     5    11        11
#  6     6    11         6
#  7     7    11         9
#  8     8    11        11
#  9     9    11        10
# 10    10    11        10
answered Apr 6, 2018 by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika

Related Questions In Data Analytics

0 votes
1 answer
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 17, 2018 in Data Analytics by DataKing99
• 8,250 points
1,897 views
0 votes
1 answer

How to sample n random rows per group in a dataframe?

You can assign a random ID to ...READ MORE

answered Jul 3, 2018 in Data Analytics by Sahiti
• 6,370 points
5,108 views
+1 vote
2 answers
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,249 views
0 votes
1 answer

With the help of tidyverse: how to rename a column to a variable name

With the help of Dplyr: rename function ...READ MORE

answered Apr 3, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Apr 3, 2018 by DeepCoder786 945 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

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

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

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

How to change mulitiple characters in a column to a date

Firstly we have to set dataf variable ...READ MORE

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

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,919 views
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