英文:
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("tbl_df", "tbl", "data.frame"), 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 = "omit"))
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
<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 has any clue for doing this via dplyr or another iterative method (for loop and so on)?
Thanks
答案1
得分: 1
你可以使用rowSums
和filter
:
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 %>%
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
答案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 %>%
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论