首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在R中对连接的字符串使用数据透视表

如何在R中对连接的字符串使用数据透视表
EN

Stack Overflow用户
提问于 2018-08-10 00:36:52
回答 2查看 127关注 0票数 0

我有一张桌子,看起来像这样-

代码语言:javascript
复制
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)

接下来,我想要一个数据透视表将连接列布局为valuename。但是,透视表仅适用于整数或数值。下面的代码可以单独使用Prop、LowerCI或UpperCI列运行,但会给出concat列的错误消息-

代码语言:javascript
复制
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列上工作?我想要一个具有以下布局的表格,并且上面表格中的连接列中的单元格填充了字符串

代码语言:javascript
复制
                     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       
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-15 15:59:26

我是pivottabler包的作者。

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

改编包vignettes中的一个示例:

代码语言:javascript
复制
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()

结果:

票数 0
EN

Stack Overflow用户

发布于 2018-08-10 01:52:24

将相同的函数应用于CI (较低或较高)是投机性的,因为它可以用于平均统计报告小计水平,以及concat报告小计的意义(至少在数据透视表的简单方式中)。在没有小计的情况下,你可以很容易地使用tidyr库和表的字符类型的报表变量:这是2行代码。第一行是为列创建组,第二行是将表格式更改为展开版本

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51772083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档