如何仅对 R dplyr 中数据帧的特定单元格进行操作

How to do operations only on specific cells of dataframe in R dplyr

我下面有数据框,我想保持除最后一个单元格外的所有内容不变。
我希望它四舍五入到最接近的整数。有什么方法可以使用 dplyr 定位一个或多个特定细胞吗?

    df <- data.frame(id = c(rep(101, 4), rep(202, 2), "tot"),
                status = c("a","b","c","d", "a", "b", "cccc"),
                wt = c(100,200,100,105, 20,22,10000),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.9))


> df
   id status    wt  ht
1 101      a   100 5.3
2 101      b   200 5.2
3 101      c   100 5.0
4 101      d   105 5.1
5 202      a    20 4.3
6 202      b    22 4.2
7 tot   cccc 10000 4.9

我想要的输出是:

df[df$id=="tot", 4] <- round(df[df$id=="tot", 4])
> df
   id status    wt  ht
1 101      a   100 5.3
2 101      b   200 5.2
3 101      c   100 5.0
4 101      d   105 5.1
5 202      a    20 4.3
6 202      b    22 4.2
7 tot   cccc 10000 5.0

可能的解决方案,基于dplyr

library(dplyr)

df %>% 
  mutate(ht = if_else(row_number() == n(), round(ht,0), ht))

#>    id status    wt  ht
#> 1 101      a   100 5.3
#> 2 101      b   200 5.2
#> 3 101      c   100 5.0
#> 4 101      d   105 5.1
#> 5 202      a    20 4.3
#> 6 202      b    22 4.2
#> 7 tot   cccc 10000 5.0