gpt4 book ai didi

以不同方式替换值

转载 作者:行者123 更新时间:2023-12-05 09:33:05 24 4
gpt4 key购买 nike

我有一个数据框如下:

| genome |time_1|time_2|time_3|time_4|
|genome_1| 28 |28 |NA |NA |
|genome_2| 2 |4 |5 |2 |
|genome_3| 12 |12 |12 |12 |
|genome_4| 2 |NA |NA |NA |
|genome_5| 3 |3 |NA |NA |

我想将每一行中相同的值(即至少出现两次)替换为 1,将出现一次的值替换为 0。我期望的表格应该是这样的:

| genome |time_1|time_2|time_3|time_4|
|genome_1| 1 |1 |NA |NA |
|genome_2| 1 |0 |0 |1 |
|genome_3| 1 |1 |1 |1 |
|genome_4| 0 |NA |NA |NA |
|genome_5| 1 |1 |NA |NA |

有什么想法吗?非常感谢

最佳答案

在 Base R 中你会做:

id <-t(apply(df[-1], 1, function(x)duplicated(x, NA)|duplicated(x, NA, TRUE)))
cbind(df[1], NA^is.na(df[-1]) * id)


genome time_1 time_2 time_3 time_4
1 genome_1 1 1 NA NA
2 genome_2 1 0 0 1
3 genome_3 1 1 1 1
4 genome_4 0 NA NA NA
5 genome_5 1 1 NA NA

你也可以这样做:

 df %>%
pivot_longer(starts_with("time"))%>%
mutate(value = (duplicated(value, NA)|duplicated(value, NA, TRUE))* NA^is.na(value))%>%
pivot_wider()

# A tibble: 5 x 5
genome time_1 time_2 time_3 time_4
<chr> <int> <int> <int> <int>
1 genome_1 1 1 NA NA
2 genome_2 1 0 0 1
3 genome_3 1 1 1 1
4 genome_4 1 NA NA NA
5 genome_5 1 1 NA NA

关于以不同方式替换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67491450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com