0

For example, with a function that returns a data frame like aggregate, you can do this:

aggregate(data, subset, mean)[, "row"]

And you can access the columns of the rows this way.

How would you access specific data points, without doing something like this?

df <- aggregate(data, ....)
df[df$column == max(df$column), df$row] # returns row w/ max column value

There seems to be no way to do this without having the name of the data frame to reference.

Attempting something like this doesn't seem to work:

aggregate(data, subset, mean)[aggregate(data, subset, mean)$column == 
max(aggregate(data, subset,mean), "row"] 

and it's also pretty ugly code so I'd rather not even if I could.

user3450277
  • 436
  • 6
  • 24
  • technically you could maybe try `within()`, but i didnt really get why you want to do that,... – Tonio Liebrand Apr 20 '17 at 19:06
  • 3
    Please provide a reproducible example. At first line `df` is a function, not a variable that could be subsetable – gonzalez.ivan90 Apr 20 '17 at 19:07
  • Oh, sorry. That's definitely confusing. – user3450277 Apr 20 '17 at 19:11
  • @BigDataScientist How would you use `within()` for this context? Wouldn't that require deleting most of the data since it returns the modified data frame? – user3450277 Apr 20 '17 at 19:14
  • @BigDataScientist Also seems like you still wouldn't be able to make a comparison like `df$column == max(df$column)` without having the data frame assigned to reference – user3450277 Apr 20 '17 at 19:16
  • Side note: though I'm guilty as well (specifically `df`), using R function names as variables makes it both confusing to readers as well as problematic if you do not sufficiently define all necessary variables. For instance, if you forget to define `data`, then `aggregate(data, ...)` may eventually give you `Error: object of type 'closure' is not subsettable`, because in that instance `data` is a *function* (of length 1, so you'll also get complaints about `arguments must have same length`). This comment quickly infringes on personal naming preferences, so is just a technique/suggestion. – r2evans Apr 20 '17 at 19:32
  • (That was more an expansion of @gonzalez.ivan90's comment.) – r2evans Apr 20 '17 at 19:32
  • you didnt answer my remark, but i guess in the end this answers yourquestion: http://stackoverflow.com/questions/29586382/r-subset-data-frame-from-max-value-of-one-vector-and-grouped-by-another – Tonio Liebrand Apr 20 '17 at 19:34

0 Answers0