【问题标题】:How can I replace various columns in a tibble using select?如何使用 select 替换 tibble 中的各个列?
【发布时间】:2017-01-12 08:31:25
【问题描述】:

我尝试用相同大小的数据替换使用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

然后我得到错误

选择错误(iris,-one_of("Petal.Length")) = temp:找不到 函数“选择”

感谢任何cmets。

【问题讨论】:

  • 不太方便,但iris[,!grepl("Petal.Length",colnames(iris))]=tempworks
  • 不是R没有找到函数select,而是select<-rownames 想要的行为与 select 可以做的不同。
  • @Haboryme 谢谢。我知道 - 使用通常的 data.frame 方法,这是获得的方法。我只是认为有一种“整洁”的方式可以做到这一点。

标签: r tidyverse


【解决方案1】:

你想绑定两个数据框的列,所以你可以简单地使用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

【讨论】:

  • 也可以先绑定再选择,看我更新的例子。
  • bind != 替换;如果绑定是 OP 想要的,那么建议纠正这个问题......
  • 从技术上讲,您选择一个数据框的列并将它们与另一个数据框的列组合/绑定。当然,“精确”替换还需要检查已删除列的列索引,并对合并数据框重新排序,以便新列也位于已删除列的位置(这在删除列随机分布时很重要)。
【解决方案2】:

我们可以使用-&gt; 将输出分配给'temp'

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

【讨论】:

  • 谢谢,但我想在 tibble 中有 temp 而不是在 temp 中 tibble 的列。
【解决方案3】:

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

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

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

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

【讨论】:

  • 谢谢。我只想用 temp 中的值替换 tibble 中的列。
  • 那么评论中的@Haboryme 方法是最直接的方法。 (那么你为什么要首先使用select?)
  • 我想更换列,并且由于 tidyverse 方法被大量宣传,我想我会尝试坚持下去。
猜你喜欢
  • 1970-01-01
  • 2017-09-20
  • 2016-04-02
  • 2020-12-16
  • 1970-01-01
  • 2021-05-23
  • 2021-12-20
  • 1970-01-01
  • 2015-03-03
相关资源
最近更新 更多