Knitr Kable赢得输出可以有人告诉我为什么?

时间:2017-07-19 23:20:16

标签: r knitr r-markdown rscript

我有两个Kable,两个都将正确输出作为数据框但我希望它们是kables,因为它看起来更聪明。第一个作为一个kable很好用:

    #Build tables and convert to df
    #First time=N 
    firstTimeNWork <- as.data.frame(tbl(SDreams, "PassengersInfo") %>% filter(FirstTime == 0 & WorkHoliday == 1) 
                            %>% select(FirstTime,WorkHoliday,Price)) 

    firstTimeNWorkN <- as.data.frame(tbl(SDreams, "PassengersInfo") %>% filter(FirstTime == 0 & WorkHoliday == 0) 
                             %>% select(FirstTime,WorkHoliday,Price))

    #First time=Y
    firstTimeYWork <- as.data.frame(tbl(SDreams, "PassengersInfo") %>% filter(FirstTime == 1 & WorkHoliday == 1) 
                            %>% select(FirstTime,WorkHoliday,Price))

    firstTimeYWorkN <- as.data.frame(tbl(SDreams, "PassengersInfo") %>% filter(FirstTime == 1 & WorkHoliday == 0) 
                             %>% select(FirstTime,WorkHoliday,Price))

    #Setup headings for the outputted table
    FirstTime <- c('No',' ','Yes',' ')

    WorkHoliday<- c('Yes','No','Yes','No')

    #Find out average amount paid based on the tables i created
    #First time=N
    firstTimeNWorkMean <- mean(firstTimeNWork$Price)
    firstTimeNWorkNMean <- mean(firstTimeNWorkN$Price)
    #First time=Y
    firstTimeYWorkMean <- mean(firstTimeYWork$Price)
    firstTimeYWorkNMean <- mean(firstTimeYWorkN$Price)

    #Add values under heading
    MeanPrice <-c(firstTimeNWorkMean,firstTimeNWorkNMean,firstTimeYWorkMean,firstTimeYWorkNMean)
    #Create the output df
    passengerSummary1 <- data.frame(FirstTime,WorkHoliday,MeanPrice)
    #Label columns & display
    kable(passengerSummary1,
  col.names = c("First time travelling?","As part of work?","Average price paid in (£)"), 
  align = c("c","c","c"),digits = 0)

然而第二个Kable不会创造:

    #Create tables
    wouldVisitAgainNPrice <- as.data.frame(tbl(SDreams, "PassengersInfo") %>% filter(Propensity == 1)
                                  %>% select(Propensity,Price))
    wouldVisitAgainYPrice <- as.data.frame(tbl(SDreams, "PassengersInfo") %>% filter(Propensity == 2)
                                  %>% select(Propensity,Price))
    #Count amount of passengers based on filter
    wouldVisitAgainNPriceTab <-tabulate(wouldVisitAgainNPrice$Propensity)
    wouldVisitAgainYPriceTab <-tabulate(wouldVisitAgainYPrice$Propensity)[[2]]
    #Setup column
    WouldVisitAgain <-c('No','Yes')
    #Add column with values 
    Passengers <-c(wouldVisitAgainNPriceTab,wouldVisitAgainYPriceTab)
    #Means of prices
    wouldVisitAgainNPriceMean <-mean(wouldVisitAgainNPrice$Price)
    wouldVisitAgainYPriceMean <-mean(wouldVisitAgainYPrice$Price)
    #Add column with price values
    MeanPrice2 <-c(wouldVisitAgainNPriceMean,wouldVisitAgainYPriceMean)
    #Create df 
    passengerSummary2 <-data.frame(WouldVisitAgain,Passengers,MeanPrice2)
    #Label columns & display
    kable(passengerSummary2,
    col.names = c("Would visit again?","No of Passengers","Mean Price in (£)"), 
  align = c("c","c","c"),digits = 0)

正如你可以说凯布尔代码是完全一样的,所以我很困惑为什么它不会起作用。我已经尝试更新我的所有软件包并重新启动R Studio并删除对象,但这些都没有任何区别。

提前抱歉代码数量和有多糟糕。我是新手,所以我知道我可能做了一些非常愚蠢的事情,并且会被问及为什么我这样做,我唯一的答案就是我&# 39;新的,我会记住这一点,如果我有时间,也许可以尝试一下。

如果有人可以提供帮助,请提前致谢:)

0 个答案:

没有答案