2016-03-03 86 views
4

类型列表的列对于样本数据帧的错误消息:在data.table

df1 <- structure(list(country = list("PL", "PL", "PL", "PL", "SE", "SE", 
            "SE", "SE", "SE", "SE", "SE", "SE"), 
        region = c("PL42", "PL34", 
          "PL33", "PL62", "SE22", "SE32", "SE11", "SE31", "SE23", "SE12", 
           "SE21", "SE33"), 
        N = c(59L, 55L, 59L, 48L, 233L, 91L, 406L, 148L, 
         323L, 248L, 163L, 104L), 
        freq.1 = c(31L, 34L, 37L, 27L, 109L, 
           53L, 175L, 82L, 169L, 134L, 80L, 51L), result = c(53.52, 61.2, 
                                                    63.24, 55.7, 46.78, 58.24, 43.1, 55.41, 52.32, 54.03, 49.08, 
                                                    49.04), level = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)), .Names = c("country", 
                                                                    "region", "N", "freq.1", "result", "level"), class = c("data.table", 
                                                                                  "data.frame"), row.names = c(NA, -12L)) 

我想申请一个简短的函数产生一个汇总表:

variable.country <-setDT(df1)[order(country), 
              list(min_result = min(result), 
               no.regions =.N, 
               max_result = max(result), 
               level= level[1L]), 
              by = country] 

但我得到的错误:

Error in forder(x, country) : 
    Column '1' is type 'list' which is not supported for ordering currently. 

我该如何解决?

+0

你的对象名称是'df1'而不是'df' – akrun

+1

我厌倦了编辑搞砸了数据帧格式。你的责任。或者,如果这个错字是,那么你应该删除。 –

+0

国家列是一个“列表”列 – akrun

回答

4

“国家”栏是list。我们可以unlist列,它应该正常工作

df1[, country:= unlist(country)][order(country), 
          list(min_result = min(result), 
           no.regions =.N, 
           max_result = max(result), 
           level= level[1L]), .(country)] 
# country min_result no.regions max_result level 
#1:  PL  53.52   4  63.24  2 
#2:  SE  43.10   8  58.24  2