简体   繁体   中英

How to write a function to create multiple dataframes based on the values of a variable?

I have a dataframe which contains a variable called ProgramName which contains names of different programs but sometimes this names change. I want to create a function that creates a dataframe for each program name. Similar to what the subset function does.

There are other characteristics in the dataframe that also need to be filter so the following code takes care of that

ma <- as.data.frame(Missing_AttendanceKIDS, stringsAsFactors = FALSE)
mi <- as.data.frame(Missing_AttendanceSIS, stringsAsFactors = FALSE)
mi <- mi %>% select(c("ProgramName", "FnLnDOB", "Funding", "Missing     Days"))
ma$ProgramName <- as.character(ma$ProgramName)
ma <- ma[!(ma$Funder == "School Board (FL)" & ma$Issue == "non-existent"), ]
HaveFirstTermCode <- ma %>% subset(FirstTermCodeDate != "NULL")
ma <- ma[!(ma$FirstTermCodeDate != "NULL"), ]
InvalidTermCode <- ma %>% subset(Issue == "invalid term code")
ma <- ma[!(ma$Issue == "invalid term code"), ]
Blkgenerator <- ma %>% subset(ma$Issue == "non-existent" & ma$Funder != "School Board (FL)")
ma <- ma[!(ma$Issue == "non-existent" & ma$Funder != "School Board (FL)"), ]

ma$ProgramName <- as.factor(ma$ProgramName)

The code below is what I would like the function to do. If possible also give the result dataframe the name of the program..

Missing_Attendance_Acadiana <- subset(ma, ma$ProgramName == "Acadiana" & ma$Issue == "blank")
Missing_Attendance_Alabama <- subset(ma, ma$ProgramName == "Alabama Family Services" & ma$Issue == "blank")
Missing_Attendance_Beaufort <- ma[ma$ProgramName %like% "Beaufort" & ma$Issue == "blank", ]
Missing_Attendance_Piedmont <- ma[ma$ProgramName %like% "Piedmont" & ma$Issue == "blank", ]

After I get the dataframes I am running the following function to create an html with the dataframe in it using R Markdown.

tablemu = function(df) kable(df, row.names = FALSE, caption = "Missing Attendance") %>%
kable_styling(bootstrap_options = "striped", position = "center", full_width = T, fixed_thead = list(enabled = T, background = "Orange"), stripe_color =    list(enabled = T, background = "yellow") ) %>%
column_spec(1:4, bold = T, border_left = T, border_right = T)

Simply use split which creates a named list of data frames from the factor(s), then run it through your needed method with lapply .

sub <- ma[ma$Issue == "blank",]
df_list <- split(sub, sub$ProgramName)

lapply(df_list, tablemu)

Alternatively, use by (object-oriented wrapper to tapply ) to streamline the two above calls. Below will output a printed list of your HTML outputs:

sub <- ma[ma$Issue == "blank",]
by(sub, sub$ProgramName, tablemu)

No need to flood your global environment with many variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM