我是数据科学领域的新手,我正在尝试做一些事情,这花了我很长时间才能实现。我有一个6600个列表元素的列表,其中包含一个由不同长度的字符(密码子)组成的向量。每个列表都有一个名称,该名称对应于特定的基因。
列表看起来像这样:
GenesSplitted_________list[6600]_______List of length 6600
_____YBOR24W__________list[330]________Liost of length 330
依此类推。
列表中的每个元素看起来都像这样
GeneSplitted[[1]]
[[1]]
[1] "ATG"
[[2]]
[1] "TTG"
[[3]]
[1] "AAT"
[[4]]
[1] "AGT"
[[5]]
[1] "TCA
依此类推。
任务是应用循环离子,我可以在其中将列表的每个元素转换为数据帧。当我提取列表中的一个元素并应用此代码时,它似乎可以工作:
a <- as.matrix(GenesSplitted[[1]]) #first element of the list into matrix
a <- as.data.frame(Gene1) #transformed into dataframe
a$transcript <- df[1,1] # assign a new column named
# transcript with the name
# indicated in another data frame
# with the gene name
当我执行此代码时,我得到了我所期望的,基本上看起来像这样:
1 ATG YBR024W
2 TTG YBR024W
3 AAT YBR024W
4 AGT YBR024W
5 TCA YBR024W
6 AGA YBR024W
7 AAA YBR024W
8 TAT YBR024W
依此类推。
然后我尝试遍历列表。我创建了一个空列表,其中包含6600个要在循环时填充的元素。
GenesSplittedFrames <- vector(mode = "list", length = 6600) #Empty list for the frames
z<- 1
for (frame in GenesSplitted[[]]){
a <- as.matrix(GenesSplitted[[frame]])
b <- as.data.frame(a)
b$transcript <- df[z,1]
print(b)
z <- z+1
}
尽管如此,我仍无法获得期望的结果,并且之前我在R中一直在进行这样的循环。
感谢一百万。
未经测试,因为没有可复制形式的数据,但据我所知,它可能起作用。
# Create an empty list for the frames
GenesSplittedFrames <- vector(mode = "list", length = 6600)
for (frame_inx in seq_along(GenesSplitted)){
a <- as.matrix(GenesSplitted[[frame_inx]])
b <- as.data.frame(a)
b$transcript <- df[frame_inx, 1]
print(b)
GenesSplittedFrames[[frame_inx]] <- b
}