Update a data.frame using specific integers for row numbers or a vectorised filter. Also supports dplyr groups. see Examples.
# S3 method for class 'data.frame'
update(object, rows, ...)
iris |>
update(3:4, Species = c("A", "B")) |>
head()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 A
#> 4 4.6 3.1 1.5 0.2 B
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
iris |>
update(Species == "setosa" & Sepal.Length > 5,
Species = "something else") |>
head()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 something else
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 something else
if (require("dplyr")) {
# also supports dplyr groups:
iris |>
group_by(Species) |>
# update every 2nd to 4th row in group
update(2:4, Species = "test") |>
# groups will be updated automatically
count()
}
#> # A tibble: 4 × 2
#> # Groups: Species [4]
#> Species n
#> <chr> <int>
#> 1 setosa 47
#> 2 test 9
#> 3 versicolor 47
#> 4 virginica 47