1

I'm working on some stuff for my fantasy football league. I populate a series of data frames, each named for the position (e.g., QB for quarterbacks, RB for running backs, etc.)

I've been able to use the apply() function to do a lot with rbinding the separate chunks of a position into a single dataframe, but what I'd really like to do is to strip out some text from something for each position.

So I have a dataframe named QB, and one of the columns is called Player, that has some superfluous text I'd like to remove.

So, if I were to do this, it works:

thePosNick <- "QB"
thePlayer <- paste(thePosNick, "$Player", sep="")
g <- str_replace(eval(parse(text=thePlayer)), "remove this text", "")
g <- str_replace(g, "more meaningless text", "")
g <- str_trim(g)
#expected behavior:
QB$Player <- g

That works fine, however, I'd love to be able to do this dynamically, and doing the following doesn't seem to work:

thePosNick <- "QB"
thePlayer <- paste(thePosNick, "$Player", sep="")
g <- str_replace(eval(parse(text=thePlayer)), "remove this text", "")
g <- str_replace(g, "more meaningless text", "")
g <- str_trim(g)
#but i'd love to be able to do something like this:
assign(thePlayer, value=g)

Can anyone tell me what I'm doing wrong? Any suggestions are greatly appreciated, thanks!

Frank
  • 66,179
  • 8
  • 96
  • 180
Maashu
  • 305
  • 2
  • 10
  • Is it the same text for each player? You could look at `gsub`. It would also help a lot if you created a small set of example data to illustrate the input and output. – dayne Sep 24 '13 at 15:22
  • 2
    Make your life easier and put all of your data.frames in a list. You can then use convenient tools like `lapply` and don't need the `assign`/`get`/`eval(parse())` nightmare. – Roland Sep 24 '13 at 15:39

4 Answers4

4

First, I'd make a function to do your fixing.

fixit <- function(g) {
  g <- str_replace(g, "remove this text", "")
  g <- str_replace(g, "more meaningless text", "")
  g <- str_trim(g)
  g
}

Then get the variable you want, fix it, and assign back.

thePosNick <- "QB"
x <- get(thePosNick)
x$Player <- fixit(x$Player)
assign(thePosNick, x)

Alternatively, a more R idiomatic way would be to use a list to store all your positions instead of just storing them in the global environment. Then you could use lapply to fix them all at once.

positions <- list(QB=QB, RB=RB, TE=TE)
positions <- lapply(positions, function(x) { x$Player <- fixit(x$Player) } )
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
3

Store your stuff like this?

biglist = list(QB=list(Player='yo remove this text here'))

# $QB
# $QB$Player
# [1] "yo remove this text here"

Then

g <- biglist[[thePosNick]][['Player']]
biglist[[thePosNick]][['Player']] <- str_replace(g, "remove this text", "")

# $QB
# $QB$Player
# [1] "yo  here"

That is, you can put names of things inside [[]]. You're only encountering trouble assigning to stuff in the global environment (as opposed to nested in a list), I reckon.

Comment. As @Roland said, you'd be better of yet storing this in a data.frame or data.table, perhaps with one row per player: DF <- data.frame(Player='You',Pos='QB',Team='Youse',...)

Frank
  • 66,179
  • 8
  • 96
  • 180
1

You must use

eval(parse(text=paste(thePlayer,"<-",deparse(g))))

instead of assign(thePlayer, value=g).

Why? Because QB$Player <- g is not a direct assignment, but a call to the function '$<-', which places the value in g in a special position of the object QB. There is no object QB$Player in your environment, that is why assign() fails.

Opinion:

This is all pretty bad because the better solution is to use a list of dataframes like this:

L <- list(QB=QB, RB=RB, ...)

g <- str_replace(L[["QB"]]$Player, "remove this text", "")
g <- str_replace(g, "more meaningless text", "")
g <- str_trim(g)
L[["QB"]]$Player <- g
Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69
0

One quick and dirty solution would be

eval(parse(text=paste(thePlayer,"<-g",sep="")))

I can't help but think, though, manipulating these objects by their names as strings might not be the cleanest way to go about this whole problem.

mrip
  • 14,913
  • 4
  • 40
  • 58
  • That's why I called it "quick and dirty". The `eval(parse())` structure is also used in the original question. – mrip Sep 24 '13 at 15:42