Vishnu Raghavan
Vishnu Raghavan

Reputation: 83

DPLYR based multiple subsetting

The following dataframe contains data regarding a marketing campaign.

Sl No   Success_Percentage  communication_type  Message
 1       35.46666667           email            Hello, Buy New Product
  2      32.32830821          email              Hi Buy New Product
  3       22.9226361           SMS               Hello World
  4       21.88888889          SMS               Hello, Buy New Product
  5       40.04085802          FB                Hi Buy New Product
  6       38.7283237            FB               Hello World

I wish create a table as follows.

Success_Percentage  communication_type  Message
  22.9226361          SMS             Hello World
 21.88888889          SMS             Hello, Buy New Product
   35.46666667      email             Hello, Buy New Product
   32.32830821      email             Hi Buy New Product
   40.04085802       FB               Hi Buy New Product
    38.7283237        FB              Hello World

I Have used dplyr package to achieve subsets.

  require(dplyr)
  dataframe%>%
  group_by(communication_type, Message)
  summarise_all(order(Success_Percentage))

I am unable to obtain the above output. Note that Success % is randomly mixed(not in linear order as here) I am unable to get the desired output.

Upvotes: 0

Views: 58

Answers (1)

HNSKD
HNSKD

Reputation: 1644

From your new table, it seems that you would like to arrange the table according to the communication_type and within each communication_type, you would like to arrange Success_Percentage in descending order.

Your dataset:

df <- structure(list(Sl_No = 1:6, Success_Percentage = c(35.46666667, 
                                                         32.32830821, 22.9226361, 21.88888889, 40.04085802, 38.7283237
), communication_type = c("email", "email", "SMS", "SMS", "FB", 
                          "FB"), Message = c("Hello, Buy New Product", "Hi Buy New Product", 
                                             "Hello World", "Hello, Buy New Product", "Hi Buy New Product", 
                                             "Hello World")), class = "data.frame", .Names = c("Sl_No", "Success_Percentage", 
                                                                                               "communication_type", "Message"), row.names = c(NA, -6L))

Factorise communication_type in the order SMS, email and FB; and arrange accordingly:

df %>% 
  mutate(communication_type = factor(communication_type, levels = c("SMS", "email", "FB"))) %>%
  arrange(communication_type, desc(Success_Percentage))

Upvotes: 1

Related Questions