2016-03-03 80 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