以下代码将向量与数据帧组合在一起:
newrow = c(1:4)
existingDF = rbind(existingDF,newrow)
Run Code Online (Sandbox Code Playgroud)
但是,此代码始终在数据帧的末尾插入新行.
如何在数据框内的指定点插入行?例如,假设数据帧有20行,如何在第10行和第11行之间插入新行?
我经常需要根据不同变量的值更改变量的值,如下所示:
mtcars$mpg[mtcars$cyl == 4] <- NA
Run Code Online (Sandbox Code Playgroud)
我尝试过这样做,dplyr但悲惨地失败了:
mtcars %>%
mutate(mpg = mpg == NA[cyl == 4]) %>%
as.data.frame()
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做dplyr?
如果我加载MASS包:
library(MASS)
Run Code Online (Sandbox Code Playgroud)
然后加载尝试运行dplyr::select,我收到一个错误:
library(dplyr)
mtcars %.%
select(mpg)
# Error in select(`__prev`, mpg) : unused argument (mpg)
Run Code Online (Sandbox Code Playgroud)
如何使用加载dplyr::select的MASS包?
我尝试使用这里提供的代码用dplyr找到所有重复的元素,如下所示:
library(dplyr)
mtcars %>%
mutate(cyl.dup = cyl[duplicated(cyl) | duplicated(cyl, from.last = TRUE)])
Run Code Online (Sandbox Code Playgroud)
如何转换此处显示的代码以使用dplyr查找所有重复的元素?我上面的代码只是抛出一个错误?或者甚至更好,是否有另一种功能可以比复杂的x[duplicated(x) | duplicated(x, from.last = TRUE)])方法更简洁地实现这一目标?
我需要替换数据帧中因子列的级别.使用iris数据集作为一个例子,我将如何取代它含有任何细胞virginica与setosa在物种列?
我期望以下工作,但它会生成一条警告消息,只需插入NAs:
iris$Species[iris$Species == 'virginica'] <- 'setosa'
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用knitr和pandoc转换为PDF时控制绘图的位置.我的.Rmd文件看起来像这样:
# My report
Some text some text some text some text some text some text some text some text some text
```{r myplot, echo=FALSE, fig.pos="placeHere", results='hide'}
library(ggplot2)
ggplot(mtcars, aes(mpg, drat)) + geom_point()
```
Some text some text some text some text some text some text some text some text some text
\usepackage{graphicx}
\begin{figure}[placeHere]
\centering
\includegraphics[width=0.5\textwidth]{placeHere}
\end{figure}
Some text some text some text some text some text some text some text some text some text
Run Code Online (Sandbox Code Playgroud)
我正在使用此处提供的功能转换为PDF:http://quantifyingmemory.blogspot.co.uk/2013/02/reproducible-research-with-r-knitr.html
如何在第二和第三块文本之间放置图?乳胶代码目前无法正常工作. …
我在R中使用knitr包和pandoc将.Rmd文件转换为PDF.Pandoc链接到.bib文件并自动在PDF末尾插入参考书目.bib文件中的条目如下所示,取自http://johnmacfarlane.net/pandoc/demo/biblio.bib:
@Book{item1,
author="John Doe",
title="First Book",
year="2005",
address="Cambridge",
publisher="Cambridge University Press"
}
@Article{item2,
author="John Doe",
title="Article",
year="2006",
journal="Journal of Generic Studies",
volume="6",
pages="33-34"
}
Run Code Online (Sandbox Code Playgroud)
为了构建我的参考书目,我使用以下函数,取自:http://quantifyingmemory.blogspot.co.il/2013/02/reproducible-research-with-r-knitr.html
knitsPDF <- function(name) {
library(knitr)
knit(paste0(name, ".Rmd"), encoding = "utf-8")
system(paste0("pandoc -o ", name, ".pdf ", name, ".md --bibliography /Users/.../Desktop/test.bib --csl /Users/.../Desktop/taylor-and-francis-harvard-x.csl"))
}
Run Code Online (Sandbox Code Playgroud)
我的.Rmd文件的内容是:
This is some text [@item1]
This is more text [@item2]
# References
Run Code Online (Sandbox Code Playgroud)
输出的PDF看起来像这样:

如果我尝试插入附录,则引用仍会在文档末尾打印,如下所示:

如何在引用后插入附录?
stargazer自动居中表中的值.如何左列对齐?
将此代码放在.Rnw文件中并使用knitr转换为.tex:
<<load, echo=FALSE, warning=FALSE, message=FALSE>>=
opts_chunk$set(eval=TRUE, echo=FALSE, warning=FALSE, message=FALSE, dpi=300)
@
\documentclass[a4paper,11pt]{article}
\usepackage{lipsum} % Required to insert dummy text
\begin{document}
\title{}
\author{}
\date{\today}
\maketitle
\section{Header}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. …Run Code Online (Sandbox Code Playgroud) 我正在使用knitr并pander在markdown文件中创建一个表.我正在使用内部的Pandoc将降价文件转换为PDF.
这段代码:
library(knitr)
```{r myTable, echo=FALSE, message=FALSE, results='asis', comment=""}
library(pander)
pandoc.table(head(iris))
```
Run Code Online (Sandbox Code Playgroud)
然后在R中运行此函数:
knitsPDF <- function(name) {
knit(paste0(name, ".Rmd"), encoding = "utf-8")
callformat <-"pandoc -V geometry:margin=1in %s.md -o %s.pdf"
system(sprintf(callformat, name, name))
}
knitsPDF(name) # insert file name of .Rmd file
Run Code Online (Sandbox Code Playgroud)
在PDF文件中生成此表:

我怎样才能1.减少表格中列的宽度?2.减小表的字体大小?
我很困惑为什么箱形图没有在这个情节中订购:
set.seed(200)
x <- data.frame(country=c(rep('UK', 10),
rep("USA", 10),
rep("Ireland", 5)),
wing=c(rnorm(25)))
ggplot(x, aes(reorder(country, wing, median), wing)) + geom_boxplot()
Run Code Online (Sandbox Code Playgroud)

如何根据最高最低中位数(从左到右)订购箱图?