@@ -235,19 +235,21 @@ fetch_ucirepo <- function(name, id) {
235235 metadata $ variables <- NULL
236236
237237 # Organize variables into IDs, features, or targets
238- variables_by_role <- list (
239- ID = list (),
240- Feature = list (),
241- Target = list (),
242- Other = list ()
243- )
244- for (variable in variables ) {
245- if (! (variable $ role %in% names(variables_by_role ))) {
246- stop(' Role must be one of "ID", "Feature", "Target", or "Other"' )
238+ # First, ensure all roles are valid
239+ if (! all(sapply(variables , function (v ) v $ role %in% c(" ID" , " Feature" , " Target" , " Other" )))) {
240+ stop(' Role must be one of "ID", "Feature", "Target", or "Other".' )
241+ }
242+
243+ # Create the list of variables by role
244+ variables_by_role <- lapply(
245+ c(" ID" , " Feature" , " Target" , " Other" ),
246+ function (role ) {
247+ trimws(sapply(variables [sapply(variables , function (v ) v $ role == role )], `[[` , " name" ))
247248 }
249+ )
248250
249- variables_by_role [[ variable $ role ]] <- c( variables_by_role [[ variable $ role ]], trimws( variable $ name ))
250- }
251+ # Name the list elements
252+ names( variables_by_role ) <- c( " ID " , " Feature " , " Target " , " Other " )
251253
252254 # Extract dataframes for each variable role
253255 ids_df <- if (length(variables_by_role $ ID ) > 0 ) df [ , unlist(variables_by_role $ ID ), drop = FALSE ] else NULL
0 commit comments