> dput(ranks)
structure(c(NA, NA, 2L, 2L, NA, NA, NA, 2L, 1L), .Names = c("LANG1",
"LANG2", "LANG3", "LANG4", "LANG5", "LANG6", "LANG7", "LANG8",
"LANG9"))
> ranks
LANG1 LANG2 LANG3 LANG4 LANG5 LANG6 LANG7 LANG8 LANG9
NA NA 2 2 NA NA NA 2 1
Run Code Online (Sandbox Code Playgroud)
我有一个包含 9 个元素的向量,我想检查该向量中的所有元素是否相同。通常我只使用all(ranks == ranks[1]),但在这种情况下,由于第一个元素是NA,我没有得到 TRUE/FALSE 输出。
```{r results = 'asis'}
for(i in 1:10){
cat(paste("This is iteration number" i))
}
```
Run Code Online (Sandbox Code Playgroud)
在我输出生成的 .docx 文件后,文本默认以 12 号 Cambria 字体左对齐。如何在我的 .Rmd 文件中将对齐方式更改为居中、更改字体大小和字体类型?
```{r}
knitr::include_graphics(path = "~/Desktop/R/Files/apple.jpg/")
```
Run Code Online (Sandbox Code Playgroud)
上面的代码块工作正常。但是,当我创建一个for循环时,knitr::include_graphics似乎无法正常工作。
```{r}
fruits <- c("apple", "banana", "grape")
for(i in fruits){
knitr::include_graphics(path = paste("~/Desktop/R/Files/", i, ".jpg", sep = ""))
}
```
Run Code Online (Sandbox Code Playgroud) apple = data.frame(Obs = c(1:4), Color = c("red", "red", "red", "green"), Weight = c(1.1, 1.2, 1.3, 1.4))
orange = data.frame(Obs = c(1:6), Weight = c(2, 3, 4, 5, 6, 7))
Run Code Online (Sandbox Code Playgroud)
我有两个 data.framesapple和orange,其中后者的列是前者的子集。
> apple
Obs Color Weight
1 1 red 1.1
2 2 red 1.2
3 3 red 1.3
4 4 green 1.4
> orange
Obs Weight
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
6 6 7
Run Code Online (Sandbox Code Playgroud)
我想合并 …
mat <- structure(list(c(1, 2, 3, 4, 5), 2, c(3, 2, 1), numeric(0), numeric(0),
numeric(0), c(1, 2, 3, 6), c(1, 2, 3, 4, 5), 1, numeric(0),
numeric(0), numeric(0), c(3, 4, 2), 3, c(1, 2, 3, 4, 5),
numeric(0), numeric(0), numeric(0), numeric(0), numeric(0),
numeric(0), 1.358, numeric(0), numeric(0), numeric(0), numeric(0),
numeric(0), numeric(0), 0.0223257970827299, numeric(0), numeric(0),
numeric(0), numeric(0), numeric(0), numeric(0), 1.493), .Dim = c(6L,
6L))
> mat
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] Numeric,5 Numeric,4 Numeric,3 Numeric,0 Numeric,0 Numeric,0
[2,] 2 Numeric,5 3 …Run Code Online (Sandbox Code Playgroud) df <- data.frame(id = c(1, 1, 1, 2, 2),
gender = c("Female", "Female", "Male", "Female", "Male"),
variant = c("a", "b", "c", "d", "e"))
> df
id gender variant
1 1 Female a
2 1 Female b
3 1 Male c
4 2 Female d
5 2 Male e
Run Code Online (Sandbox Code Playgroud)
我想根据gender数据集中的列删除data.frame中的重复行。我知道(这里)有一个类似的问题,但是这里的区别是,我想删除数据集每个子集中的重复行,其中每个子集由一个unique定义id。
我想要的结果是这样的:
id gender variant
1 1 Female a
3 1 Male c
4 2 Female d
5 2 Male e
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下方法并且可以工作,但是我想知道是否存在更清洁,更有效的方法?
out = …Run Code Online (Sandbox Code Playgroud) mylist <- list(NULL, structure(list(Gender = structure(1L, .Label = "Female", class = "factor"),
ID = structure(1L, .Label = "1", class = "factor"), Class = structure(1L, .Label = "A", class = "factor"),
Score1 = 21.6, Score2 = 39.61, Score3 = 8.85,
Score4 = 13.66, Score5 = 2.64999999999999, Score6 = 6.94736842105265), .Names = c("Gender",
"ID", "Class", "Score1", "Score2", "Score3",
"Score4", "Score5", "Score6"), row.names = c(NA, -1L
), class = "data.frame"), list(structure(list(Gender = structure(1:2, .Label = c("Female",
"Male"), class = "factor"), ID = structure(c(1L, …Run Code Online (Sandbox Code Playgroud) mydat <- c(rep(4:10, each = 3), rep(1:2, each = 2))
barplot(table(mydat))
abline(v = 3, col = "blue")
Run Code Online (Sandbox Code Playgroud)
这给了我以下情节:
我想要一条垂直线x=3。但是,由于 x 轴的间隔方式,垂直线似乎不在正确的位置。我怎样才能解决这个问题?即我希望我的情节看起来像这样:
x = 1:3
y = 1:3
> expand.grid(x = 1:3, y = 1:3)
x y
1 1 1
2 2 1
3 3 1
4 1 2
5 2 2
6 3 2
7 1 3
8 2 3
9 3 3
Run Code Online (Sandbox Code Playgroud)
使用expand.grid给了我所有的组合。但是,我只想要成对比较,也就是说,我不想要 1 对 1、2 对、2 或 3 对 3 的比较。此外,我只想保留唯一的对,即我想保留1 对 2(而不是 2 对 1)。
总之,对于上述x和y,我想要以下 3 对组合:
x y
1 1 2
2 1 3
3 2 3
Run Code Online (Sandbox Code Playgroud)
同样,对于x = …
library(tidyverse)
nsource <- 5
mydata <- structure(list(p1 = c(0.153603238035774, 0.251732841184133),
p2 = c(0.0108935803858148, 0.12041117307058),
p3 = c(0.712158280629968, 0.257556345715588),
p4 = c(0.0529741150781822, 0.17690785400165),
p5 = c(0.0703707858702605, 0.193391786028049)),
row.names = 1:2, class = "data.frame")
mydata <- mydata %>%
rowwise %>%
mutate(P = sample(1:nsource, size = 1, replace = TRUE,
prob = c_across(matches('^p\\d+$')))) %>%
ungroup
> Error: `c_across()` must only be used inside dplyr verbs.
Run Code Online (Sandbox Code Playgroud)
我有一个数据集mydata
p1 p2 p3 p4 p5
1 0.1536032 0.01089358 0.7121583 0.05297412 0.07037079
2 0.2517328 0.12041117 0.2575563 0.17690785 …Run Code Online (Sandbox Code Playgroud) r ×10
dataframe ×2
knitr ×2
bar-chart ×1
combinations ×1
dplyr ×1
duplicates ×1
list ×1
matrix ×1
merge ×1
pairwise ×1
r-markdown ×1