代码之家  ›  专栏  ›  技术社区  ›  HM8689

如何对R中的连接字符串使用Pivottabler

r
  •  0
  • HM8689  · 技术社区  · 7 年前

    我有一张这样的桌子-

    LDAutGroup  PatientDays ExposedDays sex    Ageband    DrugGroup Prop    LowerCI UpperCI concat
    Group1            100        23       M    5 to 10      PSY      23       15.84   32.15  23 (15.84 -32.15)    F
    Group2            500        56       F    11  to 17    HYP      11.2      8.73   14.27  11.2 (8.73 -14.27)
    Group3            300        89       M    18 and over  PSY      29.67    24.78   35.07  29.67 (24.78 -35.07)
    Group1            200        34       F    5 to 10      PSY      17       12.43   22.82  17 (12.43 -22.82)
    Group2            456        78       M    11 to 17     ANX      17.11    13.93   20.83  17.11 (13.93 -20.83)
    

    接下来,我希望透视表将concat列作为valuename进行布局。但是,数据透视表只处理整数或数值。下面的代码可以单独运行Prop、LowerCI或UpperCI列,但会为concat列提供错误消息-

    library(readr)
    library(dplyr)
    library(epitools)
    library(gtools)
    library(reshape2)
    library(binom)
    library(pivottabler)    
    pt <- PivotTable$new()
        pt$addData(a)
        pt$addColumnDataGroups("LDAutGroup")
        pt$addColumnDataGroups("sex")
        pt$addRowDataGroups("DrugGroup")
        pt$addRowDataGroups("Ageband")
        pt$defineCalculation(calculationName="TotalTrains", type="value", valueName="Prop")
        pt$renderPivot()
    

    我能在concat列上做这个吗?我想要一个具有以下布局的表,以及用上表的concat列中的字符串填充的单元格

                         Group1     Group2      Group3  
                          M   F     M    F      M    F
    ANX 11 to 17                        
        18 and over                     
        Total                       
    HYP 11  to 17                       
        18 and over                     
        5 to 10                     
        Total                       
    PSY 18 and over                     
        5 to 10                     
        Total       
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   cbailiss    7 年前

    我是pivottabler包的作者。

    如您所说,pivottabler当前只对整数/数字列进行透视。然而,使用自定义单元计算函数来计算每个单元中的值,仍然存在一种解决方法。自定义计算函数是为更复杂的用例而设计的,因此以这种方式使用它们是一种大锤式方法,但它可以完成这项工作,而且我认为在某些情况下是有意义的,例如,如果您有其他数值枢轴表,并且希望在输出中的枢轴表具有统一的外观。

    根据包的小插曲改编一个示例:

    library(pivottabler)
    library(dplyr)
    
    trainsConcatendated <- mutate(bhmtrains, ConcatValue = paste(TOC, TrainCategory, sep=" "))
    
    getConcatenatedValue <- function(pivotCalculator, netFilters, format, baseValues, cell) {
      # get the data frame
      trains <- pivotCalculator$getDataFrame("trainsConcatendated")
      # apply the filters coming from the headers in the pivot table
      filteredTrains <- pivotCalculator$getFilteredDataFrame(trains, netFilters)
      # get the distinct values
      distinctValues <- distinct(filteredTrains, ConcatValue)
      # get the value of the concatenated column
      # this just returns the first concatenated value for the cell
      # if there are multiple values, the others are ignored
      if(length(distinctValues$ConcatValue)==0) { tv <- "" }
      else { tv <- distinctValues$ConcatValue[1] }
      # build the return value
      # the raw value must be numerical, so simply set this to zero
      value <- list()
      value$rawValue <- 0
      value$formattedValue <- tv
      return(value)
    }
    
    pt <- PivotTable$new()
    pt$addData(trainsConcatendated)
    pt$addColumnDataGroups("TrainCategory", addTotal=FALSE)
    pt$addRowDataGroups("TOC", addTotal=FALSE)
    pt$defineCalculation(calculationName="ConcatValue", 
                         type="function", calculationFunction=getConcatenatedValue)
    pt$renderPivot()
    

    结果:

    enter image description here

        2
  •  0
  •   Nar    7 年前

    对CI应用相同的函数(较低或较高)是推测性的,因为mean statistics可以报告小计级别,而concat不能报告小计(至少以pivot table的简单方式)。 由于没有小计,您可以很容易地使用tidyr库和表扩展格式的字符类型的报表变量:这里有两行代码。首先是为列创建组,第二行是将表格式更改为扩展版本

    library(tidyr)
    
    Table_Original <- unite(Table_Original, "Col_pivot", c("LDAutGroup", "sex"), sep = "_", remove = F)
    Table_Pivot <- spread(Table_Original[ ,c("Col_pivot","DrugGroup", "Ageband", "concat")], Col_pivot, concat)