stringr - How to replace column in a dynamically-named data frame in R -
i'm working on stuff fantasy football league. populate series of data frames, each named position (e.g., qb quarterbacks, rb running backs, etc.)
i've been able use apply() function lot rbinding separate chunks of position single dataframe, i'd strip out text each position.
so have dataframe named qb, , 1 of columns called player, has superfluous text i'd remove.
so, if this, works:
theposnick <- "qb" theplayer <- paste(theposnick, "$player", sep="") g <- str_replace(eval(parse(text=theplayer)), "remove text", "") g <- str_replace(g, "more meaningless text", "") g <- str_trim(g) #expected behavior: qb$player <- g
that works fine, however, i'd love able dynamically, , doing following doesn't seem work:
theposnick <- "qb" theplayer <- paste(theposnick, "$player", sep="") g <- str_replace(eval(parse(text=theplayer)), "remove text", "") g <- str_replace(g, "more meaningless text", "") g <- str_trim(g) #but i'd love able this: assign(theplayer, value=g)
can tell me i'm doing wrong? suggestions appreciated, thanks!
first, i'd make function fixing.
fixit <- function(g) { g <- str_replace(g, "remove text", "") g <- str_replace(g, "more meaningless text", "") g <- str_trim(g) g }
then get
variable want, fix it, , assign
back.
theposnick <- "qb" x <- get(theposnick) x$player <- fixit(x$player) assign(theposnick, x)
alternatively, more r idiomatic way use list store positions instead of storing them in global environment. use lapply fix them @ once.
positions <- list(qb=qb, rb=rb, te=te) positions <- lapply(positions, function(x) { x$player <- fixit(x$player) } )
Comments
Post a Comment