遍历数据而不是在R中建立索引

新蜂

我正在尝试使用Rmarkdown将数据转换为html文档,并且目前依靠转换为矢量和索引来解决我的问题。

尽管我的样本数据有4个观测值,但我的实际数据集却有30多个记录,因此索引似乎很麻烦且不自然。

是否有更好的方法按顺序提取这些元素?任何建议都很好。

 --
title: "Rmarkdown report"
output: html_document
---
    
    
```{r echo = FALSE}
mydata <- data.frame(First = c("John", "Hui", "Jared"), Second = c("Smith", "Chang", "Jzu"), Sport = c("Football","Soccer","Ballet"), Age = c("12", "13", "12"), submission =     c("Microbes may be the friends of future colonists living off the land on the moon, Mars or elsewhere in the solar system and aiming to establish self-sufficient homes. Space     colonists, like people on Earth, will need what are known as rare earth elements, which are critical to modern technologies. These 17 elements, with daunting names like yttrium,     lanthanum, neodymium and gadolinium, are sparsely distributed in the Earths crust. Without the rare earths, we wouldn’t have certain lasers, metallic alloys and powerful magnets that     are used in cellphones and electric cars. But mining them on Earth today is an arduous process. It requires crushing tons of ore and then extracting smidgens of these metals using     chemicals that leave behind rivers of toxic waste water.",

"Experiments conducted aboard the International Space Station show that a potentially cleaner, more efficient method could work on other worlds: let bacteria do the messy work of     separating rare earth elements from rock. The idea is the biology is essentially catalyzing a reaction that would occur very slowly without the biology, said Charles S. Cockell, a     professor of astrobiology at the University of Edinburgh.
On Earth, such biomining techniques are already used to produce 10 to 20 percent of the world’s copper and also at some gold mines; scientists have identified microbes that help     leach rare earth elements out of rocks.",
"Experiments conducted aboard the International Space Station show that a potentially cleaner, more efficient method could work on other worlds: let bacteria do the messy work of     separating rare earth elements from rock. The idea is the biology is essentially catalyzing a reaction that would occur very slowly without the biology, said Charles S. Cockell, a     professor of astrobiology at the University of Edinburgh.
On Earth, such biomining techniques are already used to produce 10 to 20 percent of the world’s copper and also at some gold mines; scientists have identified microbes that help     leach rare earth elements out of rocks."))
    

    
first<- as.vector(mydata$First)
sec <- as.vector(mydata$Second)
age <- as.vector(mydata$Age)
submission <- as.vector(mydata$submission)

```
    
    
    
    
## 

**First:** `r first[1]` &emsp; **Second:**  `r sec[1]` <br>
**Age:** `r age[1]`    


**submission** <br>

`r submission[1]`


***

**First:** `r first[2]` &emsp; **Second:**  `r sec[2]` <br>
**Age:** `r age[2]`    


**submission** <br>

`r submission[2]`
弗里克先生

这是一种遍历所有行的方法

---
title: "Rmarkdown report"
output: html_document
---
    
    
```{r echo = FALSE}
# using data from above
# mydata <- data.frame(...)

# Define template (using column names from data.frame)
template <- "**First:** `r First` &emsp; **Second:**  `r Second` <br>
**Age:** `r Age`    


**submission** <br>

`r submission`"


# Now process the template for each row of the data.frame
src <- lapply(1:nrow(mydata), function(i) {
  knitr::knit_child(text=template, envir=mydata[i, ], quiet=TRUE)
})

```
# Print result to document
`r knitr::knit_child(text=unlist(src))`

在这里,我们使用knit_child一个模板字符串,然后将其用于data.frame的每一行。我在这里使用了一个技巧,将data.frame的行作为环境传递,以便模板可以将所有列视为变量,因此我们无需创建所有data.frame列的向量版本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

遍历数据而不是在R中建立索引

来自分类Dev

遍历数据集并在R或Python中定位行索引

来自分类Dev

按索引遍历数据框

来自分类Dev

在R中循环遍历数据帧长度的正确方法

来自分类Dev

遍历数据框以在R中创建图

来自分类Dev

如何遍历数据帧列表以在R中设置列名?

来自分类Dev

遍历数据框列表以在R中创建图形

来自分类Dev

R - 使用 for 循环遍历数据帧

来自分类Dev

遍历数据框中的行

来自分类Dev

如何遍历数据框中的列?

来自分类Dev

遍历数据框中的多列

来自分类Dev

遍历数组中的列并复制数据

来自分类Dev

遍历数据结构的元素而不是Collection

来自分类Dev

遍历数据框

来自分类Dev

遍历数据框

来自分类Dev

从JavaScript中的索引向后遍历数组

来自分类Dev

如何在zsh中遍历数组索引?

来自分类Dev

如何在zsh中遍历数组索引?

来自分类Dev

从JavaScript中的索引向后遍历数组

来自分类Dev

如何管理遍历数组REACT中每个索引的状态

来自分类Dev

如何修复循环遍历数组中索引的代码?

来自分类Dev

R使用map2遍历数据框列表中的列以适合统计模型

来自分类Dev

遍历数据帧以提取值大于R中阈值的特定对

来自分类Dev

循环遍历数据框的行并将其用作r中的函数输入

来自分类Dev

在R中以组的形式遍历数据集的每x个数字列

来自分类Dev

如何使用字符向量遍历数据帧并计算R中匹配项的均值

来自分类Dev

循环遍历数据以将值>或<变量设置为R中的NA

来自分类Dev

在数据帧区中循环遍历数据帧

来自分类Dev

在R中使用for循环遍历数据帧的名称