0

我尝试用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:找不到函数“select”

感谢您的任何评论。

4

3 回答 3

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
于 2017-01-12T10:05:43.030 回答
0

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

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

尽管上面的示例使用随机数产生了行为,但它可能不适合您的需求 - 我想您希望将特征和行与temp.

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

于 2017-01-12T09:06:57.460 回答
0

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

select(iris, -one_of("Petal.Length"))  -> temp
于 2017-01-12T09:03:39.120 回答