2 views

I am new to data science and I am taking a long time to learn the concepts. Now I am working on a list that consists of 6600 elements, It also contains a vector of characters. The list has a name corresponding to a particular gene

The list looks like this:

GenesSplitted_________list[6600]_______List of length 6600

_____YBOR24W__________list[330]________Liost of length 330

Each element in the list looks like this:

GeneSplitted[[1]]

[[1]]

[1] "ATG"

[[2]]

[1] "TTG"

[[3]]

[1] "AAT"

[[4]]

[1] "AGT"

[[5]]

[1] "TCA

I am trying to apply a loop so that I can transform the list into a data frame. I try extracting each element from the list and apply the code to convert. The code is as follows:

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

I am getting the output as shown which I wanted:

1   ATG YBR024W

2   TTG YBR024W

3   AAT YBR024W

4   AGT YBR024W

5   TCA YBR024W

6   AGA YBR024W

7   AAA YBR024W

8   TAT YBR024W

After changing to the data frame I tried to loop the list again and created an empty list with 6600 which is pretending to fill the loop.

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

}

But I am not able to get the desired output. please, someone, help me solve it?

by (36.8k points)

I have not used the nested loop because I don't have reproducible data. But this code works for your problem.

# 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

}

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch