Extracting crucial details from M-Pesa text messages
M-PESA, operated by Safaricom is a mobile money transfer service in Kenya. It is one of the best mobile payment options. One can use M-PESA to buy airtime, send or receive cash on their phones and pay goods and/or services using merchant paybill numbers or till numbers. The following is an example of an M-Pesa text message that one would receive if they paid for goods or services via a till number.
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334#
This script details how one can extract some key details from the message for purposes of data analysis. These details include the transaction code, transaction type (purchase of airtime, cash transfer, cash withdrawal, cash deposit, pay bill payment, merchant payment (Lipa Na Mpesa)), date (including the day, month and year), time (hour, minute and time of day (morning / afternoon)) , transaction amount , transaction cost and M-Pesa balance.
Note: This data has been anonymized
0. Load the packages required
## Create a vector of packages to be installed
pkgs <- c("tidyverse","lubridate", "kableExtra")
## Check if there are packages you want to load, that are not already installed.
miss_pkgs <- pkgs[!pkgs %in% installed.packages()[,1]]
## Installing the missing packages
if(length(miss_pkgs)>0){
install.packages(miss_pkgs)
}
## Loading all the packages
invisible(lapply(pkgs,library,character.only=TRUE))
## Remove the objects that are no longer required
rm(miss_pkgs)
rm(pkgs)
1. Read in the data
df <- read_csv("mpesa.csv - Sheet1.csv")
#> Parsed with column specification:
#> cols(
#> `Text message` = col_character()
#> )
tab_output(df)
Text message |
---|
XXXYYYZZZ1 confirmed.You bought Ksh100.00 of airtime on 27/4/21 at 11:05 AM.New M-PESA balance is Ksh5,350.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,900.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. |
XXXYYYZZZ2 confirmed.You bought Ksh30.00 of airtime on 25/4/21 at 3:45 PM.New M-PESA balance is Ksh5,450.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,940.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. |
XXXYYYZZZ3 Confirmed. Ksh500.00 sent to KPLC PREPAID for account 123456789 on 23/4/21 at 9:57 PM New M-PESA balance is Ksh5,941.22. Transaction cost, Ksh23.00. Amount you can transact within the day is 296,283.00. |
XXXYYYZZZ4 Confirmed. Ksh607.00 paid to SUPERMARKET FULANI HAPO. on 23/4/21 at 3:26 PM.New M-PESA balance is Ksh6,494.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,813.00.You can now access M-PESA via *334# |
XXXYYYZZZ5 Confirmed. Ksh1,030.00 sent to MSEE FULANI HAPO +254722222222 on 23/4/21 at 2:38 PM. New M-PESA balance is Ksh7. Transaction cost, Ksh22.00. Amount you can transact within the day is 297,420.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. |
XXXYYYZZZ6 Confirmed.on 23/4/21 at 12:31 PMWithdraw Ksh1,500.00 from 010000 - Shop fulani hapo New M-PESA balance is Ksh8. Transaction cost, Ksh28.00. Amount you can transact within the day is 298,500.00. |
XXXYYYZZZ9 Confirmed. Ksh80.00 sent to MSEE WA CHIPO 0704444444 on 12/4/21 at 1:57 PM. New M-PESA balance is Ksh 210. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,665.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. |
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334# |
XXXYYYZZZ11 Confirmed. On 25/7/20 at 3:29 PM Give Ksh2,000.00 cash to Agent fulani New M-PESA balance is Ksh2,100.22. |
2. Extract crucial information from the messages
2.1 Transaction code
df <- df %>%
mutate(trn_code =
gsub("confirmed.*|Confirmed.*", "", `Text message`))
tab_output(df)
Text message | trn_code |
---|---|
XXXYYYZZZ1 confirmed.You bought Ksh100.00 of airtime on 27/4/21 at 11:05 AM.New M-PESA balance is Ksh5,350.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,900.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ1 |
XXXYYYZZZ2 confirmed.You bought Ksh30.00 of airtime on 25/4/21 at 3:45 PM.New M-PESA balance is Ksh5,450.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,940.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ2 |
XXXYYYZZZ3 Confirmed. Ksh500.00 sent to KPLC PREPAID for account 123456789 on 23/4/21 at 9:57 PM New M-PESA balance is Ksh5,941.22. Transaction cost, Ksh23.00. Amount you can transact within the day is 296,283.00. | XXXYYYZZZ3 |
XXXYYYZZZ4 Confirmed. Ksh607.00 paid to SUPERMARKET FULANI HAPO. on 23/4/21 at 3:26 PM.New M-PESA balance is Ksh6,494.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,813.00.You can now access M-PESA via *334# | XXXYYYZZZ4 |
XXXYYYZZZ5 Confirmed. Ksh1,030.00 sent to MSEE FULANI HAPO +254722222222 on 23/4/21 at 2:38 PM. New M-PESA balance is Ksh7. Transaction cost, Ksh22.00. Amount you can transact within the day is 297,420.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ5 |
XXXYYYZZZ6 Confirmed.on 23/4/21 at 12:31 PMWithdraw Ksh1,500.00 from 010000 - Shop fulani hapo New M-PESA balance is Ksh8. Transaction cost, Ksh28.00. Amount you can transact within the day is 298,500.00. | XXXYYYZZZ6 |
XXXYYYZZZ9 Confirmed. Ksh80.00 sent to MSEE WA CHIPO 0704444444 on 12/4/21 at 1:57 PM. New M-PESA balance is Ksh 210. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,665.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ9 |
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334# | XXXYYYZZZ10 |
XXXYYYZZZ11 Confirmed. On 25/7/20 at 3:29 PM Give Ksh2,000.00 cash to Agent fulani New M-PESA balance is Ksh2,100.22. | XXXYYYZZZ11 |
2.2 Transaction type
df <- df %>%
mutate(trn_type =
## Purchase of airtime
ifelse(`Text message` %in%
grep("airtime", `Text message`,value = TRUE,
ignore.case = TRUE), "Airtime Purchase",
## Transfer
ifelse(`Text message` %in%
grep("sent to", `Text message`,value = TRUE,
ignore.case = TRUE) &
!`Text message` %in%
grep("for account", `Text message`,value = TRUE,
ignore.case = TRUE), "Customer Transfer",
## Withdrawal
ifelse(`Text message` %in%
grep("withdraw", `Text message`,value = TRUE,
ignore.case = TRUE), "Withdrawal",
## Deposit
ifelse(`Text message` %in%
grep("Give", `Text message`,value = TRUE,
ignore.case = TRUE), "Deposit",
## Pay Bill Payment
ifelse(`Text message` %in%
grep("for account", `Text message`,value = TRUE,
ignore.case = TRUE), "Pay Bill Payment",
## Merchant Payment (Lipa Na Mpesa)
ifelse(`Text message` %in%
grep("paid to", `Text message`,value = TRUE,
ignore.case = TRUE), "Merchant Payment","")))))))
tab_output(df)
Text message | trn_code | trn_type |
---|---|---|
XXXYYYZZZ1 confirmed.You bought Ksh100.00 of airtime on 27/4/21 at 11:05 AM.New M-PESA balance is Ksh5,350.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,900.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ1 | Airtime Purchase |
XXXYYYZZZ2 confirmed.You bought Ksh30.00 of airtime on 25/4/21 at 3:45 PM.New M-PESA balance is Ksh5,450.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,940.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ2 | Airtime Purchase |
XXXYYYZZZ3 Confirmed. Ksh500.00 sent to KPLC PREPAID for account 123456789 on 23/4/21 at 9:57 PM New M-PESA balance is Ksh5,941.22. Transaction cost, Ksh23.00. Amount you can transact within the day is 296,283.00. | XXXYYYZZZ3 | Pay Bill Payment |
XXXYYYZZZ4 Confirmed. Ksh607.00 paid to SUPERMARKET FULANI HAPO. on 23/4/21 at 3:26 PM.New M-PESA balance is Ksh6,494.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,813.00.You can now access M-PESA via *334# | XXXYYYZZZ4 | Merchant Payment |
XXXYYYZZZ5 Confirmed. Ksh1,030.00 sent to MSEE FULANI HAPO +254722222222 on 23/4/21 at 2:38 PM. New M-PESA balance is Ksh7. Transaction cost, Ksh22.00. Amount you can transact within the day is 297,420.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ5 | Customer Transfer |
XXXYYYZZZ6 Confirmed.on 23/4/21 at 12:31 PMWithdraw Ksh1,500.00 from 010000 - Shop fulani hapo New M-PESA balance is Ksh8. Transaction cost, Ksh28.00. Amount you can transact within the day is 298,500.00. | XXXYYYZZZ6 | Withdrawal |
XXXYYYZZZ9 Confirmed. Ksh80.00 sent to MSEE WA CHIPO 0704444444 on 12/4/21 at 1:57 PM. New M-PESA balance is Ksh 210. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,665.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ9 | Customer Transfer |
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334# | XXXYYYZZZ10 | Merchant Payment |
XXXYYYZZZ11 Confirmed. On 25/7/20 at 3:29 PM Give Ksh2,000.00 cash to Agent fulani New M-PESA balance is Ksh2,100.22. | XXXYYYZZZ11 | Deposit |
2.3 Date (Day, Month, Year)
## Date
df <- df %>%
mutate(temp = gsub(".*confirmed|.*Confirmed", "", `Text message`)) %>%
mutate(trn_date = str_match(temp, regex("on\\s*(.*?)\\s*at",
ignore_case = TRUE))[,2],
trn_date = as.Date(trn_date, format = '%d/%m/%y'))
## Day, Month, Year
df <- df %>%
mutate(trn_day = day(trn_date),
trn_dayofweek = wday(trn_date, label = TRUE, abbr = FALSE),
trn_month = month(trn_date, label = TRUE, abbr = FALSE),
trn_year = year(trn_date))
tab_output(df %>% select(-temp))
Text message | trn_code | trn_type | trn_date | trn_day | trn_dayofweek | trn_month | trn_year |
---|---|---|---|---|---|---|---|
XXXYYYZZZ1 confirmed.You bought Ksh100.00 of airtime on 27/4/21 at 11:05 AM.New M-PESA balance is Ksh5,350.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,900.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ1 | Airtime Purchase | 2021-04-27 | 27 | Tuesday | April | 2021 |
XXXYYYZZZ2 confirmed.You bought Ksh30.00 of airtime on 25/4/21 at 3:45 PM.New M-PESA balance is Ksh5,450.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,940.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ2 | Airtime Purchase | 2021-04-25 | 25 | Sunday | April | 2021 |
XXXYYYZZZ3 Confirmed. Ksh500.00 sent to KPLC PREPAID for account 123456789 on 23/4/21 at 9:57 PM New M-PESA balance is Ksh5,941.22. Transaction cost, Ksh23.00. Amount you can transact within the day is 296,283.00. | XXXYYYZZZ3 | Pay Bill Payment | 2021-04-23 | 23 | Friday | April | 2021 |
XXXYYYZZZ4 Confirmed. Ksh607.00 paid to SUPERMARKET FULANI HAPO. on 23/4/21 at 3:26 PM.New M-PESA balance is Ksh6,494.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,813.00.You can now access M-PESA via *334# | XXXYYYZZZ4 | Merchant Payment | 2021-04-23 | 23 | Friday | April | 2021 |
XXXYYYZZZ5 Confirmed. Ksh1,030.00 sent to MSEE FULANI HAPO +254722222222 on 23/4/21 at 2:38 PM. New M-PESA balance is Ksh7. Transaction cost, Ksh22.00. Amount you can transact within the day is 297,420.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ5 | Customer Transfer | 2021-04-23 | 23 | Friday | April | 2021 |
XXXYYYZZZ6 Confirmed.on 23/4/21 at 12:31 PMWithdraw Ksh1,500.00 from 010000 - Shop fulani hapo New M-PESA balance is Ksh8. Transaction cost, Ksh28.00. Amount you can transact within the day is 298,500.00. | XXXYYYZZZ6 | Withdrawal | 2021-04-23 | 23 | Friday | April | 2021 |
XXXYYYZZZ9 Confirmed. Ksh80.00 sent to MSEE WA CHIPO 0704444444 on 12/4/21 at 1:57 PM. New M-PESA balance is Ksh 210. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,665.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ9 | Customer Transfer | 2021-04-12 | 12 | Monday | April | 2021 |
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334# | XXXYYYZZZ10 | Merchant Payment | 2021-03-17 | 17 | Wednesday | March | 2021 |
XXXYYYZZZ11 Confirmed. On 25/7/20 at 3:29 PM Give Ksh2,000.00 cash to Agent fulani New M-PESA balance is Ksh2,100.22. | XXXYYYZZZ11 | Deposit | 2020-07-25 | 25 | Saturday | July | 2020 |
2.4 Time (Hour, Minute, Time of day (morning / afternoon))
df <- df %>%
mutate(temp = gsub(".*confirmed|.*Confirmed", "", `Text message`))%>%
mutate(trn_time =
str_match(temp, regex("at\\s*(.*?)\\s*New M-PESA balance",
ignore_case = TRUE))[,2],
trn_time = gsub("Withdraw.*|Give.*|\\.", "", trn_time),
trn_time) %>%
mutate(trn_timeofday =
ifelse(trn_time %in% grep("PM", trn_time,
ignore.case = FALSE, value = TRUE), "Afternoon",
ifelse(trn_time %in% grep("AM", trn_time,
ignore.case = FALSE, value = TRUE), "Morning",""))) %>%
mutate(trn_time = trimws(gsub("AM|PM", "", trn_time))) %>%
separate(trn_time, into = c("trn_hour", "trn_minute"),
sep = ":", remove = FALSE) %>%
mutate(across(c(trn_hour, trn_minute), ~as.numeric(trimws(.)))) %>%
mutate(trn_hour = ifelse(trn_timeofday == "Afternoon" & trn_hour!=12,
trn_hour + 12, trn_hour)) %>%
select(-temp)
tab_output(df)
Text message | trn_code | trn_type | trn_date | trn_day | trn_dayofweek | trn_month | trn_year | trn_time | trn_hour | trn_minute | trn_timeofday |
---|---|---|---|---|---|---|---|---|---|---|---|
XXXYYYZZZ1 confirmed.You bought Ksh100.00 of airtime on 27/4/21 at 11:05 AM.New M-PESA balance is Ksh5,350.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,900.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ1 | Airtime Purchase | 2021-04-27 | 27 | Tuesday | April | 2021 | 11:05 | 11 | 5 | Morning |
XXXYYYZZZ2 confirmed.You bought Ksh30.00 of airtime on 25/4/21 at 3:45 PM.New M-PESA balance is Ksh5,450.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,940.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ2 | Airtime Purchase | 2021-04-25 | 25 | Sunday | April | 2021 | 3:45 | 15 | 45 | Afternoon |
XXXYYYZZZ3 Confirmed. Ksh500.00 sent to KPLC PREPAID for account 123456789 on 23/4/21 at 9:57 PM New M-PESA balance is Ksh5,941.22. Transaction cost, Ksh23.00. Amount you can transact within the day is 296,283.00. | XXXYYYZZZ3 | Pay Bill Payment | 2021-04-23 | 23 | Friday | April | 2021 | 9:57 | 21 | 57 | Afternoon |
XXXYYYZZZ4 Confirmed. Ksh607.00 paid to SUPERMARKET FULANI HAPO. on 23/4/21 at 3:26 PM.New M-PESA balance is Ksh6,494.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,813.00.You can now access M-PESA via *334# | XXXYYYZZZ4 | Merchant Payment | 2021-04-23 | 23 | Friday | April | 2021 | 3:26 | 15 | 26 | Afternoon |
XXXYYYZZZ5 Confirmed. Ksh1,030.00 sent to MSEE FULANI HAPO +254722222222 on 23/4/21 at 2:38 PM. New M-PESA balance is Ksh7. Transaction cost, Ksh22.00. Amount you can transact within the day is 297,420.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ5 | Customer Transfer | 2021-04-23 | 23 | Friday | April | 2021 | 2:38 | 14 | 38 | Afternoon |
XXXYYYZZZ6 Confirmed.on 23/4/21 at 12:31 PMWithdraw Ksh1,500.00 from 010000 - Shop fulani hapo New M-PESA balance is Ksh8. Transaction cost, Ksh28.00. Amount you can transact within the day is 298,500.00. | XXXYYYZZZ6 | Withdrawal | 2021-04-23 | 23 | Friday | April | 2021 | 12:31 | 12 | 31 | Afternoon |
XXXYYYZZZ9 Confirmed. Ksh80.00 sent to MSEE WA CHIPO 0704444444 on 12/4/21 at 1:57 PM. New M-PESA balance is Ksh 210. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,665.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ9 | Customer Transfer | 2021-04-12 | 12 | Monday | April | 2021 | 1:57 | 13 | 57 | Afternoon |
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334# | XXXYYYZZZ10 | Merchant Payment | 2021-03-17 | 17 | Wednesday | March | 2021 | 6:54 | 18 | 54 | Afternoon |
XXXYYYZZZ11 Confirmed. On 25/7/20 at 3:29 PM Give Ksh2,000.00 cash to Agent fulani New M-PESA balance is Ksh2,100.22. | XXXYYYZZZ11 | Deposit | 2020-07-25 | 25 | Saturday | July | 2020 | 3:29 | 15 | 29 | Afternoon |
2.5 Transaction amount
df <- df %>%
mutate(temp = gsub("New M-PESA balance is.*", "", `Text message`)) %>%
mutate(trn_amount = str_extract(temp, "(?<=^| )Ksh.*?(?=$| )")) %>%
mutate(trn_amount = as.numeric(trimws(gsub("Ksh|Kshs|,", "",
trn_amount)))) %>%
select(-temp)
tab_output(df)
Text message | trn_code | trn_type | trn_date | trn_day | trn_dayofweek | trn_month | trn_year | trn_time | trn_hour | trn_minute | trn_timeofday | trn_amount |
---|---|---|---|---|---|---|---|---|---|---|---|---|
XXXYYYZZZ1 confirmed.You bought Ksh100.00 of airtime on 27/4/21 at 11:05 AM.New M-PESA balance is Ksh5,350.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,900.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ1 | Airtime Purchase | 2021-04-27 | 27 | Tuesday | April | 2021 | 11:05 | 11 | 5 | Morning | 100 |
XXXYYYZZZ2 confirmed.You bought Ksh30.00 of airtime on 25/4/21 at 3:45 PM.New M-PESA balance is Ksh5,450.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,940.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ2 | Airtime Purchase | 2021-04-25 | 25 | Sunday | April | 2021 | 3:45 | 15 | 45 | Afternoon | 30 |
XXXYYYZZZ3 Confirmed. Ksh500.00 sent to KPLC PREPAID for account 123456789 on 23/4/21 at 9:57 PM New M-PESA balance is Ksh5,941.22. Transaction cost, Ksh23.00. Amount you can transact within the day is 296,283.00. | XXXYYYZZZ3 | Pay Bill Payment | 2021-04-23 | 23 | Friday | April | 2021 | 9:57 | 21 | 57 | Afternoon | 500 |
XXXYYYZZZ4 Confirmed. Ksh607.00 paid to SUPERMARKET FULANI HAPO. on 23/4/21 at 3:26 PM.New M-PESA balance is Ksh6,494.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,813.00.You can now access M-PESA via *334# | XXXYYYZZZ4 | Merchant Payment | 2021-04-23 | 23 | Friday | April | 2021 | 3:26 | 15 | 26 | Afternoon | 607 |
XXXYYYZZZ5 Confirmed. Ksh1,030.00 sent to MSEE FULANI HAPO +254722222222 on 23/4/21 at 2:38 PM. New M-PESA balance is Ksh7. Transaction cost, Ksh22.00. Amount you can transact within the day is 297,420.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ5 | Customer Transfer | 2021-04-23 | 23 | Friday | April | 2021 | 2:38 | 14 | 38 | Afternoon | 1030 |
XXXYYYZZZ6 Confirmed.on 23/4/21 at 12:31 PMWithdraw Ksh1,500.00 from 010000 - Shop fulani hapo New M-PESA balance is Ksh8. Transaction cost, Ksh28.00. Amount you can transact within the day is 298,500.00. | XXXYYYZZZ6 | Withdrawal | 2021-04-23 | 23 | Friday | April | 2021 | 12:31 | 12 | 31 | Afternoon | 1500 |
XXXYYYZZZ9 Confirmed. Ksh80.00 sent to MSEE WA CHIPO 0704444444 on 12/4/21 at 1:57 PM. New M-PESA balance is Ksh 210. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,665.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ9 | Customer Transfer | 2021-04-12 | 12 | Monday | April | 2021 | 1:57 | 13 | 57 | Afternoon | 80 |
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334# | XXXYYYZZZ10 | Merchant Payment | 2021-03-17 | 17 | Wednesday | March | 2021 | 6:54 | 18 | 54 | Afternoon | 700 |
XXXYYYZZZ11 Confirmed. On 25/7/20 at 3:29 PM Give Ksh2,000.00 cash to Agent fulani New M-PESA balance is Ksh2,100.22. | XXXYYYZZZ11 | Deposit | 2020-07-25 | 25 | Saturday | July | 2020 | 3:29 | 15 | 29 | Afternoon | 2000 |
2.6 Transaction cost
df <- df %>%
mutate(trn_cost = ifelse(trn_type == "Deposit","0.00",
gsub(".*Transaction cost|. Amount you can transact.*|,|Ksh|\\.$", "",
`Text message`))) %>%
mutate(trn_cost = as.numeric(trimws(trn_cost)))
tab_output(df)
Text message | trn_code | trn_type | trn_date | trn_day | trn_dayofweek | trn_month | trn_year | trn_time | trn_hour | trn_minute | trn_timeofday | trn_amount | trn_cost |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
XXXYYYZZZ1 confirmed.You bought Ksh100.00 of airtime on 27/4/21 at 11:05 AM.New M-PESA balance is Ksh5,350.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,900.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ1 | Airtime Purchase | 2021-04-27 | 27 | Tuesday | April | 2021 | 11:05 | 11 | 5 | Morning | 100 | 0 |
XXXYYYZZZ2 confirmed.You bought Ksh30.00 of airtime on 25/4/21 at 3:45 PM.New M-PESA balance is Ksh5,450.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,940.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ2 | Airtime Purchase | 2021-04-25 | 25 | Sunday | April | 2021 | 3:45 | 15 | 45 | Afternoon | 30 | 0 |
XXXYYYZZZ3 Confirmed. Ksh500.00 sent to KPLC PREPAID for account 123456789 on 23/4/21 at 9:57 PM New M-PESA balance is Ksh5,941.22. Transaction cost, Ksh23.00. Amount you can transact within the day is 296,283.00. | XXXYYYZZZ3 | Pay Bill Payment | 2021-04-23 | 23 | Friday | April | 2021 | 9:57 | 21 | 57 | Afternoon | 500 | 23 |
XXXYYYZZZ4 Confirmed. Ksh607.00 paid to SUPERMARKET FULANI HAPO. on 23/4/21 at 3:26 PM.New M-PESA balance is Ksh6,494.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,813.00.You can now access M-PESA via *334# | XXXYYYZZZ4 | Merchant Payment | 2021-04-23 | 23 | Friday | April | 2021 | 3:26 | 15 | 26 | Afternoon | 607 | 0 |
XXXYYYZZZ5 Confirmed. Ksh1,030.00 sent to MSEE FULANI HAPO +254722222222 on 23/4/21 at 2:38 PM. New M-PESA balance is Ksh7. Transaction cost, Ksh22.00. Amount you can transact within the day is 297,420.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ5 | Customer Transfer | 2021-04-23 | 23 | Friday | April | 2021 | 2:38 | 14 | 38 | Afternoon | 1030 | 22 |
XXXYYYZZZ6 Confirmed.on 23/4/21 at 12:31 PMWithdraw Ksh1,500.00 from 010000 - Shop fulani hapo New M-PESA balance is Ksh8. Transaction cost, Ksh28.00. Amount you can transact within the day is 298,500.00. | XXXYYYZZZ6 | Withdrawal | 2021-04-23 | 23 | Friday | April | 2021 | 12:31 | 12 | 31 | Afternoon | 1500 | 28 |
XXXYYYZZZ9 Confirmed. Ksh80.00 sent to MSEE WA CHIPO 0704444444 on 12/4/21 at 1:57 PM. New M-PESA balance is Ksh 210. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,665.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ9 | Customer Transfer | 2021-04-12 | 12 | Monday | April | 2021 | 1:57 | 13 | 57 | Afternoon | 80 | 0 |
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334# | XXXYYYZZZ10 | Merchant Payment | 2021-03-17 | 17 | Wednesday | March | 2021 | 6:54 | 18 | 54 | Afternoon | 700 | 0 |
XXXYYYZZZ11 Confirmed. On 25/7/20 at 3:29 PM Give Ksh2,000.00 cash to Agent fulani New M-PESA balance is Ksh2,100.22. | XXXYYYZZZ11 | Deposit | 2020-07-25 | 25 | Saturday | July | 2020 | 3:29 | 15 | 29 | Afternoon | 2000 | 0 |
2.7 M-Pesa balance
df <- df %>%
mutate(temp = gsub(".*New M-PESA balance is", "", `Text message`),
temp = gsub("Transaction cost.*|\\..*", "", temp)) %>%
mutate(trn_balance = as.numeric(trimws(gsub("Ksh|Kshs|,|\\.", "", temp)))) %>%
select(-temp)
tab_output(df)
Text message | trn_code | trn_type | trn_date | trn_day | trn_dayofweek | trn_month | trn_year | trn_time | trn_hour | trn_minute | trn_timeofday | trn_amount | trn_cost | trn_balance |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
XXXYYYZZZ1 confirmed.You bought Ksh100.00 of airtime on 27/4/21 at 11:05 AM.New M-PESA balance is Ksh5,350.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,900.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ1 | Airtime Purchase | 2021-04-27 | 27 | Tuesday | April | 2021 | 11:05 | 11 | 5 | Morning | 100 | 0 | 5350 |
XXXYYYZZZ2 confirmed.You bought Ksh30.00 of airtime on 25/4/21 at 3:45 PM.New M-PESA balance is Ksh5,450.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,940.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ2 | Airtime Purchase | 2021-04-25 | 25 | Sunday | April | 2021 | 3:45 | 15 | 45 | Afternoon | 30 | 0 | 5450 |
XXXYYYZZZ3 Confirmed. Ksh500.00 sent to KPLC PREPAID for account 123456789 on 23/4/21 at 9:57 PM New M-PESA balance is Ksh5,941.22. Transaction cost, Ksh23.00. Amount you can transact within the day is 296,283.00. | XXXYYYZZZ3 | Pay Bill Payment | 2021-04-23 | 23 | Friday | April | 2021 | 9:57 | 21 | 57 | Afternoon | 500 | 23 | 5941 |
XXXYYYZZZ4 Confirmed. Ksh607.00 paid to SUPERMARKET FULANI HAPO. on 23/4/21 at 3:26 PM.New M-PESA balance is Ksh6,494.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,813.00.You can now access M-PESA via *334# | XXXYYYZZZ4 | Merchant Payment | 2021-04-23 | 23 | Friday | April | 2021 | 3:26 | 15 | 26 | Afternoon | 607 | 0 | 6494 |
XXXYYYZZZ5 Confirmed. Ksh1,030.00 sent to MSEE FULANI HAPO +254722222222 on 23/4/21 at 2:38 PM. New M-PESA balance is Ksh7. Transaction cost, Ksh22.00. Amount you can transact within the day is 297,420.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ5 | Customer Transfer | 2021-04-23 | 23 | Friday | April | 2021 | 2:38 | 14 | 38 | Afternoon | 1030 | 22 | 7 |
XXXYYYZZZ6 Confirmed.on 23/4/21 at 12:31 PMWithdraw Ksh1,500.00 from 010000 - Shop fulani hapo New M-PESA balance is Ksh8. Transaction cost, Ksh28.00. Amount you can transact within the day is 298,500.00. | XXXYYYZZZ6 | Withdrawal | 2021-04-23 | 23 | Friday | April | 2021 | 12:31 | 12 | 31 | Afternoon | 1500 | 28 | 8 |
XXXYYYZZZ9 Confirmed. Ksh80.00 sent to MSEE WA CHIPO 0704444444 on 12/4/21 at 1:57 PM. New M-PESA balance is Ksh 210. Transaction cost, Ksh0.00. Amount you can transact within the day is 296,665.00. SAFARICOM ONLY CALLS YOU FROM 0722000000. To reverse, forward this message to 456. | XXXYYYZZZ9 | Customer Transfer | 2021-04-12 | 12 | Monday | April | 2021 | 1:57 | 13 | 57 | Afternoon | 80 | 0 | 210 |
XXXYYYZZZ10 Confirmed. Ksh700.00 paid to GEL NAIL SHOP. on 17/3/21 at 6:54 PM.New M-PESA balance is Ksh3,646.22. Transaction cost, Ksh0.00. Amount you can transact within the day is 299,300.00.You can now access M-PESA via *334# | XXXYYYZZZ10 | Merchant Payment | 2021-03-17 | 17 | Wednesday | March | 2021 | 6:54 | 18 | 54 | Afternoon | 700 | 0 | 3646 |
XXXYYYZZZ11 Confirmed. On 25/7/20 at 3:29 PM Give Ksh2,000.00 cash to Agent fulani New M-PESA balance is Ksh2,100.22. | XXXYYYZZZ11 | Deposit | 2020-07-25 | 25 | Saturday | July | 2020 | 3:29 | 15 | 29 | Afternoon | 2000 | 0 | 2100 |