美文网首页
【每天一个R语言命令】-table

【每天一个R语言命令】-table

作者: 肖ano | 来源:发表于2021-01-29 09:53 被阅读0次

    【描述】

    tableuses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels.
    table使用交叉分类因子来建立每个因子水平组合的计数列联表。

    【用法】
    1、统计元素次数
    2、结果提取

    table(...,
          exclude = if (useNA == "no") c(NA, NaN),
          useNA = c("no", "ifany", "always"),
          dnn = list.names(...), deparse.level = 1)
    
    as.table(x, ...)
    is.table(x)
    
    ## S3 method for class 'table'
    as.data.frame(x, row.names = NULL, ...,
                  responseName = "Freq", stringsAsFactors = TRUE,
                  sep = "", base = list(LETTERS))
    

    【参数】

    
    ... 
    one or more objects which can be interpreted as factors (including character strings), or a list (or data frame) whose components can be so interpreted. (For as.table, arguments passed to specific methods; for as.data.frame, unused.)
    
    exclude 
    levels to remove for all factors in .... If it does not contain NA and useNA is not specified, it implies useNA = "ifany". See ‘Details’ for its interpretation for non-factor arguments.
    
    useNA   
    whether to include NA values in the table. See ‘Details’. Can be abbreviated.
    
    dnn 
    the names to be given to the dimensions in the result (the dimnames names).
    
    deparse.level   
    controls how the default dnn is constructed. See ‘Details’.
    
    x   
    an arbitrary R object, or an object inheriting from class "table" for the as.data.frame method. Note that as.data.frame.table(x, *) may be called explicitly for non-table x for “reshaping” arrays.
    
    row.names   
    a character vector giving the row names for the data frame.
    
    responseName    
    The name to be used for the column of table entries, usually counts.
    
    stringsAsFactors    
    logical: should the classifying factors be returned as factors (the default) or character vectors?
    
    sep, base   
    passed to provideDimnames.
    

    【代码】

    > test <- c(1,2,3,2,1,1,5,6,4,5,1)
    > table(test)
    1 2 3 4 5 6   #test中的元素
    4 2 1 1 2 1   #各个元素出现的频率
    
    > test <- sample(c("a", "b", "c"), 100, replace=TRUE)
    > names(table(test))
    [1] "a" "b" "c"
    > as.numeric(table(test))
    [1] 42 25 33
    
    > as.data.frame(table(test))
      x Freq
    1 a   42
    2 b   25
    3 c   33
    

    相关文章

      网友评论

          本文标题:【每天一个R语言命令】-table

          本文链接:https://www.haomeiwen.com/subject/bklxtltx.html