2016-09-03 21 views
6

के लिए आर में सारणी-जैसी समूहीकृत तालिका I अक्सर मुझे dplyr का उपयोग करके आर में सारांश आंकड़ों की गणना कर रही है, फिर परिणाम को सीएसवी में लिखना और तालिका को उत्पन्न करने के लिए इसे टैबलेट में लोड करना क्योंकि टेबलू की टेबल इतनी सरल और आसान हैं। मैं आरमार्कडाउन

में सीधे तालिकाएं उत्पन्न करता हूं आर में समूहबद्ध तालिकाओं के लिए कोई आसान समाधान है?

यह डेटा उत्पन्न करने के लिए बहुत आसान है मैं चाहता हूँ होगा:

library(tidyr) 
library(dplyr) 

summary_table <- iris %>% 
    gather(measure, value, -Species) %>% 
    separate(measure, into=c("attribute", "dimension")) %>% 
    group_by(Species, attribute, dimension) %>% 
    summarise(mean=mean(value)) 

summary_table 

Source: local data frame [12 x 4] 
Groups: Species, attribute [?] 

     Species attribute dimension mean 
     <fctr>  <chr>  <chr> <dbl> 
1  setosa  Petal Length 1.462 
2  setosa  Petal  Width 0.246 
3  setosa  Sepal Length 5.006 
4  setosa  Sepal  Width 3.428 
5 versicolor  Petal Length 4.260 
6 versicolor  Petal  Width 1.326 
7 versicolor  Sepal Length 5.936 
8 versicolor  Sepal  Width 2.770 
9 virginica  Petal Length 5.552 
10 virginica  Petal  Width 2.026 
11 virginica  Sepal Length 6.588 
12 virginica  Sepal  Width 2.974 

अब मैं के रूप में इस प्रस्तुत करना चाहता हूं:

enter image description here

मैं कुछ अलग करने की कोशिश करना चाहते हैं आयोजन के तरीके, इसलिए मैं कॉलम

grouped rows

के बजाय पंक्तियों पर आसानी से समूह में सक्षम होना चाहता हूं

वर्गीकृत किया पंक्तियों संस्करण के प्रमुख विशेषताएं हैं:

  • समूहन चर, बल्कि एक अलग पंक्ति से एक अलग कॉलम में, एक सेल कि पंक्तियों
  • क्षैतिज सेल के सभी तक फैला में बाईं तरफ है समूह स्तर

मैं rmarkdown के लिए नया हूं, लेकिन अंतिम लक्ष्य यह एक HTML दस्तावेज़ में है।

क्या यह संभव है?

+0

साथ ही आप अपने दम पर एकत्रित करने से सोच सकते हैं। मैंने 'कुल (एक्स = आईरिस [, उपनाम (आईरिस) [कॉलनाम (आईरिस)! =" प्रजातियां "]], = सूची (आईरिस $ प्रजातियां), FUN = function (y) {ifelse (is.numeric (वाई), माध्य (वाई), एनए)} ) 'शुरुआत के लिए। – nilsole

उत्तर

4

htmlTable पैकेज का उपयोग करके प्रत्येक तालिका को बनाने के तरीके यहां दिए गए हैं। मुझे यकीन नहीं था कि प्रजातियों के बीच क्षैतिज रेखाएं कैसे जोड़ें, लेकिन मैंने ज़ेबरा छायांकन जोड़ा। यहाँ

--- 
title: "<h3>Untitled</h3>" 
author: "Author" 
date: "September 3, 2016" 
output: html_document 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE) 
``` 

```{r} 
library(tidyr) 
library(dplyr) 
library(reshape2) 
library(htmlTable) 
``` 

```{r} 
st = iris %>% 
    gather(measure, value, -Species) %>% 
    separate(measure, into=c("attribute", "dimension")) %>% 
    group_by(Species, attribute, dimension) %>% 
    summarise(mean=mean(value)) %>% 
    spread(dimension, mean) 

# Keep only first value of outer grouping column 
st = st %>% 
    group_by(Species) %>% 
    mutate(count=1:n()) %>% 
    ungroup %>% 
    mutate(Species = ifelse(count==1, as.character(Species), NA)) %>% 
    select(-count) 

# Remove names of grouping columns 
names(st)[1:2] = "" 

# Round numeric columns to two decimal places 
st[,sapply(st,is.numeric)] = sapply(st[,sapply(st,is.numeric)], function(x) sprintf("%1.2f",x)) 

htmlTable(st, rnames=FALSE, align="llrr", align.header="llrr", 
      col.rgroup = rep(c("none", "gray93"), each=2), 
      css.cell = c("padding-left: 0em","padding-left: 1em",rep("padding-left: 2em",2))) 
``` 
```{r} 
# Another option 
htmlTable(st[,-1], rnames=FALSE, align="llrr", align.header="lrr", 
      n.rgroup=rep(2,3), 
      rgroup=rep(unique(iris$Species),2), 
      #col.rgroup = c("none","gray93"), # If you want to add alternating shading 
      css.cell=c("padding-left: 0.5em","padding-left: 4em","padding-left: 1.5em")) 
``` 

```{r} 
st = iris %>% 
    melt(id.var="Species") %>% 
    group_by(Species, variable) %>% 
    summarise(mean=mean(value)) %>% 
    dcast(Species ~ variable) 
names(st)[1] = "" 

# Round numeric columns to two decimal places 
st[,sapply(st,is.numeric)] = sapply(st[,sapply(st,is.numeric)], function(x) sprintf("%1.2f",x)) 

# Set up grouping columns and column names 
group_col = gsub("(.*)\\..*", "\\1", names(st)) 
group_col = factor(group_col, levels=unique(group_col)) 
names(st) = gsub(".*\\.", "", names(st)) 

htmlTable(st, rnames=FALSE, align="lrrrr", 
      align.header="lrrrr", 
      cgroup=unique(group_col), n.cgroup=unclass(table(group_col)), 
      css.cell = c("padding-left: 0em","padding-left: 1.5em", rep("padding-left: 2em",3))) 
``` 

उत्पादन है::

यहाँ दस्तावेज़ है

enter image description here