这是示例数据集。让我向所有人展示我对我的数据所做的事情,然后我将解释我正在努力解决的问题。如果标题描述不正确,我深表歉意。我已经尽力了,但是我对此有些陌生。如有需要,可以随时将其更改为更合适的
Location sample1 sample 2 sample 3
chr1:1234 0/1 1/1 0/0
chr2:5678 0/0 0/0 0/0
chr3:2345 1/1 1/1 1/1
chr4:6789 0/1 1/1 ./.
我用它来将它们转换为YES,NO或MAYBE
replacement<-function(x){
x=replace(x,which(x=='./.'),0.1)
x=replace(x,which(x=='0/0'),0)
x=replace(x,which(x=='0/1'),1)
x=replace(x,which(x=='1/1'),2)
}
test=apply(test.data.set,2,replacement)
test.data.2 <- as.data.frame(test)
replacement<-function(x){
x=replace(x,which(x=='0.1'), "MAYBE")
x=replace(x,which(x=='0'), "NO")
x=replace(x,which(x=='1'), "YES")
x=replace(x,which(x=='2'), "YES")
}
test.data.3=apply(test.data.2,2,replacement)
test.data.4 <- as.data.frame(test.data.3)
运行后的数据集
Location sample1 sample 2 sample 3
chr1:1234 YES YES NO
chr2:5678 NO NO NO
chr3:2345 YES YES YES
chr4:6789 YES YES MAYBE
所以我上面写的内容目前对我有用。但是,我有一个新的数据集,其中包含大约300个样本(列),而且大约。我甚至不确定5亿行,因此我需要更改十亿个“单元格”。我尝试在具有256G内存的群集上运行此命令,但它只是超时。我知道我上面写的内容与更改数据的“最流畅”方式相去甚远。有人建议简化此过程吗?我觉得dplyr必须有某种方式来做到这一点。
任何帮助都将是惊人的!如果您需要澄清,请随时提出任何问题。
答案 0 :(得分:1)
library(tidyverse)
重新创建数据:
df <- tibble(
Location = letters[1:4],
sample1 = c("0/1", "0/0", "1/1", "0/1"),
sample2 = c("1/1", "0/0", "1/1", "1/1"),
sample3 = c("0/0", "0/0", "1/1", "./.")
)
代码:
df %>% mutate_at(
vars(- Location),
funs(case_when(
. == "1/1" | . == "0/1" ~ "YES",
. == "0/0" ~ "NO",
. == "./." ~ "MAYBE"
))
)
结果:
# A tibble: 4 x 4
Location sample1 sample2 sample3
<chr> <chr> <chr> <chr>
1 a YES YES NO
2 b NO NO NO
3 c YES YES YES
4 d YES YES MAYBE