r - Iterating a list of data frames through a function -
i have written function strip down data frame contain columns want plot. want iterate list of data frames through function, each individual data frame contains info relevant plot.
here function:
clean_data <- function(show_df){ show_data <- show_df[,c(1:2,7)] colnames(show_data) <- c("week", "weeklygross", "avgticketprice") #turns weeklygross numeric values show_data$weeklygross <- gsub('[^a-za-z0-9.]', '', show_data$weeklygross) show_data$weeklygross <- as.numeric(show_data$weeklygross) #turns avgticketprice numeric values show_data$avgticketprice <- gsub('[^a-za-z0-9.]', '', show_data$avgticketprice) show_data$avgticketprice <- as.numeric(show_data$avgticketprice) show_data }
and here code when attempt iterate list of data frames through function:
df.list <- list(atw_df, cly_df, gent_df, kin_df, mo_df,on_df, van_df, war_df) new_list <- list() (i in seq(df.list)){ new_list <- clean_data(i) }
i know loop missing something, cannot figure out what. want store each data frame list in it's revised format variable can use them plot information.
edit: made code changes, receiving incorrect number of dimensions error in show_df[, c(1:2, 7)]
edit2: more changes made loop, still receiving same error message.
once have function, , list, do
new_list <- lapply(df.list, clean_data)
which call clean_data
once each data frame in df.list
, return list of newly cleaned data frames.
thus entire "loop" becomes
df.list <- list(atw_df, cly_df, gent_df, kin_df, mo_df,on_df, van_df, war_df) new_list <- lapply(df.list, clean_data)
Comments
Post a Comment