0

I'm trying to convert these lists like Python's list. I've used these codes

library(GenomicRanges)
library(data.table)
library(Repitools)    
pcs_by_tile<-lapply(as.list(1:length(tiled_chr)) , function(x){
                obj<-tileSplit[[as.character(x)]]
                if(is.null(obj)){
                  return(0)
                } else {
                  runs<-filtered_identical_seqs.gr[obj] 
                  df <- annoGR2DF(runs)
                  score = split(df[,c("start","end")], 1:nrow(df[,c("start","end")]))
                  #print(score)
                  return(score)
                }
                })
dt_text <- unlist(lapply(tiled_chr$score, paste, collapse=","))    
writeLines(tiled_chr, paste0("x.txt"))

The following line of code iterates through each row of the DataFrame (only 2 columns) and splits them into the list. However, its output is different from what I desired.

score = split(df[,c("start","end")], 1:nrow(df[,c("start","end")]))

enter image description here

But I wanted the following kinda output:

[20350, 20355], [20357, 20359], [20361, 20362], ........
1
  • Can you explain more about the structure of score? I'm a little confused Commented Feb 4, 2022 at 2:47

1 Answer 1

1

If I understand your question correctly, using as.tuple from the package 'sets' might help. Here's what the code might look like

library(sets)

score = split(df[,c("start","end")], 1:nrow(df[,c("start","end")]))

....

df_text = unlist(lapply(score, as.tuple),recursive = F)

This will return a list of tuples (and zeroes) that look more like what you are looking for. You can filter out the zeroes by checking the type of each element in the resulting list and removing the ones that match the type. For example, you could do something like this

df_text_trimmed <- df_text[!lapply(df_text, is.double)]

to get rid of all your zeroes

Edit: Now that I think about it, you probably don't even need to convert your dataframes to tuples if you don't want to. You just need to make sure to include the 'recursive = F' option when you unlist things to get a list of 0s and dataframes containing the numbers you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.