Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

r - inserting data inside dataframe within a list dynamically

I have a problem as below.

rept4<-"CGCCCCCTGCTGGCGACTAGGGCAACTGCAGGGTCCTCTTGCTCAAGGTGAGTGGCAGACGCCCACCTGCTGGCAGCCGGGGACACTGCAGGGCCCTCTTGCTTACTGTATAGTGGTGGCA"
rept4<-unlist(strsplit(rept4,""))

Now as per suggestion I have a list of dataframes as below.

i1 <- 5:15
lst <- lapply(i1, function(x)  data.frame(setNames(replicate(x,character(0)), 
                           paste0("C", seq_len(x))), stringsAsFactors = FALSE))
names(lst) <- paste0("len", i1)

My question is how to put data inside every dataframe from within a loop depending upon the length.

  for ( x in 5: 15){

 arr4len<-length(rept4)
 windowsize<-x
 numrows<-ceiling(arr4len/windowsize)
 k<-0
 for (i in 1:numrows){
  for(j in 1:windowsize){
    k<-k+1
    lst$lenx[i,j]<-rept4[k]
  }
 }
}

Here I am trying to access lst$lenx and x-th element like lst$len10 or lst$len11 etc.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If the task is to split rept4 into a list of data.frames, I would do splitting and creating the data.frames in two steps: (1) split the string in substrings of lengths i1, (2) turn the substrings into data.frames

rept4<-"CGCCCCCTGCTGGCGACTAGGGCAACTGCAGGGTCCTCTTGCTCAAGGTGAGTGGCAGACGCCCACCTGCTGGCAGCCGGGGACACTGCAGGGCCCTCTTGCTTACTGTATAGTGGTGGCA"
i1 <- 5:15
end <- cumsum(i1)
start <- end - i1 + 1L
lst <- lapply(stringr::str_sub(rept4, start, end), 
              function(x) 
                setNames(
                  as.data.frame(t(unlist(strsplit(x, ""))), 
                                stringsAsFactors = FALSE),
                paste0("C", seq_len(nchar(x))))
)
names(lst) <- paste0("len", i1)
lst
$len5
  C1 C2 C3 C4 C5
1  C  G  C  C  C

$len6
  C1 C2 C3 C4 C5 C6
1  C  C  T  G  C  T

$len7
  C1 C2 C3 C4 C5 C6 C7
1  G  G  C  G  A  C  T

$len8
  C1 C2 C3 C4 C5 C6 C7 C8
1  A  G  G  G  C  A  A  C

$len9
  C1 C2 C3 C4 C5 C6 C7 C8 C9
1  T  G  C  A  G  G  G  T  C

$len10
  C1 C2 C3 C4 C5 C6 C7 C8 C9 C10
1  C  T  C  T  T  G  C  T  C   A

$len11
  C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11
1  A  G  G  T  G  A  G  T  G   G   C

$len12
  C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12
1  A  G  A  C  G  C  C  C  A   C   C   T

$len13
  C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13
1  G  C  T  G  G  C  A  G  C   C   G   G   G

$len14
  C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14
1  G  A  C  A  C  T  G  C  A   G   G   G   C   C

$len15
  C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15
1  C  T  C  T  T  G  C  T  T   A   C   T   G   T   A

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...