问题描述:

I have a data.frame in R, myTable consisting of a date , myDate; integer, myCount and factor ,myCategory. It is sorted by date

I need to create a list from the data.frame (in order to plot a Highcharts graph in rCharts)

mySeries <- lapply(split(myTable, myTable$myCategory), function(x) {

res <- lapply(split(x, rownames(x)), as.list)

names(res) <- NULL

return(res)

})

However, the resulting list is not ordered by myDate within myCategory. How can this be achieved?

myTable shown below

TIA

myTable <- structure(list(myDate = structure(c(15642, 15649, 15656, 15663,

15670, 15670, 15677, 15677, 15684, 15684, 15691, 15691, 15698,

15698, 15705, 15705, 15712, 15712, 15719, 15719, 15726, 15726,

15733, 15733, 15740, 15740, 15747, 15747, 15754, 15754, 15761,

15761, 15768, 15768, 15775, 15775, 15782, 15782, 15789, 15789,

15796, 15796, 15803, 15803, 15810, 15810, 15817, 15817, 15824,

15824, 15831, 15831, 15838, 15838, 15845, 15845, 15852, 15852,

15859, 15859, 15866, 15866), class = "Date"), myCount = c(142L,

338L, 350L, 324L, 490L, 4L, 567L, 178L, 414L, 113L, 489L, 195L,

241L, 168L, 355L, 164L, 1215L, 253L, 2171L, 222L, 1916L, 437L,

1820L, 420L, 1897L, 356L, 1632L, 381L, 1571L, 464L, 1835L, 620L,

1348L, 500L, 1398L, 448L, 2668L, 461L, 2510L, 456L, 2654L, 733L,

1977L, 617L, 1645L, 523L, 1857L, 550L, 1247L, 477L, 1286L, 560L,

1168L, 719L, 1264L, 649L, 1162L, 682L, 1149L, 801L, 1450L, 1018L

), myCategory = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,

2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,

2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,

2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,

2L, 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("myDate",

"myCount", "myCategory"), row.names = c(62L, 59L, 56L, 55L, 51L,

52L, 47L, 48L, 49L, 50L, 53L, 54L, 60L, 61L, 57L, 58L, 45L, 46L,

39L, 40L, 1L, 2L, 23L, 24L, 35L, 36L, 41L, 42L, 31L, 32L, 27L,

28L, 11L, 12L, 33L, 34L, 37L, 38L, 43L, 44L, 17L, 18L, 15L, 16L,

7L, 8L, 3L, 4L, 29L, 30L, 19L, 20L, 13L, 14L, 9L, 10L, 21L, 22L,

网友答案:

I don't understand why do you want to split your data in order to plot it. I would try to plot it without splitting. Also, as mentioned your structure is truncated due to a bad copy and paste. I had to remove rownames part from your structure in order to use it. IFor example, here I am using ggplot2 to plot the data without splitting. For example:

library(ggplot2)

ggplot(myTable)+
  geom_line(aes(x=myDate,y=myCount,group=myCategory,color=myCategory)) 

or splitting using faceting like this :

ggplot(myTable) +
  geom_line(aes(x=myDate,y=myCount)) +
  facet_wrap(~myCategory)

EDIT

The same thing can be obtained using rCharts package:

library(rCharts)
rPlot(myCount ~ myDate | myCategory, data = myTable, 
      color = 'myCategory', type = 'line')

Here my data is :

structure(list(myDate = structure(c(15642, 15649, 15656, 15663, 
15670, 15670, 15677, 15677, 15684, 15684, 15691, 15691, 15698, 
15698, 15705, 15705, 15712, 15712, 15719, 15719, 15726, 15726, 
15733, 15733, 15740, 15740, 15747, 15747, 15754, 15754, 15761, 
15761, 15768, 15768, 15775, 15775, 15782, 15782, 15789, 15789, 
15796, 15796, 15803, 15803, 15810, 15810, 15817, 15817, 15824, 
15824, 15831, 15831, 15838, 15838, 15845, 15845, 15852, 15852, 
15859, 15859, 15866, 15866), class = "Date"), myCount = c(142L, 
338L, 350L, 324L, 490L, 4L, 567L, 178L, 414L, 113L, 489L, 195L, 
241L, 168L, 355L, 164L, 1215L, 253L, 2171L, 222L, 1916L, 437L, 
1820L, 420L, 1897L, 356L, 1632L, 381L, 1571L, 464L, 1835L, 620L, 
1348L, 500L, 1398L, 448L, 2668L, 461L, 2510L, 456L, 2654L, 733L, 
1977L, 617L, 1645L, 523L, 1857L, 550L, 1247L, 477L, 1286L, 560L, 
1168L, 719L, 1264L, 649L, 1162L, 682L, 1149L, 801L, 1450L, 1018L
), myCategory = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("myDate", 
"myCount", "myCategory"), row.names = c(NA, -62L), class = "data.frame")
网友答案:

I have heard off-forum of the solution which is to add an appropriate order within the lapply that creates the list

mySeries <- lapply(split(myTable, myTable$myCategory), function(x) {
  res <- lapply(split(x, rownames(x)), as.list)
  names(res) <- NULL
  res <- res[order(sapply(res, function(x) x$myDate))] ## Provides Solution
  return(res)

})
相关阅读:
Top