我有以下示例数据集:
XYZ 185g
ABC 60G
Gha 20g
Run Code Online (Sandbox Code Playgroud)
如何删除字符串"185g", "60G", "20g"而不意外删除主单词中的字母 g 和 G?我尝试了下面的代码,但它也替换了主要单词中的字母。
a <- str_replace_all(a$words,"[0-9]"," ")
a <- str_replace_all(a$words,"[gG]"," ")
Run Code Online (Sandbox Code Playgroud) 我有一个 0-9 的整数向量,需要这些连续向量元素的所有唯一可能的组合,包括原始元素。
> vec <- 0:9
> vec
[1] 0 1 2 3 4 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)
任务类似于这个问题。主要(也是棘手的)区别是我只需要连续组合(例如"0", "01", "012", ... "0123456789", ... "1", ... "123456789")而不是非连续组合(例如"013")。
我将如何创建这个组合子集?
我在VSCode中使用时遇到了问题saveWidget,可能是pandoc.
当我在 Rstudio 中运行以下行时,它运行良好并且mtcars.html可以生成
htmlwidgets::saveWidget(DT::datatable(mtcars), "mtcars.html", selfcontained = TRUE, title = "mtcars")\nRun Code Online (Sandbox Code Playgroud)\n\n然而,当我将相同的代码移动到 VSCode 时,它给了我一个错误,说
\n\nError in htmlwidgets::saveWidget(DT::datatable(mtcars), "mtcars.html", : \n Saving a widget with selfcontained = TRUE requires pandoc. For details see:\nhttps://github.com/rstudio/rmarkdown/blob/master/PANDOC.md\nRun Code Online (Sandbox Code Playgroud)\n\n我怀疑 VSCode 无法识别 的路径pandoc,因为我输入find_pandocVScode 来查找版本和目录,显示
> rmarkdown::find_pandoc()\n$version\n[1] \'0\'\n\n$dir\nNULL\nRun Code Online (Sandbox Code Playgroud)\n\n然而,在 Rstudio 中它显示
\n\n> find_pandoc()\n$version\n[1] \xe2\x80\x982.7.2\xe2\x80\x99\n\n$dir\n[1] "C:/Program Files/RStudio/bin/pandoc"\nRun Code Online (Sandbox Code Playgroud)\n 我正在尝试在不使用 for 循环的情况下创建一个唯一的、随机分配的(无替换)组 ID。这是我得到的:
library(datasets)
library(dplyr)
data(iris)
iris <- iris %>% group_by(Species) %>% mutate(id = cur_group_id())
Run Code Online (Sandbox Code Playgroud)
这给了我每个 iris$Species 的组 id,但是,我希望组 id 从 c(1,2,3) 随机分配,而不是根据数据集的顺序分配。
任何帮助创建它都会非常有帮助!我确信有一种方法可以用 dplyr 做到这一点,但我很难过......
例如,假设您有以下数据框:
ID<-c("11", "12", "13", "14", "14")
Date<-c("2020-01-01", "2020-02-01", "2020-03-15", "2020-04-10", "2020-06-01")
Item<-c("Item1", "Item1", "Item2", "Item2", "Item2")
ItemPrice<-c(5, 5, 7, 7, 7)
Quantity<-c(1, 2, -2, 2, 3)
Cost<-c(5, 10, -14, 14, 21)
df<-data.frame(ID, Date, Item, ItemPrice, Quantity, Cost)
df
ID Date Item ItemPrice Quantity Cost
1 11 2020-01-01 Item1 5 1 5
2 12 2020-02-01 Item1 5 2 10
3 13 2020-03-15 Item2 7 -2 -14
4 14 2020-04-10 Item2 7 2 14
5 14 2020-06-01 Item2 7 3 21
Run Code Online (Sandbox Code Playgroud)
但是,您希望按以下方式分隔行, …
我的数据框看起来像这样
df <- data.frame(gene=c("A","B","C","A","B","D"),
origin=rep(c("old","new"),each=3),
value=sample(rnorm(10,2),6))
gene origin value
1 A old 1.5566908
2 B old 1.3000358
3 C old 0.7668213
4 A new 2.5274712
5 B new 2.2434525
6 D new 2.0758326
Run Code Online (Sandbox Code Playgroud)
我想找到两个不同的起源群体(旧的和新的)之间的共同基因
我希望我的数据看起来像这样
gene origin value
1 A old 1.5566908
2 B old 1.3000358
4 A new 2.5274712
5 B new 2.2434525
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。理想情况下,我想在使用多列的组中找到共同的行
我有一个像 DF
现在我想用 15 替换 Col B = NA,因为这是缺失值。C 列第一个 NA 为 14,第二个 NA 为 15。D 列第一个 NA 为 13,第二个 NA 为 14,第三个 NA 为 15。因此数字遵循从上到下或从下到上的顺序。
可重现的样本数据
structure(list(`Col A` = c(11, 12, 13, 14, 15), `Col B` = c(NA,
11, 12, 13, 14), `Col C` = c(NA, NA, 11, 12, 13), `Col D` = c(NA,
NA, NA, 11, 12)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud) 我有两列带有值 a 和 b。我想添加第三列c,即(在第i行)b的0到i的总和加上c的0到(i-1)的总和,乘以a,即
c_i = (sum_i (b) + sum_(i-1) (c) ) * a_i
Run Code Online (Sandbox Code Playgroud)
我试过
data %>%
mutate(
c = a * (cumsum(b) + lag(cumsum(c), default = 0))
)
Run Code Online (Sandbox Code Playgroud)
但是这不起作用,因为我只是根据目前不存在的 c 值创建 c :
Run Code Online (Sandbox Code Playgroud)Error: Problem with `mutate()` input `c`. x object 'c' not found
以前我使用 for 循环处理此类问题。不过,我习惯了dplyr,总有办法。但是,我不明白。
我很感激任何帮助!
编辑:在以前的版本中我是不准确的,因为 a 也是一个向量,而不是一个常数。我在公式里改了
所需的输出:
row 1: 0.5 * (7 + 0 ) =3.5
row 2: 0.3 * (7+1 + 3.5) = 3.45
row 3: 1.0 * (7+1+9 + 3.5+3.45) = 23.95 …Run Code Online (Sandbox Code Playgroud) 我有示例数据如下:
library(data.table)
sample <- fread("
1,0,2,NA,cat X, type 1
3,4,3,1,cat X, type 2
1,0,2,2,cat X, type 3
3,4,3,0,cat X, type 4
1,0,2,NA,cat Y, type 1
3,4,3,NA,cat Y, type 2
1,0,2,2,cat Y, type 3
3,4,3,35,cat Y, type 4
1,0,2,NA,cat X, type 1
3,4,3,1,cat X, type 2
1,0,2,2,cat X, type 3
3,4,3,NA,cat X, type 4
1,0,2,NA,cat Y, type 1
3,4,3,NA,cat Y, type 2
1,0,2,2,cat Y, type 3
3,4,3,1,cat Y, type 4
1,0,2,4,cat X, type 1
3,4,3,1,cat X, type 2
1,0,2,2,cat X, type …Run Code Online (Sandbox Code Playgroud) 我有一个这样的数据集:
data <- read.csv(text = "foo,bar
a,b
a,a
b,c
c,a
c,b")
Run Code Online (Sandbox Code Playgroud)
我想计算一个表,告诉我每个可能值出现的行数,所以像这样:
| 价值 | 数数 |
|---|---|
| A | 3 |
| 乙 | 3 |
| C | 3 |
我尝试使用 dplyr 按两列进行分组,然后进行汇总,但这不会为您提供每个值的计数,而是每个列值的计数。任何想法?
r ×10
dataframe ×5
dplyr ×4
tidyverse ×2
combinations ×1
data.table ×1
filtering ×1
imputation ×1
na ×1
pandoc ×1
performance ×1
recursion ×1
regex ×1
split ×1
vector ×1