如何使用 select 替换 tibble 中的各个列?

How can I replace various columns in a tibble using select?

我尝试用相同大小的数据替换使用 select 选择的所有列。 一个可重现的例子是

library(tidyverse)
iris = as_data_frame(iris)
temp = cbind( runif(nrow(iris)), runif(nrow(iris)), runif(nrow(iris)), runif(nrow(iris)))
select(iris, -one_of("Petal.Length"))  = temp

然后我得到错误

Error in select(iris, -one_of("Petal.Length")) = temp : could not find function "select"

感谢任何评论。

我们可以使用->将输出分配给'temp'

select(iris, -one_of("Petal.Length"))  -> temp

使用 tidyverse 范例,您可以使用:

dplyr::mutate_at(iris, vars(-one_of("Petal.Length")), .funs = funs(runif))

虽然上面的示例产生了随机数的行为,但它可能不适合您的需要 - 我想您想要匹配特征和行到 temp 中的那个。

这可以通过将 iris 和 temp 转换为长格式并相应地使用 *join 方法连接和替换数据来完成。

你想绑定两个数据框的列,所以你可以简单地使用bind_cols():

library(tidyverse)

iris <- as_tibble(iris)
temp <- tibble(r1 = runif(nrow(iris)), r2 = runif(nrow(iris)), r3 = runif(nrow(iris)), r4 = runif(nrow(iris)))

select(iris, -Petal.Length) %>% bind_cols(temp)
# or use:
# bind_cols(iris, temp) %>% select(-Petal.Length)

这给你:

# A tibble: 150 × 8
   Sepal.Length Sepal.Width Petal.Width Species        r1        r2         r3        r4
          <dbl>       <dbl>       <dbl>  <fctr>     <dbl>     <dbl>      <dbl>     <dbl>
1           5.1         3.5         0.2  setosa 0.7208566 0.1367070 0.04314771 0.4909396
2           4.9         3.0         0.2  setosa 0.4101884 0.4795735 0.75318182 0.1463689
3           4.7         3.2         0.2  setosa 0.6270065 0.5425814 0.26599432 0.1467248
4           4.6         3.1         0.2  setosa 0.8001282 0.4691908 0.73060637 0.0792256
5           5.0         3.6         0.2  setosa 0.5663895 0.4745482 0.65088630 0.5360953
6           5.4         3.9         0.4  setosa 0.8813042 0.1560600 0.41734507 0.2582568
7           4.6         3.4         0.3  setosa 0.5046977 0.9555570 0.22118401 0.9246906
8           5.0         3.4         0.2  setosa 0.5283764 0.4730212 0.24982471 0.6313071
9           4.4         2.9         0.2  setosa 0.5976045 0.4717439 0.14270551 0.2149888
10          4.9         3.1         0.1  setosa 0.3919660 0.5125420 0.95001067 0.5259598
# ... with 140 more rows