-1

I am trying to take my data frame that has a list of player id numbers and find their name, using this function. Right now my code will simply print separate tibbles of each result, but I want it to combine those results into a data frame. I tried using rbind, but it doesn't work.

for(x in dataframe...)
  print(function I am using to find name)
  • Claire, welcome to SO! Do you mean you want to add the players' names to the `dataframe` based on your `$playerid` field? – r2evans Jun 05 '20 at 17:47
  • (BTW: caution with that package, it's operating in side-effect and changing your global environment. While it may be inoccuous, it's bad-practice to do that.) – r2evans Jun 05 '20 at 17:49
  • You might be able to do `bind_rows(map(dataframe$playerid, baseballr:playername_lookup))`. – r2evans Jun 05 '20 at 17:50
  • If you want more help, though, it really helps (for questions like this) when the question is self-contained and reproducible. By that I mean including sample representative data (perhaps via `dput(head(dataframe))` or building data programmatically (e.g., `data.frame(...)`), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jun 05 '20 at 17:51

2 Answers2

0

You can initialise a results data frame like this

results <- data.frame()

You can then add the results in each loop using rbind combining the previous version with the new results. In the first iteration of the loop you add your first results to an empty data frame. So combined

results <- data.frame()

for(x in dataframe$playerid){
  results <- rbind(results, baseballr::playername_lookup(x))
}

The problem in you code was that you simply printed the results without saving them anywhere.

As mentioned in the comment below, the better way to do this, once your data set becomes very large, is to create a list an later combine that to a data.frame.

results <- list()

for(i in seq_len(nrow(dataframe))){
  results[[i]] <- baseballr::playername_lookup(dataframe$playerid[i])
}
final_results <- do.call(rbind, results)
SBFin
  • 352
  • 1
  • 9
  • 1
    Iteratively `rbind`ing rows in R "works" but it scales *horribly*, see Chapter 2: *Growing Objects* of the [R Inferno](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf). Much better to build a `list` of frames and combine them in one call (either `do.call(rbind, list_of_frames)` or `bind_rows(list_of_frames)`). – r2evans Jun 05 '20 at 17:53
  • Maybe something for meta but how do you answer a question like this. Sure, you can provide a function that works and can be copied and pasted or you try to point out why the solution did not work and "fix" it. Ideally both, right? – SBFin Jun 05 '20 at 18:07
  • 1
    It's very difficult to be confident with an answer without representative data, certainly. Often it's enough to give a nudge in one direction, many programmers can take the incomplete or imperfect-fit answer and adapt (given clues and data that we don't have). I try to answer when I can, but I also encourage askers with clear guidance (such as links to popular "good questions" hints, such as https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info). After that, it's a horse-to-water thing, and I feel I can shrug it off and hope they can get their help somewhere. – r2evans Jun 05 '20 at 18:12
0

Use sapply which is more efficient than looping :

results <- data.frame(name = sapply(dataframe[,'playerid'], FUN = function(id) baseballr::playername_lookup(id)))
Waldi
  • 39,242
  • 6
  • 30
  • 78