2017-06-13 5 views
0

私は、複数のリレーショナルテーブルに格納されているフォレストインベントリデータを持っています。マークダウンで、データのリレーショナル階層に沿った情報を表示するレポートを作成したいと思います。すなわち、Rマークダウンでループを介してリレーショナルデータを表示

Plot 1 
    Subplot 1 
     Tree 1 
     Tree 2 
     Tree 3 
Plot 1 
    Subplot 2 
     Tree 1 
     Tree 2 
     Tree 3 (and so on…) 

以下に示すコードは、すべてのサブプロットデータを一緒に再印刷します。しかし、私は個々のテーブル(私はknitrを使用しています)の各サブプロットをしたいので、私はそれぞれのサブプロットの下に追加ツリーの属性を追加することができます。私の問題は、以下のコードで再現することができます:

# Define Plot Number 

```{r} 
call_plot <- 1 
``` 



```{r echo=FALSE, include=FALSE} 
# Make the df plot_info 
plot_id <- c(1:3) 
x <- c(89.74, 89.67, 89.62) 
y <- c(22.71, 22.66, 22.71) 
plot_info <- as.data.frame(cbind(plot_id, x, y)) 

# Make the df subplot_info 
plot_id <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3) 
subplot_id <- c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5) 
subplot_info <- as.data.frame(cbind(plot_id, subplot_id)) 

# Make the df for tree 
plot_id <- c(1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
     2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3) 
subplot_id <- c(4, 4, 4, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 
      5, 5, 1, 1, 2, 2, 2, 2, 2, 4, 4, 5, 5, 5, 5) 

species <- c("Phoenix sylvestris", "Albizia procera", "Tamarindus indica", 
     "Lannea coromandelica", "Lannea coromandelica", "Cocos nucifera", 
     "Cocos nucifera", "Samanea saman", "Samanea saman", "Cocos nucifera", 
     "Swietenia mahagoni", "Gmelina arborea", "Mangifera indica", "Mangifera 
     indica", "Cocos nucifera", "Cocos nucifera", "Samanea saman", "Mangifera 
     indica", "Artocarpus heterophyllus", "Artocarpus heterophyllus", "Artocarpus 
     heterophyllus", "Cocos nucifera", "Cocos nucifera", "Swietenia mahagoni", 
     "Cocos nucifera", "Samanea saman", "Polyalthia longifolia", "Samanea saman", 
     "Dillenia indica") 

tree <- as.data.frame(cbind(plot_id, subplot_id, species)) 
``` 


```{r latex_table, results='markup', echo=FALSE, results='asis'} 

# Asign call_plot 
plot_info <- plot_info[which(plot_info$plot_id == call_plot),] 
# Print Plot table 
knitr::kable(plot_info, table.attr = "id=\"mytable\"", caption = "Plot Location") 

#Asign subplot data to plot table 
subplot_info <- subplot_info[which(subplot_info$plot_id == plot_info$plot_id),] 

# Loop through subplots 
for (i in 1:length(unique(subplot_info$subplot_id))){ 

p = (knitr::kable(subplot_info, table.attr = "id=\"mytable\"", caption = "Subplot")); 

print(p) 
} 
``` 

答えて

0

問題はあなたのループにあります。私が正しく理解していれば、ツリー情報を表示したいが、テーブルsubplot_infoを呼び出す。これが動作するかどうか確認してください:

ツリーデータフレームにtree_id列を追加します。

tree_id <- 1:length(subplot_id) 

tree <- as.data.frame(cbind(plot_id, subplot_id, tree_id, species)) 

は、(サブプロットを通じて#ループ)のためのあなたのループを変更すると:

# loop through subplot 
vec_plot_id <- unique(tree$plot_id) 
vec_subplot_id <- unique(tree$subplot_id) 
for (i in 1:length(vec_plot_id)){ 
    for (j in 1:length(vec_subplot_id)){ 

    table_tree <- tree[which(tree$plot_id == vec_plot_id[i] & tree$subplot_id == vec_subplot_id[j]),] 
    table_tree[,c("plot_id", "subplot_id")] <- NULL 

    caption = paste("plot id: ", vec_plot_id[i], ", subplot id: ", vec_subplot_id[j]) 

    p = (knitr::kable(table_tree, table.attr = "id=\"mytable\"", caption = caption)) 

    print(p) 
    } 
} 

・ホープこのヘルプを!

+0

完璧!ありがとう@ダビッド – cosi

関連する問題