我有一個包含 90 個名稱的串列,我想使用回圈將它們劃分并包含到物件中。我已經根據模式選擇了串列的名稱,但我不確定如何回圈創建物件名稱。我之前嘗試過使用 assign() 函式,但它創建了值(在反引號內)而不是物件。謝謝!!!
所以串列有 90 個名稱,每個樣本名稱重復 5 次,所以基本上我總共有 18 個樣本,每個樣本有 5 個檔案。我想為每個樣本創建一個物件,其中包含與該樣本對應的名稱串列,因此是一個包含 5 個專案的串列。所以我想創建一個回圈而不是復制粘貼函式 (sample.1 = sample.names.dilutions[grep("Sample 1_", sample.names.dilutions)] ) 18 次。我希望這是有道理的?
#list
>sample.names.dilutions
> length(sample.names.dilutions)
[1] 90
#names in list
> sample.names.dilutions[1:20]
[1] "New AS Plate 21_AS Plate_Sample 1_100.fcs" "New AS Plate 21_AS Plate_Sample 1_25.fcs"
[3] "New AS Plate 21_AS Plate_Sample 1_250.fcs" "New AS Plate 21_AS Plate_Sample 1_50.fcs"
[5] "New AS Plate 21_AS Plate_Sample 1_500.fcs" "New AS Plate 21_AS Plate_Sample 10_100.fcs"
[7] "New AS Plate 21_AS Plate_Sample 10_25.fcs" "New AS Plate 21_AS Plate_Sample 10_250.fcs"
[9] "New AS Plate 21_AS Plate_Sample 10_50.fcs" "New AS Plate 21_AS Plate_Sample 10_500.fcs"
[11] "New AS Plate 21_AS Plate_Sample 11_100.fcs" "New AS Plate 21_AS Plate_Sample 11_25.fcs"
[13] "New AS Plate 21_AS Plate_Sample 11_250.fcs" "New AS Plate 21_AS Plate_Sample 11_50.fcs"
[15] "New AS Plate 21_AS Plate_Sample 11_500.fcs" "New AS Plate 21_AS Plate_Sample 12_100.fcs"
[17] "New AS Plate 21_AS Plate_Sample 12_25.fcs" "New AS Plate 21_AS Plate_Sample 12_250.fcs"
[19] "New AS Plate 21_AS Plate_Sample 12_50.fcs" "New AS Plate 21_AS Plate_Sample 12_500.fcs"
#function i want to create with loop
> sample.1 = sample.names.dilutions[grep("Sample 1_", sample.names.dilutions)]
> length(sample.1)
[1] 5
> sample.1
[1] "New AS Plate 21_AS Plate_Sample 1_100.fcs" "New AS Plate 21_AS Plate_Sample 1_25.fcs"
[3] "New AS Plate 21_AS Plate_Sample 1_250.fcs" "New AS Plate 21_AS Plate_Sample 1_50.fcs"
[5] "New AS Plate 21_AS Plate_Sample 1_500.fcs"
> #i have 18 different samples and want to assign value and subset according to sample name
> for(i in 1:18) {
print(sample.names[i], quote=FALSE) = sample.names.dilutions[grep(paste0("Sample ",i,"_"), sample.names.dilutions)]}
Error in print(sample.names[i], FALSE) <- sample.names.dilutions[grep(paste0("Sample ", :
could not find function "print<-"
uj5u.com熱心網友回復:
我想我現在明白了;感謝您在評論中澄清您的問題。如果我遺漏了什么或您有任何疑問,請告訴我。
術語,快速
我相信您有興趣根據每個元素中的模式將字串向量拆分為多個較短的字串向量。串列只是向量的向量。
g 是一個包含 20 個字串元素的向量(請參閱下面的資料代碼塊)。
is.vector(g)
#> [1] TRUE
這是一個僅包含一個向量的串列。
str(list(g))
#> List of 1
#> $ : chr [1:20] "New AS Plate 21_AS Plate_Sample 12_50.fcs" "New AS Plate 21_AS Plate_Sample 1_100.fcs" "New AS Plate 21_AS Plate_Sample 1_25.fcs" "New AS Plate 21_AS Plate_Sample 1_250.fcs" ...
現在進入問題...
在您的問題中,您特別詢問使用assign(). 雖然使用起來assign()很方便,但[通常不推薦][1]。但有時你必須做你必須做的事情,這并不可恥。這是您可以手動使用它的方法,一次一組(就像您在問題中顯示的那樣)。
# Using assign() one group at a time
h <- g[grep("Sample 1_", g)]
assign(x = "sample_1_group", value = h)
assign()在 for 回圈中使用非常簡單(并且看似合乎邏輯)。
定義 for 回圈的第一步是定義回圈將“回圈”的內容。或者換句話說,在回圈的每次迭代中會發生什么變化。在您的情況下,我們正在尋找一個號碼。我們可以手動或以編程方式定義。
# Define groups manually
ids <- c(12,1,10,11)
ids
#> [1] 12 1 10 11
# Pattern match groups
all_ids <- gsub(pattern = ".*Sample (\\d ).*", replacement = "\\1", x = g)
all_ids
#> [1] "12" "1" "1" "1" "1" "1" "10" "10" "10" "10" "10" "11" "11" "11" "11"
#> [16] "11" "12" "12" "12" "12"
ids <- unique(all_ids)
ids
#> [1] "12" "1" "10" "11"
在我們知道我們正在回圈什么之后,我們可以指定回圈。paste0()可以成為這里的主力。此回圈遍歷 id(一次一個 id),在 中找到匹配的字串g,并將它們作為向量寫入您的環境。在回圈的每次迭代期間,我們希望在我們的環境中出現一個新向量。
# For-loop with assign
for(i in ids){
a <- paste0("Sample ", i, "_")
h <- g[grep(a, g)]
h_name <- paste0("sample_", i, "_group")
assign(x = h_name, value = h)
}
這在技術上可行,但不是最好的。“Works”可能已經足夠好了,沒有問題,但是您可能會發現使用串列(向量的向量)來存盤來自 for 回圈的資訊實際上更方便。編程速度很快,您的作業區沒有一堆新物件,而且上面鏈接中的所有可怕的東西(不是真的)都不是問題。
# Save the results of a for-loop in a list!
# First, make a blank list to hold the results
results <- list()
for(i in ids){
a <- paste0("Sample ", i, "_")
h <- g[grep(a, g)]
h_name <- paste0("sample_", i, "_group")
results[[h_name]] <- h
}
results
#> $sample_12_group
#> [1] "New AS Plate 21_AS Plate_Sample 12_50.fcs"
#> [2] "New AS Plate 21_AS Plate_Sample 12_100.fcs"
#> [3] "New AS Plate 21_AS Plate_Sample 12_25.fcs"
#> [4] "New AS Plate 21_AS Plate_Sample 12_250.fcs"
#> [5] "New AS Plate 21_AS Plate_Sample 12_500.fcs"
#>
#> $sample_1_group
#> [1] "New AS Plate 21_AS Plate_Sample 1_100.fcs"
#> [2] "New AS Plate 21_AS Plate_Sample 1_25.fcs"
#> [3] "New AS Plate 21_AS Plate_Sample 1_250.fcs"
#> [4] "New AS Plate 21_AS Plate_Sample 1_50.fcs"
#> [5] "New AS Plate 21_AS Plate_Sample 1_500.fcs"
#>
#> $sample_10_group
#> [1] "New AS Plate 21_AS Plate_Sample 10_100.fcs"
#> [2] "New AS Plate 21_AS Plate_Sample 10_25.fcs"
#> [3] "New AS Plate 21_AS Plate_Sample 10_250.fcs"
#> [4] "New AS Plate 21_AS Plate_Sample 10_50.fcs"
#> [5] "New AS Plate 21_AS Plate_Sample 10_500.fcs"
#>
#> $sample_11_group
#> [1] "New AS Plate 21_AS Plate_Sample 11_100.fcs"
#> [2] "New AS Plate 21_AS Plate_Sample 11_25.fcs"
#> [3] "New AS Plate 21_AS Plate_Sample 11_250.fcs"
#> [4] "New AS Plate 21_AS Plate_Sample 11_50.fcs"
#> [5] "New AS Plate 21_AS Plate_Sample 11_500.fcs"
額外學分
For-loops are great: it's easy to see what's going on inside of them, its easy to do a lot of data handling in them, and they are usually reasonably fast to execute. But sometimes its all about speed. R is vectorized ([I'm honestly not exactly sure what this means][2] besides "it can do multiple calculations simultaneously"), but a for-loop doesn't take advantage of this very well. The apply() family of vectorized functions do, and they can usually be easy to implement in cases where you might also use a for-loop. Here's how you could do that with your data:
# Vectorized
lapply(ids, function(i) g[grep(paste0("Sample ", i, "_"), g)])
#> [[1]]
#> [1] "New AS Plate 21_AS Plate_Sample 12_50.fcs"
#> [2] "New AS Plate 21_AS Plate_Sample 12_100.fcs"
#> [3] "New AS Plate 21_AS Plate_Sample 12_25.fcs"
#> [4] "New AS Plate 21_AS Plate_Sample 12_250.fcs"
#> [5] "New AS Plate 21_AS Plate_Sample 12_500.fcs"
#>
#> [[2]]
#> [1] "New AS Plate 21_AS Plate_Sample 1_100.fcs"
#> [2] "New AS Plate 21_AS Plate_Sample 1_25.fcs"
#> [3] "New AS Plate 21_AS Plate_Sample 1_250.fcs"
#> [4] "New AS Plate 21_AS Plate_Sample 1_50.fcs"
#> [5] "New AS Plate 21_AS Plate_Sample 1_500.fcs"
#>
#> [[3]]
#> [1] "New AS Plate 21_AS Plate_Sample 10_100.fcs"
#> [2] "New AS Plate 21_AS Plate_Sample 10_25.fcs"
#> [3] "New AS Plate 21_AS Plate_Sample 10_250.fcs"
#> [4] "New AS Plate 21_AS Plate_Sample 10_50.fcs"
#> [5] "New AS Plate 21_AS Plate_Sample 10_500.fcs"
#>
#> [[4]]
#> [1] "New AS Plate 21_AS Plate_Sample 11_100.fcs"
#> [2] "New AS Plate 21_AS Plate_Sample 11_25.fcs"
#> [3] "New AS Plate 21_AS Plate_Sample 11_250.fcs"
#> [4] "New AS Plate 21_AS Plate_Sample 11_50.fcs"
#> [5] "New AS Plate 21_AS Plate_Sample 11_500.fcs"
Created on 2021-10-14 by the reprex package (v2.0.1)
Data:
g <- c("New AS Plate 21_AS Plate_Sample 12_50.fcs",
"New AS Plate 21_AS Plate_Sample 1_100.fcs",
"New AS Plate 21_AS Plate_Sample 1_25.fcs",
"New AS Plate 21_AS Plate_Sample 1_250.fcs",
"New AS Plate 21_AS Plate_Sample 1_50.fcs",
"New AS Plate 21_AS Plate_Sample 1_500.fcs",
"New AS Plate 21_AS Plate_Sample 10_100.fcs",
"New AS Plate 21_AS Plate_Sample 10_25.fcs",
"New AS Plate 21_AS Plate_Sample 10_250.fcs",
"New AS Plate 21_AS Plate_Sample 10_50.fcs",
"New AS Plate 21_AS Plate_Sample 10_500.fcs",
"New AS Plate 21_AS Plate_Sample 11_100.fcs",
"New AS Plate 21_AS Plate_Sample 11_25.fcs",
"New AS Plate 21_AS Plate_Sample 11_250.fcs",
"New AS Plate 21_AS Plate_Sample 11_50.fcs",
"New AS Plate 21_AS Plate_Sample 11_500.fcs",
"New AS Plate 21_AS Plate_Sample 12_100.fcs",
"New AS Plate 21_AS Plate_Sample 12_25.fcs",
"New AS Plate 21_AS Plate_Sample 12_250.fcs",
"New AS Plate 21_AS Plate_Sample 12_500.fcs")
[1]: Why is using assign bad?) [2]: How do I know a function or an operation in R is vectorized?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/322698.html
