1

Starting on my first home-project in R and am having trouble with reading my data frame for future algorithm construction. In this instance, I simply want to loop through the data frame and store the data from each observation (rows) into a vector or similar data structure. Each data structure will be composed of a string and some integers and doubles. The caveat I am facing is I want each data structure I build to be named in association with the string that it contains. So as you can see from the image of my data frame, each time I loop through, I want to create a structure named for each Stats$teamName, containing all of the proceeding data in the observation.Image of Data Frame

So after looping through the first observation, I want a structure named "Virginia" to be made that looks like this: ("Virginia", 2, 1, 1, 2) and so on for each row.

  • 1
    It's not difficult with [assign](https://stat.ethz.ch/R-manual/R-devel/library/base/html/assign.html), but why do you want to do that? It's usually not a very good idea to loop through observations, or to pollute the global namespace with one variable per observation of a dataset. You can do for instance `for (i in seq_len(nrow(df)) assign(df[i, "teamName"], df[i, ])` where `df` is to be replaced with the name of your dataframe. Each variable will hold a dataframe with one row. It's equivalent to a list: if you prefer a list, store `as.list(df[i, ])` instead of just `df[i, ]`. –  Dec 25 '20 at 20:44
  • 1
    Thanks for the speedy response. My goal is to have it set up so each variable in the data frame is associated with the team name at the beginning of the observation so that later on I could write a line of code that will, for example: finds the quotient of mmRank / apiRank for each team in the data frame. I could be overthinking my approach, not sure. I am relatively new to R! – Price Nimmich Dec 25 '20 at 20:49
  • 3
    Well, if you only want to compute `mmRank / apiRank` for each team, the simplest is `df <- within(df, quotient <- mmRank / apiRank)`, or in tidyverse dialect, `df %<>% mutate(quotient = mmRank / apiRank)` (that is, add a variable to your dataset to hold the quotient). You seem to be overthinking it, indeed. –  Dec 25 '20 at 21:15
  • Please don't post screenshots of data. Instead, provide `dput` output or a data frame definition, in code, that others can copy into local environments to demonstrate a solution. It's also good to provide expected output (actual output, not a description) - that way answers can compare against an exact target result. – andrew_reece Dec 25 '20 at 21:16
  • [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with, including a workable sample of data – camille Dec 25 '20 at 21:54

1 Answers1

1

If you need an object per team you can use split() and then store each dataframe in a list. It is better to keep data in the list, and if it is necessary to have individual dataframes, you can use list2env(). Here an example:

#Data
df <- data.frame(teamName=c('Virginia','Duke','UNC'),
                 conferenceRank=c(2,3,1),
                 mmRank=c(1,1,1),
                 apiRank=c(1,3,7),
                 rankOfConference=c(2,2,2),
                 stringsAsFactors = F)
#Create objects per team
L <- split(df,df$teamName)
#set to envir
list2env(L,envir = .GlobalEnv)

The object L will look like this:

L
$Duke
  teamName conferenceRank mmRank apiRank rankOfConference
2     Duke              3      1       3                2

$UNC
  teamName conferenceRank mmRank apiRank rankOfConference
3      UNC              1      1       7                2

$Virginia
  teamName conferenceRank mmRank apiRank rankOfConference
1 Virginia              2      1       1                2
Duck
  • 39,058
  • 13
  • 42
  • 84