library(dplyr)
tib <- tibble(a = c(1,2,3))
Run Code Online (Sandbox Code Playgroud)
以下工作如预期:
tib %>% mutate(b = a^2, c = sqrt(b))
# A tibble: 3 x 3
a b c
<dbl> <dbl> <dbl>
1 1 1 1
2 2 4 2
3 3 9 3
tib %>% mutate(b = a^2, c = sum(a))
# A tibble: 3 x 3
a b c
<dbl> <dbl> <dbl>
1 1 1 6
2 2 4 6
3 3 9 6
tib %>% mutate(b = a^2) %>% mutate(c = sum(b)) …
Run Code Online (Sandbox Code Playgroud) 我试图使用mutate为变量分配列名.
df <-data.frame(x = sample(1:100, 50), y = rnorm(50))
new <- function(name){
df%>%mutate(name = ifelse(x <50, "small", "big"))
}
Run Code Online (Sandbox Code Playgroud)
我跑的时候
new(name = "newVar")
Run Code Online (Sandbox Code Playgroud)
它不起作用.我知道 mutate_()
可以帮助,但我正在努力与它一起使用它ifelse
.
任何帮助,将不胜感激.
假设我有一个像
term cnt
apple 10
apples 5
a apple on 3
blue pears 3
pears 1
Run Code Online (Sandbox Code Playgroud)
如何过滤此列中的所有部分找到的字符串,例如得到结果
term cnt
apple 10
pears 1
Run Code Online (Sandbox Code Playgroud)
无需指出我要过滤的字词(主语),而是通过自引用方式(即,它会针对整个列检查每个字词,并删除部分匹配的字词)。令牌的数量不受限制,字符串的一致性也不受限制(即“ apples”将与“ apple”匹配)。这将导致基于dplyr的广义反向版本
d[grep("^apple$|^pears$", d$term), ]
Run Code Online (Sandbox Code Playgroud)
另外,有趣的是,使用这种去部门化来求和,例如
term cnt
apple 18
pears 4
Run Code Online (Sandbox Code Playgroud)
我无法使其与contains()或grep()一起使用。
谢谢
我一直在dplyr::mutate_at
尝试通过对某些列应用相同的函数来创建新变量。当我在参数中命名我的函数时.funs
,mutate 调用会创建带有后缀的新列,而不是替换现有列,这是我在此线程中发现的一个很酷的选项。
df = data.frame(var1=1:2, var2=4:5, other=9)
df %>% mutate_at(vars(contains("var")), .funs=funs('sqrt'=sqrt))
#### var1 var2 other var1_sqrt var2_sqrt
#### 1 1 4 9 1.000000 2.000000
#### 2 2 5 9 1.414214 2.236068
Run Code Online (Sandbox Code Playgroud)
但是,我注意到当vars
用于指向我的列的参数只返回一列而不是几列时,生成的新列会删除初始名称:它被命名sqrt
而不是在other_sqrt
这里:
df %>% mutate_at(vars(contains("other")), .funs=funs('sqrt'=sqrt))
#### var1 var2 other sqrt
#### 1 1 4 9 3
#### 2 2 5 9 3
Run Code Online (Sandbox Code Playgroud)
我想了解为什么会发生这种行为,以及如何避免它,因为我事先不知道contains()
将返回多少列。
编辑:新创建的列必须继承原始列的原始名称,加上末尾的后缀“sqrt”。
谢谢
我是这个网站的新手,也是编码的新手。我想知道你们中是否有人可以帮助我
我需要通过评分分布计算前 5 部电影,计算每部电影 4 星或更高评分的百分比。
到目前为止,我只能使用 dplyr 计算出现次数。
是否可以使用 dplyr (类似于我的编码)来计算它?
我不确定我是否需要变异来提出解决方案,或者是否有另一种方法可以这样做。
到目前为止我的代码:
dfAux1 <- na.omit(dfAux)
dfAux1 %>%
group_by(movie) %>%
summarise(tot = n()) %>%
arrange(desc(tot))%>%
head(5)
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的:
**Expected result**:
0.7000000, 'The Shawshank Redemption'
0.5333333, 'Star Wars IV - A New Hope'
0.5000000, 'Gladiator'
0.4444444, 'Blade Runner'
0.4375000, 'The Silence of the Lambs'
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的结果:
# A tibble: 5 x 2
movie tot
<fctr> <int>
1 Toy Story 17
2 The Silence of the Lambs 16
3 Star Wars IV - …
Run Code Online (Sandbox Code Playgroud) 目前我必须使用add_column
将新列直接插入到所需位置,或者使用mutate
, 然后select
使用新的所需列顺序。
mips.group <- str_extract(mips.manifest$PlateName, "[:alnum:]+_([[:alnum:]&&[^P]]+(_CL)?)?|(KORgex)")
mips.manifest %<>%
add_column(MIPSGroup=mips.group, .after="PlateName")
Run Code Online (Sandbox Code Playgroud)
是否可以直接告诉mutate
在哪里添加新列,如果没有,是否有原因?
以以下示例数据为例:
set.seed(1)
foo <- data.frame(x=rnorm(10, 0, 10), y=rnorm(10, 0, 10), fac = c(rep("A", 5), rep("B", 5)))
Run Code Online (Sandbox Code Playgroud)
我想通过变量“fac”将数据帧“foo”拆分为 A 和 B,应用返回每个子组长度向量的函数(马氏距离),然后将输出变异回原始数据帧。例如:
auto.mahalanobis <- function(x) {
temp <- x[, c("x", "y")]
return(mahalanobis(temp, center = colMeans(temp, na.rm=T), cov = cov(temp,
use="pairwise.complete.obs")))
}
foo %>% group_by(fac) %>%
mutate(mahal = auto.mahalanobis(.))
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误。显然,可以通过拆分数据集、应用函数并将输出添加为一列,然后再将其全部重新组合在一起来手动完成此过程。但是必须有更有效的方法来做到这一点(也许这是对 dplyr 的滥用?)。
我有一个数据集,我尝试使用if else语句基于不同的变量来更改某些变量的值。但是,我只想在满足特定条件时更改变量-否则我希望变量保持不变。如何在dplyr中执行此操作?
例如,如果我有4个站点(a,b,c和d),分别与10、20、30和40的值相关联,而我只想将站点a的10的值更改为12 。
df2 <- df %>%
mutate(lat = ifelse(site == "a", 12, WHAT GOES HERE?))
Run Code Online (Sandbox Code Playgroud) id first middle last Age
1 Carol Jenny Smith 15
2 Sarah Carol Roberts 20
3 Josh David Richardson 22
Run Code Online (Sandbox Code Playgroud)
我正在尝试在任何名称列(第一,中间,最后)中找到一个特定的名称。例如,如果我找到了一个名字叫Carol的人(无论名字/中间名/姓氏都没关系),我想对“ Carol”列进行突变并给出1。所以我想要的是以下内容
id first middle last Age Carol
1 Carol Jenny Smith 15 1
2 Sarah Carol Roberts 20 1
3 Josh David Richardson 22 0
Run Code Online (Sandbox Code Playgroud)
我一直在尝试ifelse(c(first,middle,last)==“ Carol”,1,0)或“ Carol”%in%首先...等,但是由于某种原因,我只能处理一个列而不是多个列专栏..有人可以帮我吗?先感谢您!
我有一个小标题:
df = tibble(one = list('a', 'b'), two = list(c('p1', 'p2', 'p3'), NA_character_), three = list(NA_character_, c('z1', 'z2', 'z3')))
df
# A tibble: 2 x 3
one two three
<chr> <list> <list>
1 a <chr [3]> <chr [1]>
2 b <chr [1]> <chr [3]>
Run Code Online (Sandbox Code Playgroud)
我想替换丢失的值的列two
和three
与列的值one
使用coalesce()
,然后在折叠每个字符向量(横行)two
和three
成使用单个串toString()
。我的预期输出如下所示:
df = tibble(one = c('a', 'b'), two = list('p1, p2, p3', 'b'), three = list('a', 'z1, z2, z3'))
df
# A …
Run Code Online (Sandbox Code Playgroud)