Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
子设置或排列R中的数据_R_Bioinformatics - Fatal编程技术网

子设置或排列R中的数据

子设置或排列R中的数据,r,bioinformatics,R,Bioinformatics,因为我是R的新手,这个问题对你来说可能是小菜一碟。 我有一个txt格式的数据。第一列有集群编号,第二列有不同生物的名称。 例如: 0 org4 | gene759 1 org1 | gene992 2 org1 | gene1101 3组织4 |基因757 4 org1 | gene1702 5 org1 | gene989 6 org1 | gene990 7 org1 | gene1699 9 org1 | gene1102 10组织4 |基因2439 10 org1 | gene1374

因为我是R的新手,这个问题对你来说可能是小菜一碟。 我有一个txt格式的数据。第一列有集群编号,第二列有不同生物的名称。 例如:

  • 0 org4 | gene759
  • 1 org1 | gene992
  • 2 org1 | gene1101
  • 3组织4 |基因757
  • 4 org1 | gene1702
  • 5 org1 | gene989
  • 6 org1 | gene990
  • 7 org1 | gene1699
  • 9 org1 | gene1102
  • 10组织4 |基因2439
  • 10 org1 | gene1374
  • 我需要按照以下格式重新排列/重塑数据

    集群编号组织1组织2组织3组织4
  • 01
  • 100 00
  • 我想不出在R里怎么做。
    谢谢

    将表格读入R可以用


    input我们可以使用
    table

    out <- cbind(ClusterNo = seq_len(nrow(df1)), as.data.frame.matrix(table(seq_len(nrow(df1)), 
           factor(sub("\\|.*", "", df1[[2]]), levels = paste0("org", 1:4)))))
    
    head(out, 2)
    #    ClusterNo org1 org2 org3 org4
    #1         1    0    0    0    1
    #2         2    1    0    0    0
    

    out@akrun…..在有多个条目的情况下,每个集群是否只能获得一个值?对于Org2,我在集群中得到3。有可能在单个集群中将我的值限制为1吗?@Furqan你可以执行
    out[,-1]是的,它起作用了。是否可以在原始命令中修复此表达式?因为我想将此命令存储为函数。您可以使用
    +(table(seq_len(..)!=0)
    ,然后使用
    包装为.data.frame.matrix
    > input
       V1            V2 V3
    1   0  org4|gene759  4
    2   1  org1|gene992  1
    3   2 org1|gene1101  1
    4   3  org4|gene757  4
    5   4 org1|gene1702  1
    6   5  org1|gene989  1
    7   6  org1|gene990  1
    8   7 org1|gene1699  1
    9   9 org1|gene1102  1
    10 10 org4|gene2439  4
    11 10 org1|gene1374  1
    
    possibleOrgs <- seq_len(max(input[, 3])) # = c(1, 2, 3, 4)
    
    result <- vapply(unique(input[, 1]), function (x) 
      possibleOrgs %in% input[input[, 1] == x, 3], logical(4)))
    
    result <- t(result) * 1
    colnames (result) <- paste0('org', possibleOrgs)
    rownames(result) <- unique(input[, 1])
    
    > result
    
       org1 org2 org3 org4
    0     0    0    0    1
    1     1    0    0    0
    2     1    0    0    0
    3     0    0    0    1
    4     1    0    0    0
    5     1    0    0    0
    6     1    0    0    0
    7     1    0    0    0
    9     1    0    0    0
    10    1    0    0    1
    
    out <- cbind(ClusterNo = seq_len(nrow(df1)), as.data.frame.matrix(table(seq_len(nrow(df1)), 
           factor(sub("\\|.*", "", df1[[2]]), levels = paste0("org", 1:4)))))
    
    head(out, 2)
    #    ClusterNo org1 org2 org3 org4
    #1         1    0    0    0    1
    #2         2    1    0    0    0
    
    out1 <- as.data.frame.matrix(table(df1[[1]], 
        factor(sub("\\|.*", "", df1[[2]]), levels = paste0("org", 1:4))))