在基本的R语言中,这个问题很简单,但我在使用dplyr时感到非常困惑(尽管总体来说dplyr让我的生活变得更好了!)。 假设您有以下的tibbles:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df1 <- tibble(x=seq(5)*19, a1=seq(5)*1, a2=seq(5)*2, a3=seq(5)*4)
df1
#> # A tibble: 5 x 4
#> x a1 a2 a3
#> <dbl> <dbl> <dbl> <dbl>
#> 1 19 1 2 4
#> 2 38 2 4 8
#> 3 57 3 6 12
#> 4 76 4 8 16
#> 5 95 5 10 20
df2 <- tibble(b1=3, b2=0.5, b3=10)
df2
#> # A tibble: 1 x 3
#> b1 b2 b3
#> <dbl> <dbl> <dbl>
#> 1 3 0.5 10
此内容由reprex包(v0.3.0)于2020-06-11创建。
然后我只想将df1中的a1替换为a1/b1,将a2替换为a2/b2等等。 这必须足够通用,以处理许多列的情况。 欢迎任何建议。
dplyr::transmute()
吗? - markhoguemutate(across(a1:a3))
不是正确的应用程序。 - Ronak Shah