首页 > 解决方案 > 根据数据框 R 中的另一列更改列中的值

问题描述

    df ex:
# A tibble: 20 x 2
   Autoswing `Duration(seconds)`
   <chr>                   <dbl>
 1 ✗                           4
 2 ✗                           5
 3 ✗                           7
 4 ✗                           6
 5 ✗                           8
 6 ✗                           9
 7 ✗                          11
 8 ✗                          16
 9 ✗                          16
10 ✗                           8
11 ✗                          12
12 ✗                          15
13 ✗                          10
14 ✗                         Inf
15 ✗                          14
16 ✗                         Inf
17 ✗                         Inf
18 ✗                         Inf
19 ✗                         Inf
20 ✗                         Inf

如果 Duration(seconds) 为 inf,我想在 Autoswing 中将所有 ✗ 更改为 ✓。我试图使用 if 语句

标签: rdataframeif-statement

解决方案


我们可以使用base R方法,即检查无限值is.infinite来创建一个逻辑向量并进行赋值

df$Autoswing[is.infinite(df$`Duration(seconds)`)] <- "✓&quot;

-输出

> df
# A tibble: 20 × 2
   Autoswing `Duration(seconds)`
   <chr>                   <dbl>
 1 ✗                           4
 2 ✗                           5
 3 ✗                           7
 4 ✗                           6
 5 ✗                           8
 6 ✗                           9
 7 ✗                          11
 8 ✗                          16
 9 ✗                          16
10 ✗                           8
11 ✗                          12
12 ✗                          15
13 ✗                          10
14 ✓                         Inf
15 ✗                          14
16 ✓                         Inf
17 ✓                         Inf
18 ✓                         Inf
19 ✓                         Inf
20 ✓                         Inf

或与data.table

library(data.table)
setDT(df)[is.infinite(`Duration(seconds)`), Autoswing := "✓&quot;]

dplyr使用replace

library(dplyr)
df %>% 
   mutate(Autoswing = replace(Autoswing, is.infinite(`Duration(seconds)`), "✓&quot;))
# A tibble: 20 × 2
   Autoswing `Duration(seconds)`
   <chr>                   <dbl>
 1 ✗                           4
 2 ✗                           5
 3 ✗                           7
 4 ✗                           6
 5 ✗                           8
 6 ✗                           9
 7 ✗                          11
 8 ✗                          16
 9 ✗                          16
10 ✗                           8
11 ✗                          12
12 ✗                          15
13 ✗                          10
14 ✓                         Inf
15 ✗                          14
16 ✓                         Inf
17 ✓                         Inf
18 ✓                         Inf
19 ✓                         Inf
20 ✓                         Inf

数据

df <- structure(list(Autoswing = c("✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, 
"✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, 
"✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;, "✗&quot;), `Duration(seconds)` = c(4, 
5, 7, 6, 8, 9, 11, 16, 16, 8, 12, 15, 10, Inf, 14, Inf, Inf, 
Inf, Inf, Inf)), class = c("tbl_df", "tbl", "data.frame"), 
row.names = c(NA, 
-20L))

推荐阅读