当突变是有条件的(取决于某些列值的值)时,是否可以使用mutate?
这个例子有助于显示我的意思.
structure(list(a = c(1, 3, 4, 6, 3, 2, 5, 1), b = c(1, 3, 4,
2, 6, 7, 2, 6), c = c(6, 3, 6, 5, 3, 6, 5, 3), d = c(6, 2, 4,
5, 3, 7, 2, 6), e = c(1, 2, 4, 5, 6, 7, 6, 3), f = c(2, 3, 4,
2, 2, 7, 5, 2)), .Names = c("a", "b", "c", "d", "e", "f"), row.names = c(NA,
8L), class = "data.frame")
a b c …Run Code Online (Sandbox Code Playgroud) 在具有四列的大型数据框("myfile")中,我必须添加第五列,其中值基于前四列.最近我成为了dplyr的忠实粉丝,主要是因为它在大型数据集中的速度.所以我想知道我是否可以使用mutate函数处理我的问题.
我的数据帧(实际上是它的较短版本)看起来有点像这样:
V1 V2 V3 V4
1 1 2 3 5
2 2 4 4 1
3 1 4 1 1
4 4 5 1 3
5 5 5 5 4
...
Run Code Online (Sandbox Code Playgroud)
第五列(V5)的值基于一些条件规则:
if (V1==1 & V2!=4) {
V5 <- 1
} else if (V2==4 & V3!=1) {
V5 <- 2
} else {
V5 <- 0
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用mutate函数在所有行上使用这些规则(所以我不必使用慢循环).像这样的东西(是的,我知道它不会这样工作!):
myfile <- mutate(myfile, if (V1==1 & V2!=4){V5 = 1}
else if (V2==4 & V3!=1){V5 = 2}
else {V5 = 0})
Run Code Online (Sandbox Code Playgroud)
这应该是结果: …
我是dplyr包的数据库连接功能的新手,但我对将它用于SQLite连接非常感兴趣.我按照本教程创建了一个SQLite数据库(my_db)
my_db <- src_sqlite("my_db.sqlite3", create = T)
Run Code Online (Sandbox Code Playgroud)
并插入一个数据帧(df)作为该数据库的表(my_table).
copy_to(my_db,df,"my_table")
Run Code Online (Sandbox Code Playgroud)
现在我想在此表中插入新行.我试过这样的事情(是的,我必须承认它看起来没什么前途......但我还是试了一下):
collect(build_sql("INSERT INTO my_table VALUES (",newdf,")", con=my_db))
Run Code Online (Sandbox Code Playgroud)
有没有人知道是否可以使用dplyr向现有的sqlite db表添加行?或者您将如何处理这个问题?提前谢谢了!
我使用rmarkdown来呈现pdf文档.现在我想在文本中添加内部链接.
在rmarkdown的帮助页面中,它表示内部链接定义为:
See the [Introduction](#introduction).
Run Code Online (Sandbox Code Playgroud)
当我使用例如下一个代码时,应该有两个内部链接:link1和link2.两者都没有联系.有什么明显的东西我做错了吗?提前谢谢了!
---
title: "Test"
author: "test test"
output:
pdf_document:
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
html_document:
css: tables.css
number_sections: yes
theme: cerulean
toc: yes
toc_depth: 3
subtitle: test test test
mainfont: Calibri Light
fontsize: 12pt
header-includes:
- \usepackage[dutch]{babel}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyfoot[LE,RO]{this is a fancy foot}
- \usepackage{dcolumn}
- \usepackage{here}
- \usepackage{longtable}
- \usepackage{caption}
- \captionsetup{skip=2pt,labelsep=space,justification=justified,singlelinecheck=off}
---
# start
```{r results="asis",tidy=FALSE,eval=TRUE,echo=FALSE,message=FALSE, error=FALSE, warning=FALSE, comment = NA}
cat("click here: [link1](#test …Run Code Online (Sandbox Code Playgroud) 对于一个新项目,我想尝试新的flexdasboard包.我正在考虑一种布局,其中列和行方向以某种方式组合.
我想的布局是这样的:
如果我更改此代码:
---
title: "Focal Chart (Left)"
output: flexdashboard::flex_dashboard
---
Column {data-width=600}
-------------------------------------
### Chart 1
```{r}
```
Column {data-width=400}
-------------------------------------
### Chart 2
```{r}
```
### Chart 3
```{r}
```
Run Code Online (Sandbox Code Playgroud)
进入这个:
---
title: "Focal Chart (Left)"
output: flexdashboard::flex_dashboard
---
Column {data-width=600}
-------------------------------------
### Chart 1
```{r}
```
Column {data-width=400}
-------------------------------------
Row {data-width=400}
-------------------------------------
### Chart 2
```{r}
```
### Chart 3
```{r}
```
Row {data-width=400}
-------------------------------------
### Chart 4
```{r}
```
Run Code Online (Sandbox Code Playgroud)
(当然)这不起作用,但我还没有找到正确的方法.有没有人有想法?
在脚本中我试图运行有时是变量被填充''(意思是:完全空),例如
variable <- ''
Run Code Online (Sandbox Code Playgroud)
有谁知道检查变量是否有值的方法''?
is.null(variable)似乎不起作用.''是不一样的NULL.
使用rmarkdown呈现html文档时,有一些方法可以进行两列布局,例如此处
有没有一种简单的方法来呈现具有两列布局的pdf文档?某处有示例代码吗?
我有一个rmarkdown文档(.Rmd),我想编成一个pdf文档.number_sections已设置为"yes",toc设置为"true".如何添加出现在目录中但没有节号的附录部分?
这是一个示例.Rmd代码.如何让附录A和附录B成为无数段,并让它们同时出现在目录中?
---
title: "Test"
author: "test test"
geometry: margin=1in
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
header-includes:
- \usepackage[dutch]{babel}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyfoot[LE,RO]{this is a fancy foot}
- \usepackage{dcolumn}
- \usepackage{here}
- \usepackage{longtable}
- \usepackage{caption}
- \captionsetup{skip=2pt,labelsep=space,justification=justified,singlelinecheck=off}
subtitle: test test test
fontsize: 12pt
---
# start
# second
```{r results="asis",eval=TRUE,echo=FALSE,message=FALSE, error=FALSE, warning=FALSE, comment = NA,fig.height=4}
cat("and here some R")
```
# appendix A
# appandix B
Run Code Online (Sandbox Code Playgroud) 我想创建一个新变量,以防它不存在.如果它存在则不必更改任何内容,但如果不存在则我想要生成此变量并将其赋予NA值.我尝试使用dplyr包的mutate函数来做到这一点,但我只得到了NA的结果.
library(dplyr)
df <- structure(list(var1 = c(47, 801, 660), var2 = c(11,
17, 11)), .Names = c("var1", "var2"), row.names = c(NA,
3L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
现在我检查var2的存在.
df <- mutate(df, var2 = ifelse(exists('var2')==TRUE,var2,NA))
Run Code Online (Sandbox Code Playgroud)
结果是:
var1 var2
1 47 NA
2 801 NA
3 660 NA
Run Code Online (Sandbox Code Playgroud)
但是,结果应该是(因为没有什么必须改变:
var1 var2
1 47 11
2 801 17
3 660 11
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?它甚至可以用exists()完成吗?提前谢谢了.
r ×10
dplyr ×4
r-markdown ×4
mutate ×2
case-when ×1
dashboard ×1
if-statement ×1
latex ×1
sqlite ×1
string ×1