2015-08-15 125 views
0

我計劃簡化這個問題,但我決定只發布我的原始代碼以便清晰。我試圖創建一個列表score_loc,其中包含1874個列表元素,並從三組不同的數據中提取數據:loc_pre,loc_locatecsv_t。爲了創建這個列表,我沒有有效地使用for循環來爲每個列表元素分配數據幀,這是非常緩慢的,因爲數據非常大,給我錯誤。R從其他列表/數據框中創建新列表

重複性的數據

Shortened csv_t.csv包含第一20000行

csv_t.csv dropbox link

loc_pre.csv dropbox link

至於loc_locate,它有點難以顯示dataframes列表重複的例子。

一些先前確定的數據:

head(loc_pre) # 1874 rows 
# start end 
# 1 4844 4852 
# 2 5954 5962 
# 3 7896 7904 
# 4 12301 12309 
# 5 18553 18561 
# 6 18670 18678 

loc_locate # a list of varying lengths of dataframes; 1874 list elements 
# [[1]] 
#  start end 
# [1,]  6 6 
# 
# [[2]] 
#  start end 
# [1,]  1 1 
# [2,]  6 6 
# [3,]  9 9 
# 
# [[3]] 
#  start end 
# [1,]  6 6 
# [2,]  8 8 

head(csv_t) # 4524203 rows, tpl column values are consecutively increasing by 1 
#  tpl score 
# 1: 3239  6 
# 2: 3240  6 
# 3: 3241  7 
# 4: 3242 13 
# 5: 3243  0 
# 6: 3244  6 

所需的輸出:

你可以看到,loc_pre行號與loc_locate列表元素數量相對應。 loc_locate表示loc_pre相對於相應起始位置的位置編號。例如,如果您採用loc_locate的第一個元素和loc_pre的第一行,則可以看出您正在尋找4844,4845,4846,4847,4488,4889,4850,4851,4852中的第6個位置。在這種情況下,位置是4849.

按照這條邏輯,我想創建一個新的列表score_loc 1874個列表元素,它將顯示loc_pre的每個單獨行的那些所需位置的開始,結束和得分。評分欄將來自csv_t。

score_loc 
# [[1]] 
#  start end score 
# [1,]  6 6 10 # score corresponding to position (4844 + 6 - 1) 
# 
# [[2]] 
#  start end score 
# [1,]  1 1  1 # score corresponding to position (5954 + 1 - 1) 
# [2,]  6 6  2 # score corresponding to position (5954 + 6 - 1) 
# [3,]  9 9  8 # score corresponding to position (5954 + 9 - 1) 
# 
# [[3]] 
#  start end score 
# [1,]  6 6 19 # score corresponding to position (7896 + 6 - 1) 
# [2,]  8 8 11 # score corresponding to position (7896 + 8 - 1) 

我的代碼

正如我前面提到的,我用一個for循環,試圖做到這一點,但這種方法正在太長。我希望通過查看我的代碼,可以更清楚地瞭解我正在嘗試完成的任務。

score_loc <- list() 
for(w in 1:nrow(loc_pre)){ 
    vectornom <- loc_pre[w, 1] + loc_locate[[w]][,"start"] - 1 
    score_loc[[w]] <- data.frame(csv_t[csv_t$tpl %in% vectornom,][, 4, with=F]) # takes a long time for some reason 
} 
+0

關於'locate'中的'Score'值,它來自'csv_t'。另外,請考慮使用'dput'顯示示例,因爲它會更容易複製/粘貼 – akrun

+0

是的,我會在後面更加清晰 – ALKI

+0

我爲'loc_pre'和'csv_t'添加了一些dropbox鏈接 – ALKI

回答

1

一種方式做到這一點是使用mapply功能:

# Expand the sequences 
preList <- apply(loc_pre, 1, function(X) X[1]:X[2]) 

# Function to build tpl datasets 
posFun <- function(Seq, Loc) { 
    cbind(Loc, tpl = apply(Loc, 1, function(X, S) S[X[1]:X[2]], S = Seq)) 
} 

# Apply, combine and merge  
mOutput <- mapply(posFun, preList, loc_locate) 
mIndex <- rep(1:length(mOutput), sapply(mOutput, nrow)) # Not sure if you need this, but have included for now 
combineData <- data.frame(Index = mIndex, do.call("rbind", mOutput)) 
merge(combineData, csv_t, all.x = TRUE) 

望着數據樣本,我們似乎可以簡化這:

posFun <- function(Seq, Loc) cbind(Loc, tpl = Seq + Loc[,1] - 1) 
mOutput <- mapply(posFun, loc_pre$start, loc_locate) 
merge(do.call("rbind", mOutput), csv_t, all.x = TRUE) 
# tpl start end score 
# 1 4849  6 6  6 
# 2 5954  1 1  4 
# 3 5959  6 6  7 
# 4 5962  9 9  6 
# 5 7901  6 6  2 
# 6 7903  8 8  1 

注:我在這個例子中,我隨機生成了我的分數

相關問題