Reputation: 355
I've got a dataframe with column names that include week and year designators of the format "W1_2019" plus other text. The complete dataframe contains 52 weeks worth of 5 columns each. My goal is to take the following code, which does exactly what I want it to do for weeks 1 & 2, and put it into a loop for x=1 to 52 so I don't have to use 52 times the same half a dozen lines.
eidsr <- dget(file="test1.txt")
mode_xmt <- data.frame(District=eidsr$district) #Initializes dataframe mode_xmt with only 1 column containing District names
wtmp <- select(eidsr, contains("W1_2019"))
wtmp$mode <- "NoRep"
wtmp$mode[wtmp$W1_2019_EIDSR_Total_Malaria_cases>0] <- "Report"
wtmp$mode[wtmp$`W1_2019_EIDSR-Mobile_SMS`==1] <- "Mobile_SMS"
wtmp$mode[wtmp$`W1_2019_EIDSR-Mobile_Internet`==1] <- "Mobile_Internet"
#At this point the dataframe wtmp looks like the example below.
mode_xmt$`2019_W1` <- wtmp$mode #Appends ONLY the W1_2019 column to mode_xmt
rm(wtmp)
wtmp <- select(eidsr, contains("W2_2019"))
wtmp$mode <- "NoRep"
wtmp$mode[wtmp$W2_2019_EIDSR_Total_Malaria_cases>0] <- "Report"
wtmp$mode[wtmp$`W2_2019_EIDSR-Mobile_SMS`==1] <- "Mobile_SMS"
wtmp$mode[wtmp$`W2_2019_EIDSR-Mobile_Internet`==1] <- "Mobile_Internet"
mode_xmt$`2019_W2` <- wtmp$mode
rm(wtmp)
At the end of each operation, my working data are as follows. Dataframe wtmp looks like this:
`W1_2019_EIDSR-Timely_~ W1_2019_EIDSR_Total_Mala~ W1_2019_EIDSR_Date_R~ `W1_2019_EIDSR-Mobile_~ `W1_2019_EIDSR-Mobi~ mode
<dbl> <dbl> <chr> <dbl> <dbl> <chr>
1 NA 0 NA NA NA NoRep
2 NA NA NA NA NA NoRep
3 NA 51 NA NA NA Repo~
4 NA NA NA NA NA NoRep
5 NA 64 NA NA NA Repo~
6 NA 86 NA NA NA Repo~
7 NA 92 NA NA NA Repo~
8 NA 47 NA NA NA Repo~
9 NA 46 NA NA NA Repo~
10 NA 35 NA NA NA Repo~
mode_xmt, with the new column appended, looks like this:
District 2019_W01
1 Bo NoRep
2 Bo NoRep
3 Bo Report
4 Bo NoRep
5 Bo Report
6 Bo Report
7 Bo Report
8 Bo Report
9 Bo Report
10 Bo Report
And once I've done the second iteration for W2, mode_xmt looks like this:
District 2019_W01 2019_W02
1 Bo NoRep Report
2 Bo NoRep NoRep
3 Bo Report Report
4 Bo NoRep NoRep
5 Bo Report Report
6 Bo Report Report
7 Bo Report Report
8 Bo Report Report
9 Bo Report Report
10 Bo Report Report
Lather, rinse, repeat. Times 52. And as @DS_UNI has observed, while separate columns for week and year would be nice, they would defeat the ultimate purpose which is a time-series that stretches over more than one year ... but to keep myself from going completely nuts I'd just be happy if I could iterate the 52 weeks of a single year.
As I said, the above code works. I'm just looking for a way to loop it rather than repeating it ad nauseum.
Here's the text of a dput on the truncated data (save as test1.txt in your working directory):
structure(list(`W1_2019_EIDSR-Timely_Report` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), W1_2019_EIDSR_Total_Malaria_cases = c(0, NA, 51, NA, 64, 86, 92, 47, 46, 35, 33, NA, NA, 77, 35, 7, 24, 27, 14, 72), W1_2019_EIDSR_Date_Received = c(NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_), `W1_2019_EIDSR-Mobile_Internet` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `W1_2019_EIDSR-Mobile_SMS` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `W2_2019_EIDSR-Timely_Report`
= c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), W2_2019_EIDSR_Total_Malaria_cases = c(55, NA, 44, NA, 38, 26, 29, 40, 59, 18, 48, NA, NA, 37, 34, 51, 34, 38, 13, 56), W2_2019_EIDSR_Date_Received = c(NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_), `W2_2019_EIDSR-Mobile_Internet` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `W2_2019_EIDSR-Mobile_SMS` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), district = c("Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo", "Bo")), .Names = c("W1_2019_EIDSR-Timely_Report", "W1_2019_EIDSR_Total_Malaria_cases", "W1_2019_EIDSR_Date_Received", "W1_2019_EIDSR-Mobile_Internet", "W1_2019_EIDSR-Mobile_SMS", "W2_2019_EIDSR-Timely_Report", "W2_2019_EIDSR_Total_Malaria_cases", "W2_2019_EIDSR_Date_Received", "W2_2019_EIDSR-Mobile_Internet", "W2_2019_EIDSR-Mobile_SMS", "district"), row.names = c(NA, -20L ), class = c("tbl_df", "tbl", "data.frame"))
Upvotes: 0
Views: 65
Reputation: 2650
Your data should look something like this (I would also prefer to have a column for week and a column for year). And most probably there's a way to manipulate to get what you want.
library(dplyr)
library(reshape2)
eidsr %>%
# values should be in a column (not in headers)
melt(id.var = 'district') %>%
# extract the new variables
mutate(week_year = substr(variable, 1, 7),
variable = sub(".*EIDSR[- _]", "", variable)) %>%
# assuming missing values don't have a specific meaning you can just remove them
na.omit()
# district variable value week_year
# 21 Bo Total_Malaria_cases 0 W1_2019
# 23 Bo Total_Malaria_cases 51 W1_2019
# 25 Bo Total_Malaria_cases 64 W1_2019
# 26 Bo Total_Malaria_cases 86 W1_2019
# 27 Bo Total_Malaria_cases 92 W1_2019
# 28 Bo Total_Malaria_cases 47 W1_2019
# 29 Bo Total_Malaria_cases 46 W1_2019
# 30 Bo Total_Malaria_cases 35 W1_2019
I can see that you're loosing your patience, so if you MUST use a loop you should use one of the apply functions, and for those you need a function to repeatedly apply on a vector or a list:
wacky_fun <- function(x_chr){
malaria_col <- paste0(x_chr, '_EIDSR_Total_Malaria_cases')
sms_col <- paste0(x_chr, '_EIDSR-Mobile_SMS')
internet_col <- paste0(x_chr, '_EIDSR-Mobile_Internet')
mode_col <- rep("NoRep", nrow(eidsr))
mode_col[eidsr[malaria_col]>0] <- "Report"
mode_col[eidsr[sms_col]==1] <- "Mobile_SMS"
mode_col[eidsr[internet_col]==1] <- "Mobile_Internet"
return(mode_col)
}
We'll apply the function on all the weeks in the data
# get the unique weeks in the headers
weeks <- names(eidsr)[grepl('W[[:digit:]]_[[:digit:]]{4}', names(eidsr))] %>%
substr(1, 7) %>%
unique()
# apply the function on all the weeks, bind them with the district, and convert to data.frame
cbind('district' = eidsr$district, sapply(weeks, wacky_fun)) %>%
as.data.frame()
Upvotes: 1