有没有办法测试R语言中两个对象是否相同?
为清楚起见:在identical函数意义上,我的意思并不相同,它根据某些属性(如数值或逻辑值等)比较对象.
我对对象标识非常感兴趣,例如可以使用isPython语言中的运算符进行测试.
我有一个数据框,其中包含一组对象df$data和一组要应用于每个对象的规则df$rules.
df <- data.frame(
data = c(1,2,3),
rules = c("rule1", "rule1, rule2, rule3", "rule3, rule2"),
stringsAsFactors = FALSE
)
Run Code Online (Sandbox Code Playgroud)
规则是
rule1 <- function(data) {
data * 2
}
rule2 <- function(data) {
data + 1
}
rule3 <- function(data) {
data ^ 3
}
Run Code Online (Sandbox Code Playgroud)
对于数据框中的每一行,我想应用rules列中指定的所有规则.规则应该串联应用.
我想通了什么:
apply_rules <- function(data, rules) {
for (i in 1:length(data)) {
rules_now <- unlist(strsplit(rules[i], ", "))
for (j in 1:length(rules_now)) {
data[i] <- apply_rule(data[i], rules_now[j])
}
}
return(data)
} …Run Code Online (Sandbox Code Playgroud) 如何使用 将单元格包装在knitr::kable表格单元格中\n?
我想生成一个 .rmd 文件,其中包含一些表格,其中一列需要文本换行。应进行环绕的位置标有\n。我试过(这是一个独立的 .rmd 文件):
---
output: pdf_document
---
## A Table with text wrap
```{r cars}
knitr::kable(data.frame(col1 = c("a", "b"), col2 = c("one\ntwo", "three\nfour")))
```
Run Code Online (Sandbox Code Playgroud)
..但这不起作用。而不是留在col2,被包裹的部分生活在下一行col1。
预期输出为:
col1 | col2
-------------
a | one
| two
b | three
| four
Run Code Online (Sandbox Code Playgroud)
使用其他软件包的解决方案knitr是受欢迎的,只要它们允许打印(几乎)一样好。
我需要在布尔向量中找到第一个最长的 TRUE 序列。一些例子:
bool <- c(FALSE, TRUE, FALSE, TRUE)
# should become
c(FALSE, TRUE, FALSE, FALSE)
bool <- c(FALSE, TRUE, FALSE, TRUE, TRUE)
# should become
c(FALSE, FALSE, FALSE, TRUE, TRUE)
bool <- c(FALSE, TRUE, TRUE, FALSE, TRUE, TRUE)
# should become
c(FALSE, TRUE, TRUE, FALSE, FALSE, FALSE)
Run Code Online (Sandbox Code Playgroud)
除了上述示例中的第一个之外,此处的答案处理我的所有案例都是正确的。
我该如何改变
with(rle(bool), rep(lengths == max(lengths[values]) & values, lengths))
Run Code Online (Sandbox Code Playgroud)
以便它也处理上面的第一个示例是否正确?
我希望表格在一列中有项目符号并具有特定的表格宽度(以便在呈现为 PDF 时放置在一页上)。
如何通过rmarkdown使用众多软件包之一来实现这一目标?
到目前为止我尝试过的和拥有的:
---
output: pdf_document
---
```{r, include = FALSE}
df <- data.frame(col1 = "Some really long text here. I mean some reeeeeaaly loooong text. So long, it should be wrapped. Really.",
col2 = "* bullet point 1\n * bullet point 2", col3 = "Yes, there is still another column.")
```
# Attempt 1: kableExtra
```{r, echo = FALSE, warning = FALSE}
library(kableExtra)
df1 <- df
df1$col2 <- linebreak(df1$col2)
knitr::kable(df1, escape = FALSE) %>% column_spec(1, …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的字符串:
string <- c("A,1,some text,200", "B,2,some other text,300", "A,3,yet another one,100")
Run Code Online (Sandbox Code Playgroud)
所以每个向量元素都用逗号进一步划分.现在我只想在某个地方提取元素.让我们说出第一个逗号之前的所有元素或第二个逗号之后的所有元素.
以下代码执行我想要的操作:
sapply(strsplit(string, ","), function(x){return(x[[1]])})
# [1] "A" "B" "A"
sapply(strsplit(string, ","), function(x){return(x[[3]])})
# [1] "some text" "some other text" "yet another one"
Run Code Online (Sandbox Code Playgroud)
但是这个代码对我来说似乎相当复杂(考虑到问题的简单性).是否有更简洁的选择来实现我想要的?
ggplot2?I can only find the geom_curve function and that does define a segment by two points and the curvature argument.
Reproducible example:
df <- data.frame(
x = c(1,2,3),
y = c(2,2.5,1)
)
library(ggplot2)
p <- ggplot(data = df, aes(x = x, y = y)) + geom_point(col = "red") + xlim(0,4) + ylim(0,4)
p + geom_curve(aes(x = x[1], y = y[1], xend = x[3], yend = y[3])) …Run Code Online (Sandbox Code Playgroud) 据我ggplot2 所知,由绘制的标签的尺寸geom_text。否则,该check_overlap选项将不起作用。
这些维度存储在哪里,如何访问它们?
说明性例子
library(ggplot2)
df <- data.frame(x = c(1, 2),
y = c(1, 1),
label = c("label-one-that-might-overlap-another-label",
"label-two-that-might-overlap-another-label"),
stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)
使用check_overlap = FALSE(默认)标签会相互重叠。
ggplot(df, aes(x, y)) +
geom_text(aes(label = label)) +
xlim(0, 3)
Run Code Online (Sandbox Code Playgroud)
使用check_overlap = TRUE,不会绘制第二个标签,因为会ggplot发现重叠。
ggplot(df, aes(x, y)) +
geom_text(aes(label = label), check_overlap = TRUE) +
xlim(0, 3)
Run Code Online (Sandbox Code Playgroud)
怎么ggplot2知道那些标签重叠?我如何访问该信息?
我有一个数据框,我想在其中分隔包含月份和年份的列:
\nlibrary(tidyverse)\ndf <- data.frame(\n month_year = c("Januar / Janvier 1990", "Februar / F\xc3\xa9vrier 1990","M\xc3\xa4rz / Mars 1990")\n)\n\n# df\n# month_year\n# 1 Januar / Janvier 1990\n# 2 Februar / F\xc3\xa9vrier 1990\n# 3 M\xc3\xa4rz / Mars 1990\nRun Code Online (Sandbox Code Playgroud)\n以下内容有效,但看起来有点笨拙:
\ndf %>% \n separate(month_year, c("month","nothing","nothing2", "year"), sep = " ") %>%\n select(-starts_with("nothing"))\n\n# month year\n# 1 Januar 1990\n# 2 Februar 1990\n# 3 M\xc3\xa4rz 1990\nRun Code Online (Sandbox Code Playgroud)\n是否有更简洁的选择来达到相同的结果?
\n当写在RMarkdown我报告意识到,创下Ctrl+B了大胆的或Ctrl+I为斜体不会强调标记文本。许多其他 Markdown 编辑器(例如 StackOverflow 上的编辑器)都可以完成这项工作。
目前,我Shift+*在标记了text我想以粗体显示后使用的那一刻,返回*text*将以斜体显示的返回。然而,为了做出大胆的事情,我需要这样做两次。并且此过程无法返回操作(删除 **)。
RStudio 中是否有隐藏按钮可以打开此选项?或者是否有其他解决方案可以解决此问题,例如执行此工作的包?