如何在列范围下联合更改值,并在其他列中分别更改。

huangapple go评论66阅读模式
英文:

How to change jointly values under columns range and serately in other column

问题

以下是您提供的文本的翻译部分:

I would like to remove rows equal to zero in columns that ranges from the second to the sixth columns and that in the last column have 0.4. This would imply - if possible - to readjust the ID value properly.

我想删除那些在第二列到第六列之间的列中等于零,并且在最后一列中等于0.4的行。如果可能的话,这将意味着需要适当地调整ID的值。

My expected outcome would be:

我的期望结果如下:

    ID   Vid   Mus   Rea   Ema   SMS   tel   MMT
      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    1     1     0     1     1     1     1     0   1  
    2     3     1     1     1     0     1     0   1  
    3     4     1     1     1     1     1     1   0.4
    4     6     1     1     1     0     1     0   0.4
    5     7     0     0     1     0     0     0   0.4

这是我期望的结果。

that does not have the second ID rows from the original dataset. Does anyone have any clue for doing this via dplyr or another iterative method (for loop and so on)?

这个结果不包括原始数据集中的第二个ID行。有没有人有关于如何通过dplyr或其他迭代方法(如for循环等)来完成这个任务的线索?

Thanks

谢谢

英文:

I have this dataset

structure(list(ID = c(1, 2, 3, 4, 6, 7), V = c(0, 0, 1, 1, 
1, 0), Mus = c(1, 0, 1, 1, 1, 0), R = c(1, 0, 1, 1, 1, 1), 
    E = c(1, 0, 0, 1, 0, 0), S = c(1, 0, 1, 1, 1, 0), t = c(0, 
    0, 0, 1, 0, 0), score = c(1, 0.4, 1, 0.4, 0.4, 0.4)), row.names = c(NA, 
-6L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), na.action = structure(c(`5` = 5L, 
`12` = 12L, `15` = 15L, `21` = 21L, `22` = 22L, `23` = 23L, `34` = 34L, 
`44` = 44L, `46` = 46L, `52` = 52L, `56` = 56L, `57` = 57L, `58` = 58L
), class = &quot;omit&quot;))

I would like to remove rows equal to zero in columns that ranges from the second to the sixth columns and that in last column have 0.4. This would imply - if possible - to readjust the ID value properly.

My expected outcome would be:

ID   Vid   Mus   Rea   Ema   SMS   tel   MMT
  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     1     0     1     1     1     1     0   1  
2     3     1     1     1     0     1     0   1  
3     4     1     1     1     1     1     1   0.4
4     6     1     1     1     0     1     0   0.4
5     7     0     0     1     0     0     0   0.4

that does not have the second ID rows from the original dataset. Does anyone has any clue for doing this via dplyr or another iterative method (for loop and so on)?

Thanks

答案1

得分: 1

你可以使用rowSumsfilter

library(dplyr)
df %>%
  filter(!(rowSums(across(V:t)) == 0 & score == 0.4)) %>%
  mutate(ID = row_number())

     ID     V   Mus     R     E     S     t score
  <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     0     1     1     1     1     0   1  
2     2     1     1     1     0     1     0   1  
3     3     1     1     1     1     1     1   0.4
4     4     1     1     1     0     1     0   0.4
5     5     0     0     1     0     0     0   0.4
英文:

You can use rowSums with filter:

library(dplyr)
df %&gt;% 
  filter(!(rowSums(across(V:t)) == 0 &amp; score == 0.4)) %&gt;% 
  mutate(ID = row_number())

     ID     V   Mus     R     E     S     t score
  &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1     1     0     1     1     1     1     0   1  
2     2     1     1     1     0     1     0   1  
3     3     1     1     1     1     1     1   0.4
4     4     1     1     1     0     1     0   0.4
5     5     0     0     1     0     0     0   0.4

答案2

得分: 1

你可以在 filter() 中使用 if_all(),就像你可以在其他函数中使用 across() 一样。只需使用 row_number() 重新生成 ID

library(dplyr)

df %>%
  filter(
    !(if_all(
      .cols = V:t,
      .fns = ~ .x == 0
    ) & score == 0.4)
  ) %>%
  mutate(
    ID = row_number()
  )
#> # A tibble: 5 × 8
#>      ID     V   Mus     R     E     S     t score
#>   <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1     1     0     1     1     1     1     0   1  
#> 2     2     1     1     1     0     1     0   1  
#> 3     3     1     1     1     1     1     1   0.4
#> 4     4     1     1     1     0     1     0   0.4
#> 5     5     0     0     1     0     0     0   0.4
英文:

You can use if_all() in filter() like you can use across() in other functions. Just use row_number() to regenerate ID.

library(dplyr)

df %&gt;%
  filter(
    !(if_all(
      .cols = V:t,
      .fns = ~ .x == 0
    ) &amp; score == 0.4)
  ) %&gt;%
  mutate(
    ID = row_number()
  )
#&gt; # A tibble: 5 &#215; 8
#&gt;      ID     V   Mus     R     E     S     t score
#&gt;   &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1     1     0     1     1     1     1     0   1  
#&gt; 2     2     1     1     1     0     1     0   1  
#&gt; 3     3     1     1     1     1     1     1   0.4
#&gt; 4     4     1     1     1     0     1     0   0.4
#&gt; 5     5     0     0     1     0     0     0   0.4

huangapple
  • 本文由 发表于 2023年2月10日 16:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定