0

Why does this code print only numbers to the console? How can I get a list with names 1 through 13 and contents a,n ; b,o; et cetera?

aggregate(letters, by = data.frame(rep(1:13, 2)), head, simplify = F)

I'm using R 3.5.1.

I haven't found a duplicate yet. Here is what I have found so far on similar topics.

A similar but not identical question:

R Aggregate FUN=head

An apparently similar question that is actually about aggregating numbers:

R: Aggregate character strings

eric_kernfeld
  • 495
  • 5
  • 17

1 Answers1

1

letters is coerced to a dataframe. It becomes a factor. Factors are stored as integers in R. If the levels are lost somewhere, they often start to just look like integers. Try this instead:

aggregate(data.frame(letters, stringsAsFactors = F), by = data.frame(rep(1:13, 2)), head, simplify = F)

eric_kernfeld
  • 495
  • 5
  • 17
  • The documentation says "If simplify is true, summaries are simplified to vectors or matrices if they have a common length of one or greater than one, respectively; otherwise, lists of summary results according to subsets are obtained." The result of this command is a list, technically, but it is not a list according to subsets because it has length 2 rather than the expected length of 13. This answer would be better if it could explain that as well. – eric_kernfeld Apr 20 '19 at 12:50
  • Actually, the result does contain a list of length 13. It is the second column of the returned dataframe. – eric_kernfeld Apr 20 '19 at 13:05