使用数据框中的特定标识符进行有条件突变

发布于 2025-02-13 01:39:33 字数 667 浏览 1 评论 0原文

我有一个数据集,该数据集采用了类似的方法。

ID <- c(4,5,6,7,3,8,9)
quantity <- c(20,300, 350, 21, 44, 20, 230)
measurementvalue <- c("tin", "kg","kg","tin","tin","tin","kg")
kgs <- c(21,12, 30,23,33,11,24)

DF <- data.frame(ID, quantity, measurementvalue)

我使用条件突变的新列Totalkgs的标准方法是以下代码。

DF <- DF %>%
  mutate(totalkgs = 
           ifelse(quantity == "tin", quantity * 5, 
                  ifelse(quantity =="kg", quantity*1, quantity)))

但是,该数据集在列数量中具有错误的条目,因此我想对这些特定标识符执行划分。乘法和除法的所有最终值都应存储在列totalkgs中。我该怎么做?

让我们假设带有错误数据的ID为3,5,9,7。我想将列数量中的值与10分开。

I have a dataset that employs a similar approach to this one.

ID <- c(4,5,6,7,3,8,9)
quantity <- c(20,300, 350, 21, 44, 20, 230)
measurementvalue <- c("tin", "kg","kg","tin","tin","tin","kg")
kgs <- c(21,12, 30,23,33,11,24)

DF <- data.frame(ID, quantity, measurementvalue)

My standard way of deriving a new column totalkgs using conditional mutating is the code below.

DF <- DF %>%
  mutate(totalkgs = 
           ifelse(quantity == "tin", quantity * 5, 
                  ifelse(quantity =="kg", quantity*1, quantity)))

However, the dataset had erroneous entries in the column quantity so I'd like to perform a division on those specific identifiers. All the final values of both the multiplication and division should be store in the column totalkgs. How do I go about this?

Let's assume the id's with the error data are 3,5,9,7. and I'd like to divide the values found in the column quantity with 10.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2025-02-20 01:39:33

您可以使用case_when

ID <- c(4,5,6,7,3,8,9)
quantity <- c(20,300, 350, 21, 44, 20, 230)
measurementvalue <- c("tin", "kg","kg","tin","tin","tin","kg")
kgs <- c(21,12, 30,23,33,11,24)
DF <- data.frame(ID, quantity, measurementvalue)

library(dplyr)
DF %>%
  mutate(quantity2 = ifelse(ID %in% c(3,5,9,7), quantity/10, quantity)) %>%
  mutate(totalkgs = case_when(measurementvalue == "tin" ~ quantity2 * 5,
                              measurementvalue == "kg" ~ quantity2 * 1, 
                              TRUE ~ quantity2)) %>%
  select(-quantity2) #if you want
#>   ID quantity measurementvalue totalkgs
#> 1  4       20              tin    100.0
#> 2  5      300               kg     30.0
#> 3  6      350               kg    350.0
#> 4  7       21              tin     10.5
#> 5  3       44              tin     22.0
#> 6  8       20              tin    100.0
#> 7  9      230               kg     23.0

在2022-07-05创建的(v2.0.1)

You could use case_when:

ID <- c(4,5,6,7,3,8,9)
quantity <- c(20,300, 350, 21, 44, 20, 230)
measurementvalue <- c("tin", "kg","kg","tin","tin","tin","kg")
kgs <- c(21,12, 30,23,33,11,24)
DF <- data.frame(ID, quantity, measurementvalue)

library(dplyr)
DF %>%
  mutate(quantity2 = ifelse(ID %in% c(3,5,9,7), quantity/10, quantity)) %>%
  mutate(totalkgs = case_when(measurementvalue == "tin" ~ quantity2 * 5,
                              measurementvalue == "kg" ~ quantity2 * 1, 
                              TRUE ~ quantity2)) %>%
  select(-quantity2) #if you want
#>   ID quantity measurementvalue totalkgs
#> 1  4       20              tin    100.0
#> 2  5      300               kg     30.0
#> 3  6      350               kg    350.0
#> 4  7       21              tin     10.5
#> 5  3       44              tin     22.0
#> 6  8       20              tin    100.0
#> 7  9      230               kg     23.0

Created on 2022-07-05 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文