R语言 将一个df中2行的值合并到另一个df中的现有行

g6baxovj  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(167)

我有下面的DF是基于虹膜DF在dplyr。seticolor的值是虹膜中“Setosa”和“versicolor”行的值的总和。

iris_3 <- read.table(header = TRUE, text = "
  species_id Species   Petal.Width
  1          1 virginica         2.5
  2          1 seticolor         1.6
  3          2 virginica         1.9
  4          2 seticolor         1.7
  5          3 virginica         2.1
  6          3 seticolor         1.7
")

现在我需要根据这个DF的行(没有花瓣)将这个DF与虹膜DF连接起来。虹膜中的长度列)。
我试过这个:

iris_merged <- iris_3 %>% left_join(select(iris,-(Petal.Length | Petal.Width)),
                                    by =c("Species","species_id"))

但后来我离开了很多NA的“seticolor”行。

species_id Species   Petal.Width Sepal.Length Sepal.Width
       <int> <chr>           <dbl>        <dbl>       <dbl>
1          1 virginica         2.5          6.3         3.3
2          1 seticolor         1.6         NA          NA  
3          2 virginica         1.9          5.8         2.7
4          2 seticolor         1.7         NA          NA  
5          3 virginica         2.1          7.1         3  
6          3 seticolor         1.7         NA          NA

我如何将iris中的“setosa”和“versicolor”行组合起来,以匹配当前DF中的“seticolor”行?有没有可能在不创建辅助DF的情况下?

mu0hgdu0

mu0hgdu01#

您可以使用{powerjoin}的开发版本。如果您不需要两个版本的“Species”列,请使用keep = "left"keep = "right"

# remotes::install_github("moodymudskipper/powerjoin")
library(dplyr, w = F)
library(powerjoin)
power_left_join(
  iris_3, 
  select(iris,-(Petal.Length | Petal.Width)),
  by = ~ .x$Species == .y$Species | (.x$Species == "seticolor" & .y$Species %in.% c("versicolor", "setosa")), 
)
#>     species_id Species.x Petal.Width Sepal.Length Sepal.Width  Species.y
#> 1            1 virginica         2.5          6.3         3.3  virginica
#> 2            1 virginica         2.5          5.8         2.7  virginica
#> 3            1 virginica         2.5          7.1         3.0  virginica
#> 4            1 virginica         2.5          6.3         2.9  virginica
#> 5            1 virginica         2.5          6.5         3.0  virginica
#> 6            1 virginica         2.5          7.6         3.0  virginica
#> 7            1 virginica         2.5          4.9         2.5  virginica
#> 8            1 virginica         2.5          7.3         2.9  virginica
#> 9            1 virginica         2.5          6.7         2.5  virginica
#> 10           1 virginica         2.5          7.2         3.6  virginica
#> 11           1 virginica         2.5          6.5         3.2  virginica
#> 12           1 virginica         2.5          6.4         2.7  virginica
#> 13           1 virginica         2.5          6.8         3.0  virginica
#> 14           1 virginica         2.5          5.7         2.5  virginica
#> 15           1 virginica         2.5          5.8         2.8  virginica
#> 16           1 virginica         2.5          6.4         3.2  virginica
#> 17           1 virginica         2.5          6.5         3.0  virginica
#> 18           1 virginica         2.5          7.7         3.8  virginica
#> 19           1 virginica         2.5          7.7         2.6  virginica
#> 20           1 virginica         2.5          6.0         2.2  virginica
#> 21           1 virginica         2.5          6.9         3.2  virginica
#> 22           1 virginica         2.5          5.6         2.8  virginica
#> 23           1 virginica         2.5          7.7         2.8  virginica
#> 24           1 virginica         2.5          6.3         2.7  virginica
#> 25           1 virginica         2.5          6.7         3.3  virginica
#> 26           1 virginica         2.5          7.2         3.2  virginica
#> 27           1 virginica         2.5          6.2         2.8  virginica
#> 28           1 virginica         2.5          6.1         3.0  virginica
#> 29           1 virginica         2.5          6.4         2.8  virginica
#> 30           1 virginica         2.5          7.2         3.0  virginica
#> 31           1 virginica         2.5          7.4         2.8  virginica
#> 32           1 virginica         2.5          7.9         3.8  virginica
#> 33           1 virginica         2.5          6.4         2.8  virginica
#> 34           1 virginica         2.5          6.3         2.8  virginica
#> 35           1 virginica         2.5          6.1         2.6  virginica
#> 36           1 virginica         2.5          7.7         3.0  virginica
#> 37           1 virginica         2.5          6.3         3.4  virginica
#> 38           1 virginica         2.5          6.4         3.1  virginica
#> 39           1 virginica         2.5          6.0         3.0  virginica
#> 40           1 virginica         2.5          6.9         3.1  virginica
#> 41           1 virginica         2.5          6.7         3.1  virginica
#> 42           1 virginica         2.5          6.9         3.1  virginica
#> 43           1 virginica         2.5          5.8         2.7  virginica
#> 44           1 virginica         2.5          6.8         3.2  virginica
#> 45           1 virginica         2.5          6.7         3.3  virginica
#> 46           1 virginica         2.5          6.7         3.0  virginica
#> 47           1 virginica         2.5          6.3         2.5  virginica
#> 48           1 virginica         2.5          6.5         3.0  virginica
#> 49           1 virginica         2.5          6.2         3.4  virginica
#> 50           1 virginica         2.5          5.9         3.0  virginica
#> 51           1 seticolor         1.6          5.1         3.5     setosa
#> 52           1 seticolor         1.6          4.9         3.0     setosa
#> 53           1 seticolor         1.6          4.7         3.2     setosa
#> 54           1 seticolor         1.6          4.6         3.1     setosa
#> 55           1 seticolor         1.6          5.0         3.6     setosa
#> 56           1 seticolor         1.6          5.4         3.9     setosa
#> 57           1 seticolor         1.6          4.6         3.4     setosa
#> 58           1 seticolor         1.6          5.0         3.4     setosa
#> 59           1 seticolor         1.6          4.4         2.9     setosa
#> 60           1 seticolor         1.6          4.9         3.1     setosa
#> 61           1 seticolor         1.6          5.4         3.7     setosa
#> 62           1 seticolor         1.6          4.8         3.4     setosa
#> 63           1 seticolor         1.6          4.8         3.0     setosa
#> 64           1 seticolor         1.6          4.3         3.0     setosa
#> 65           1 seticolor         1.6          5.8         4.0     setosa
#> 66           1 seticolor         1.6          5.7         4.4     setosa
#> 67           1 seticolor         1.6          5.4         3.9     setosa
#> 68           1 seticolor         1.6          5.1         3.5     setosa
#> 69           1 seticolor         1.6          5.7         3.8     setosa
#> 70           1 seticolor         1.6          5.1         3.8     setosa
#> 71           1 seticolor         1.6          5.4         3.4     setosa
#> 72           1 seticolor         1.6          5.1         3.7     setosa
#> 73           1 seticolor         1.6          4.6         3.6     setosa
#> 74           1 seticolor         1.6          5.1         3.3     setosa
#> 75           1 seticolor         1.6          4.8         3.4     setosa
#> 76           1 seticolor         1.6          5.0         3.0     setosa
#> 77           1 seticolor         1.6          5.0         3.4     setosa
#> 78           1 seticolor         1.6          5.2         3.5     setosa
#> 79           1 seticolor         1.6          5.2         3.4     setosa
#> 80           1 seticolor         1.6          4.7         3.2     setosa
#> 81           1 seticolor         1.6          4.8         3.1     setosa
#> 82           1 seticolor         1.6          5.4         3.4     setosa
#> 83           1 seticolor         1.6          5.2         4.1     setosa
#> 84           1 seticolor         1.6          5.5         4.2     setosa
#> 85           1 seticolor         1.6          4.9         3.1     setosa
#> 86           1 seticolor         1.6          5.0         3.2     setosa
#> 87           1 seticolor         1.6          5.5         3.5     setosa
#> 88           1 seticolor         1.6          4.9         3.6     setosa
#> 89           1 seticolor         1.6          4.4         3.0     setosa
#> 90           1 seticolor         1.6          5.1         3.4     setosa
#> 91           1 seticolor         1.6          5.0         3.5     setosa
#> 92           1 seticolor         1.6          4.5         2.3     setosa
#> 93           1 seticolor         1.6          4.4         3.2     setosa
#> 94           1 seticolor         1.6          5.0         3.5     setosa
#> 95           1 seticolor         1.6          5.1         3.8     setosa
#> 96           1 seticolor         1.6          4.8         3.0     setosa
#> 97           1 seticolor         1.6          5.1         3.8     setosa
#> 98           1 seticolor         1.6          4.6         3.2     setosa
#> 99           1 seticolor         1.6          5.3         3.7     setosa
#> 100          1 seticolor         1.6          5.0         3.3     setosa
#> 101          1 seticolor         1.6          7.0         3.2 versicolor
#> 102          1 seticolor         1.6          6.4         3.2 versicolor
#> 103          1 seticolor         1.6          6.9         3.1 versicolor
#> 104          1 seticolor         1.6          5.5         2.3 versicolor
#> 105          1 seticolor         1.6          6.5         2.8 versicolor
#> 106          1 seticolor         1.6          5.7         2.8 versicolor
#> 107          1 seticolor         1.6          6.3         3.3 versicolor
#> 108          1 seticolor         1.6          4.9         2.4 versicolor
#> 109          1 seticolor         1.6          6.6         2.9 versicolor
#> 110          1 seticolor         1.6          5.2         2.7 versicolor
#> 111          1 seticolor         1.6          5.0         2.0 versicolor
#> 112          1 seticolor         1.6          5.9         3.0 versicolor
#> 113          1 seticolor         1.6          6.0         2.2 versicolor
#> 114          1 seticolor         1.6          6.1         2.9 versicolor
#> 115          1 seticolor         1.6          5.6         2.9 versicolor
#> 116          1 seticolor         1.6          6.7         3.1 versicolor
#> 117          1 seticolor         1.6          5.6         3.0 versicolor
#> 118          1 seticolor         1.6          5.8         2.7 versicolor
#> 119          1 seticolor         1.6          6.2         2.2 versicolor
#> 120          1 seticolor         1.6          5.6         2.5 versicolor
#> 121          1 seticolor         1.6          5.9         3.2 versicolor
#> 122          1 seticolor         1.6          6.1         2.8 versicolor
#> 123          1 seticolor         1.6          6.3         2.5 versicolor
#> 124          1 seticolor         1.6          6.1         2.8 versicolor
#> 125          1 seticolor         1.6          6.4         2.9 versicolor
#> 126          1 seticolor         1.6          6.6         3.0 versicolor
#> 127          1 seticolor         1.6          6.8         2.8 versicolor
#> 128          1 seticolor         1.6          6.7         3.0 versicolor
#> 129          1 seticolor         1.6          6.0         2.9 versicolor
#> 130          1 seticolor         1.6          5.7         2.6 versicolor
#> 131          1 seticolor         1.6          5.5         2.4 versicolor
#> 132          1 seticolor         1.6          5.5         2.4 versicolor
#> 133          1 seticolor         1.6          5.8         2.7 versicolor
#> 134          1 seticolor         1.6          6.0         2.7 versicolor
#> 135          1 seticolor         1.6          5.4         3.0 versicolor
#> 136          1 seticolor         1.6          6.0         3.4 versicolor
#> 137          1 seticolor         1.6          6.7         3.1 versicolor
#> 138          1 seticolor         1.6          6.3         2.3 versicolor
#> 139          1 seticolor         1.6          5.6         3.0 versicolor
#> 140          1 seticolor         1.6          5.5         2.5 versicolor
#> 141          1 seticolor         1.6          5.5         2.6 versicolor
#> 142          1 seticolor         1.6          6.1         3.0 versicolor
#> 143          1 seticolor         1.6          5.8         2.6 versicolor
#> 144          1 seticolor         1.6          5.0         2.3 versicolor
#> 145          1 seticolor         1.6          5.6         2.7 versicolor
#> 146          1 seticolor         1.6          5.7         3.0 versicolor
#> 147          1 seticolor         1.6          5.7         2.9 versicolor
#> 148          1 seticolor         1.6          6.2         2.9 versicolor
#> 149          1 seticolor         1.6          5.1         2.5 versicolor
#> 150          1 seticolor         1.6          5.7         2.8 versicolor
#> ...

相关问题