gpt4 book ai didi

r - 将字符串子集化为不均匀列表并从列表中逻辑传播 data.frame

转载 作者:行者123 更新时间:2023-12-01 13:25:53 25 4
gpt4 key购买 nike

我花了很多时间试图解决这个问题,但没有成功。

我有一个 data.frame,其中有一列包含可变长度的字符串。数据框看起来像这样:

Taxa <- as.character(c("cellularorganisms(norank)_Bacteria(superkingdom)_Actinobacteria(phylum)","cellularorganisms(norank)_Bacteria(superkingdom)_Proteobacteria(phylum)_Gammaproteobacteria(class)_Enterobacteriales(order)_Enterobacteriaceae(family)","cellularorganisms(norank)_Bacteria(superkingdom)_Proteobacteria(phylum)_Gammaproteobacteria(class)_Enterobacteriales(order)","cellularorganisms(norank)_Bacteria(superkingdom)_Proteobacteria(phylum)_Gammaproteobacteria(class)_Enterobacteriales(order)_Enterobacteriaceae(family)_Klebsiella(genus)","cellularorganisms(norank)_Bacteria(superkingdom)_Proteobacteria(phylum)_Gammaproteobacteria(class)_Enterobacteriales(order)_Enterobacteriaceae(family)_Klebsiella(genus)_Klebsiellapneumoniae(species)","cellularorganisms(norank)_Bacteria(superkingdom)_Firmicutes(phylum)_Clostridia(class)_Clostridiales(order)","cellularorganisms(norank)_Bacteria(superkingdom)_Firmicutes(phylum)_Clostridia(class)","cellularorganisms(norank)_Bacteria(superkingdom)_Firmicutes(phylum)_Clostridia(class)_Clostridiales(order)_Clostridiaceae(family)","cellularorganisms(norank)_Bacteria(superkingdom)_Firmicutes(phylum)_Clostridia(class)_Clostridiales(order)_Clostridiaceae(family)_Clostridium(genus)","cellularorganisms(norank)_Bacteria(superkingdom)_Firmicutes(phylum)_Clostridia(class)_Clostridiales(order)_Clostridiaceae(family)_Clostridium(genus)_Clostridiumbotulinum(species)","cellularorganisms(norank)_Bacteria(superkingdom)_Firmicutes(phylum)_Clostridia(class)_Clostridiales(order)_Clostridiaceae(family)_Clostridium(genus)_Clostridiumbotulinum(species)_ClostridiumbotulinumCDC66177(strain)","cellularorganisms(norank)_Bacteria(superkingdom)_Actinobacteria(phylum)_Actinobacteria(class)_Actinobacteridae(subclass)_Actinomycetales(order)_Micrococcineae(suborder)","cellularorganisms(norank)_Bacteria(superkingdom)_Actinobacteria(phylum)_Actinobacteria(class)_Actinobacteridae(subclass)_Actinomycetales(order)_Micrococcineae(suborder)_Microbacteriaceae(family)","cellularorganisms(norank)_Bacteria(superkingdom)_Actinobacteria(phylum)_Actinobacteria(class)_Actinobacteridae(subclass)_Actinomycetales(order)_Micrococcineae(suborder)_Microbacteriaceae(family)_Microbacterium(genus)","cellularorganisms(norank)_Bacteria(superkingdom)_Actinobacteria(phylum)_Actinobacteria(class)_Actinobacteridae(subclass)_Actinomycetales(order)_Micrococcineae(suborder)_Microbacteriaceae(family)_Microbacterium(genus)_Microbacteriumlaevaniformans(species)_MicrobacteriumlaevaniformansOR221(strain)"))
Percent <- c("0.000400","0.006800","0.005034","0.001760","0.000000","0.000000","0.344400","0.000000","0.000000","0.000000","0.006500","0.002819","0.000487","0.000000","0.001090")
Test <- data.frame(Percent, Taxa)
Test$Taxa <- as.character(Test$Taxa)

我可以将下划线上的这些字符串子集化为一个不等长的列表:

NewDF <- strsplit(Test$Taxa, "_", fixed=TRUE)

但我无法弄清楚如何获取此解析后的输出并将其格式化为可用的结构。

每个解析部分都有两个组成部分,一个描述符和一个分类级别(即 Bacteria(superkingdom) 是描述符 Bacteria 和分类级别的 super 王国。

我想要做的是获取这个解析后的输出,并填充一个具有以下列标题(norank、superkingdom、phylum、class、order、family、genus、species、strain)的 data.frame。输出需要跳过上面列表中未包含的分类级别(例如,在类和订单之间有分类级别为子类的行,我需要删除子类)。

此外,如果一行在特定分类级别停止并且仍有未填充的列,则应将它们设置为 NA(即第一行以门结束,因此类、目、科等应为 NA)。

最终输出应该是这样的:

                      norank           superkingdom                 phylum                      class                    order                     family                 genus                               species                                    strain
1 cellularorganisms(norank) Bacteria(superkingdom) Actinobacteria(phylum) <NA> <NA> <NA> <NA> <NA> <NA>
2 cellularorganisms(norank) Bacteria(superkingdom) Proteobacteria(phylum) Gammaproteobacteria(class) Enterobacteriales(order) Enterobacteriaceae(family) <NA> <NA> <NA>
3 cellularorganisms(norank) Bacteria(superkingdom) Proteobacteria(phylum) Gammaproteobacteria(class) Enterobacteriales(order) <NA> <NA> <NA> <NA>
4 cellularorganisms(norank) Bacteria(superkingdom) Proteobacteria(phylum) Gammaproteobacteria(class) Enterobacteriales(order) Enterobacteriaceae(family) Klebsiella(genus) <NA>
5 cellularorganisms(norank) Bacteria(superkingdom) Proteobacteria(phylum) Gammaproteobacteria(class) Enterobacteriales(order) Enterobacteriaceae(family) Klebsiella(genus) Klebsiellapneumoniae(species) <NA>
6 cellularorganisms(norank) Bacteria(superkingdom) Firmicutes(phylum) Clostridia(class) Clostridiales(order) <NA> <NA> <NA> <NA>
7 cellularorganisms(norank) Bacteria(superkingdom) Firmicutes(phylum) Clostridia(class) <NA> <NA> <NA> <NA> <NA>
8 cellularorganisms(norank) Bacteria(superkingdom) Firmicutes(phylum) Clostridia(class) Clostridiales(order) Clostridiaceae(family) <NA> <NA> <NA>
9 cellularorganisms(norank) Bacteria(superkingdom) Firmicutes(phylum) Clostridia(class) Clostridiales(order) Clostridiaceae(family) Clostridium(genus) <NA> <NA>
10 cellularorganisms(norank) Bacteria(superkingdom) Firmicutes(phylum) Clostridia(class) Clostridiales(order) Clostridiaceae(family) Clostridium(genus) Clostridiumbotulinum(species) <NA>
11 cellularorganisms(norank) Bacteria(superkingdom) Firmicutes(phylum) Clostridia(class) Clostridiales(order) Clostridiaceae(family) Clostridium(genus) Clostridiumbotulinum(species) ClostridiumbotulinumCDC66177(strain)
12 cellularorganisms(norank) Bacteria(superkingdom) Actinobacteria(phylum) Actinobacteria(class) Actinomycetales(order) <NA> <NA> <NA> <NA>
13 cellularorganisms(norank) Bacteria(superkingdom) Actinobacteria(phylum) Actinobacteria(class) Actinomycetales(order) Microbacteriaceae(family) <NA> <NA> <NA>
14 cellularorganisms(norank) Bacteria(superkingdom) Actinobacteria(phylum) Actinobacteria(class) Actinomycetales(order) Microbacteriaceae(family) Microbacterium(genus) <NA> <NA>
15 cellularorganisms(norank) Bacteria(superkingdom) Actinobacteria(phylum) Actinobacteria(class) Actinomycetales(order) Microbacteriaceae(family) Microbacterium(genus) Microbacteriumlaevaniformans(species) MicrobacteriumlaevaniformansOR221(strain)

最佳答案

您可以尝试将一系列小数据帧编译成一个 df

library(dplyr)

NewDF <-
lapply(strsplit(Test$Taxa, "_", fixed=TRUE),
function(x)
{
vars <- lapply(x, function(y)
{
m <- regexec("\\((.+?)\\)",y)
regmatches(y,m)[[1]][2]
})
vals <- as.list( x )
names(vals) <- unlist(vars)
data.frame( vals,
stringsAsFactors = FALSE )
}) %>% rbind_all

这给了我你想要的结果(也有好看的变量名)

关于r - 将字符串子集化为不均匀列表并从列表中逻辑传播 data.frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31016748/

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