gpt4 book ai didi

list - 要在R中列出的文本文件

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

我有一个很大的文本文件,每行中都有可变数量的字段。每行中的第一个条目对应于一个生物途径,每个随后的条目对应于该途径中的一个基因。前几行可能看起来像这样

path1   gene1 gene2
path2 gene3 gene4 gene5 gene6
path3 gene7 gene8 gene9

我需要将此文件读入R作为列表,每个元素是一个字符 vector ,列表中每个元素的名称是该行的第一个元素,例如:
> pathways <- list(
+ path1=c("gene1","gene2"),
+ path2=c("gene3","gene4","gene5","gene6"),
+ path3=c("gene7","gene8","gene9")
+ )
>
> str(pathways)
List of 3
$ path1: chr [1:2] "gene1" "gene2"
$ path2: chr [1:4] "gene3" "gene4" "gene5" "gene6"
$ path3: chr [1:3] "gene7" "gene8" "gene9"
>
> str(pathways$path1)
chr [1:2] "gene1" "gene2"
>
> print(pathways)
$path1
[1] "gene1" "gene2"

$path2
[1] "gene3" "gene4" "gene5" "gene6"

$path3
[1] "gene7" "gene8" "gene9"

...但是我需要自动执行数千行。我看到了 similar question posted here previously,但是我不知道如何从该线程执行此操作。

提前致谢。

最佳答案

这是一种实现方法:

# Read in the data
x <- scan("data.txt", what="", sep="\n")
# Separate elements by one or more whitepace
y <- strsplit(x, "[[:space:]]+")
# Extract the first vector element and set it as the list element name
names(y) <- sapply(y, `[[`, 1)
#names(y) <- sapply(y, function(x) x[[1]]) # same as above
# Remove the first vector element from each list element
y <- lapply(y, `[`, -1)
#y <- lapply(y, function(x) x[-1]) # same as above

关于list - 要在R中列出的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6602881/

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