gpt4 book ai didi

r - 如果行中有特定值,则创建附加行

转载 作者:行者123 更新时间:2023-12-03 20:35:47 25 4
gpt4 key购买 nike

我拥有的矩阵:

> dput(head(data1))
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0), .Dim = c(6L, 18L), .Dimnames = list(c("AT1G01050",
"AT1G01080", "AT1G01090", "AT1G01320", "AT1G01470", "AT1G01800"
), c("10", "33.95", "58.66", "84.42", "110.21", "134.16", "164.69",
"199.1", "234.35", "257.19", "361.84", "432.74", "506.34", "581.46",
"651.71", "732.59", "817.56", "896.24")))

为了便于解释,我将显示下表:

          10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46 651.71 732.59 817.56 896.24
AT1G01050 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0
AT1G01080 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1
AT1G01090 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0
AT1G01320 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1
AT1G01470 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
AT1G01800 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0

如您所见,我的表格充满了 01。在许多情况下,每一行中有多个 1。所以我想做的是将行复制/复制三份,以便在每一行中留下一个 1 。因此,例如取第一行:

          10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46 651.71 732.59 817.56 896.24
AT1G01050 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0

这一行有两个数字1。这意味着我必须为这个“基因”再创建一行。所以输出应该是这样的。我在数据中保留了第一个 1(从左边开始),但删除了第二个。为了稍后区分,我们还必须更改行的名称:

                10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46 651.71 732.59 817.56 896.24
AT1G01050_1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

现在我删除第一个 1 并保留第二个。

                10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46 651.71 732.59 817.56 896.24
AT1G01050_2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

我相信这并不容易,但我相信你们中的某个人可能知道如何解决该问题。

最佳答案

试试这个:

divideLine = function(u)
{
index = which(u %in% 1)
len = length(u)

if(sum(u)<=1) return(t(u))

t(mapply(function(x,y){vec=rep(0, len);vec[y]=1;vec}, 1:length(index), index))
}

lst = apply(mat,1,divideLine)
x = lapply(lst, nrow)

res = do.call(rbind, lst)
rownames(res)=unlist(Map(function(u,v) paste(v, as.character((1:u)-1), sep='_'),x, names(x)))

#> res
# 10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46 651.71 732.59 817.56 896.24
#AT1G01050_0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
#AT1G01050_1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
#AT1G01080_0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
#AT1G01080_1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
#AT1G01080_2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
#AT1G01080_3 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
#AT1G01080_4 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
#AT1G01080_5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
#AT1G01080_6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
#AT1G01090_0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
#AT1G01090_1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
#AT1G01090_2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
#AT1G01320_0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
#AT1G01320_1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
#AT1G01320_2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
#AT1G01320_3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
#AT1G01470_0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#AT1G01800_0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#AT1G01800_1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0

关于r - 如果行中有特定值,则创建附加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29299618/

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