【问题标题】:R Assign Values based on multiple conditionsR根据多个条件分配值
【发布时间】:2021-11-09 06:17:22
【问题描述】:

我想根据其他两个中的条件填充现有列。

数据框称为 A。

如果列 box=6 AND document = 75 那么 size= big.

我只需要填充空单元格的大小。该列中的现有条目需要保留。

示例数据:

Box      Document         Size
6          75
6          75
7          23              big
7          23              big
7          25
8          13              big
8          13              big

谢谢

--

R 格式的数据集(dput(A) 的输出):

A <- structure(
  list(
    Box = c(6, 6, 7, 7, 7, 8, 8),
    Document = c(75, 75, 23, 23, 25, 13, 13),
    Size = c("", "", "big", "big", "", "big", "big")),
  row.names = c(NA,-7L),
  class = c("tbl_df", "tbl", "data.frame")
)

【问题讨论】:

标签: r


【解决方案1】:

如果没有更多细节(即可重现的示例),很难知道这是否回答了您的问题,但这里有一个潜在的解决方案:

set.seed(3)
A <- data.frame(box = sample(5:7, size = 50, replace = TRUE),
                document = sample(74:76, size = 50, replace = TRUE))

A$size <- ifelse(A$box == 6 & A$document == 75, "big", "other")
head(A, 10)
#>    box document  size
#> 1    5       76 other
#> 2    6       76 other
#> 3    7       75 other
#> 4    6       75   big
#> 5    7       74 other
#> 6    7       76 other
#> 7    6       75   big
#> 8    7       74 other
#> 9    5       75 other
#> 10   6       74 other

另一个可能的解决方案是使用 dplyr 包中的case_when():

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

A <- data.frame(box = sample(5:7, size = 50, replace = TRUE),
                document = sample(74:76, size = 50, replace = TRUE))
A %>%
  mutate(size = case_when(box == 6 & document == 75 ~ "big",
                          box < 6 & document < 75 ~ "small",
                          document < 75 ~ "medium",
                          TRUE ~ "other"))
#>    box document   size
#> 1    6       75    big
#> 2    6       74 medium
#> 3    7       76  other
#> 4    5       75  other
#> 5    6       76  other
#> 6    5       75  other
#> 7    6       76  other
#> 8    5       74  small
#> 9    5       74  small
#> 10   6       74 medium
#> ...

由reprex package 创建于 2021-11-08 (v2.0.1)

编辑

解决您对“空白”的评论,如果它们不是“NA”:

A <- structure(
  list(
    Box = c(6, 6, 7, 7, 7, 8, 8),
    Document = c(75, 75, 23, 23, 25, 13, 13),
    Size = c("", "", "big", "big", "", "big", "big")),
  row.names = c(NA,-7L),
  class = c("tbl_df", "tbl", "data.frame")
)
A
#> # A tibble: 7 × 3
#>     Box Document Size 
#>   <dbl>    <dbl> <chr>
#> 1     6       75 ""   
#> 2     6       75 ""   
#> 3     7       23 "big"
#> 4     7       23 "big"
#> 5     7       25 ""   
#> 6     8       13 "big"
#> 7     8       13 "big"

A$Size <- ifelse(A$Box == 6 & A$Document == 75, "big", A$Size)
A
#> # A tibble: 7 × 3
#>     Box Document Size 
#>   <dbl>    <dbl> <chr>
#> 1     6       75 "big"
#> 2     6       75 "big"
#> 3     7       23 "big"
#> 4     7       23 "big"
#> 5     7       25 ""   
#> 6     8       13 "big"
#> 7     8       13 "big"

由reprex package (v2.0.1) 于 2021-11-09 创建

【讨论】:

  • 第一个选项有效 A$size
  • 什么是“空”?你的意思是“NA”吗?如果是这样,也许A$size &lt;- ifelse(is.na(A$size) &amp; A$box == 6 &amp; A$document == 75, "big", A$size)
  • 我编辑了我的帖子以包含数据样本。此命令 A$size
  • 我已经编辑了我的答案;这能解决你的问题@CarmelHilal 吗?
  • 谢谢贾里德。确实有效。
猜你喜欢
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多