0

Super new to R - like, started yesterday new. I'm trying to group and sort a data set and it seems as though samples like:

aggdata <- aggregate(mtcars, by = list(cyl, vs), FUN = mean, na.rm = TRUE)

Only work with numeric data. I've also tried working off this example: R Dataframe: aggregating strings within column, across rows, by group to build something new for my purpose but it's not giving me the output I need.

For the example, I have a data set that would look like:

Name       DateTime
Jan        2017-05-26T12:38:50.537
Tim        2017-05-23T11:52:40.423
Rob        2017-05-24T10:52:40.423
Jan        2017-05-25T10:38:50.537
Tim        2017-05-19T11:52:40.423
Rob        2017-05-19T11:52:40.423
Jan        2017-05-22T12:38:50.537
Tim        2017-05-23T11:52:40.423
Rob        2017-05-20T11:52:40.423

I would like to group the data by name then sort the groups by time so the end result would be:

Name          DateTime
Jan           2017-05-22T12:38:50.537
Jan           2017-05-25T10:38:50.537
Jan           2017-05-26T12:38:50.537
Rob           2017-05-19T11:52:40.423
Rob           2017-05-20T11:52:40.423
Rob           2017-05-24T10:52:40.423
Tim           2017-05-19T11:52:40.423
Tim           2017-05-23T11:50:40.423
Tim           2017-05-23T11:52:40.423

After grouping is successful, this sample for datetime seems promising Ordering date/time in descending order in R

In any case, if someone in the know could point me in the direction of an example that groups by string or alphanumeric I'd greatly appreciate it.

alistaire
  • 42,459
  • 4
  • 77
  • 117

1 Answers1

1

You can use the dplyr library:

install.packages("dplyr")
require(dplyr)

Use arrange(data, Name, DateTime) to order by Name, and within name by DateTime.

Julian Zucker
  • 564
  • 4
  • 13
  • Forgot about arrange vs arrange_. Fixed. – Julian Zucker Jun 07 '17 at 16:36
  • I'm using this in Azure ML Studio and it's not liking the install.packages, but the dat[order(dat$Name, dat$Datetime),] worked. thank you for taking the time to answer, I appreciate it. – Vito DiMercurio Jun 07 '17 at 17:49
  • In Azure ML Studio you can use `install.packages(“src/pack/pac.zip”, lib = “.”, repos = NULL, verbose = TRUE) library(pac, lib.loc=”.”, verbose=TRUE)` to install a package, after changing the directory the package is located. – Julian Zucker Jun 08 '17 at 02:42