我不知道描述這個的正確方式,所以請多多包涵。基本上,我在 R 中有一個函式可以讀取資料框并以特定順序粘貼內容——它用于為 LaTeX 撰寫 tex 檔案,因此我可以非常快速地制作數百個標簽。
我附上了一個只有 for 回圈的簡化版本。我希望做的是讓代碼回圈遍歷四行資料,為第五行做一些不同的事情,然后回傳接下來的四行資料。在下面的示例中,它將為大多數行粘貼一個短語,而在第五行它將粘貼其他內容——每個都基于資料框。
對于我的實際代碼,我想水平翻轉標簽以盡可能多地使用紙張。見附圖。
, 但實際上這一切都歸結為我認為的 for 回圈。
data <- data.frame(title = c("big Cactus", "little cactus", "bad cactus", "skinny cactus",
"round cactus", "spiny cactus", "twisty cactus", "bizarre cactus"),
name = c("Franklin", "Stephanie", "Megan", "Mark",
"Patricia", "KD", "Claude", "Audrey"),
needs = c("nothing", "some love", "too much", "some water",
"some sunlight", "new soil", "transplanted", "a friend"))
for (this.label in 1:dim(data)[1]) {
#main function I want for every label
line <- paste0(data$name[this.label], " the ", data$title[this.label], " needs ", data$needs[this.label])
print(line)
}
example of alternate function for every fourth row of the data frame:
line <- paste0(data$name[this.label], " is feeling left out!")
uj5u.com熱心網友回復:
make_label_A <- function(this.label) {
line <- paste0(data$name[this.label], " the ", data$title[this.label], " needs ", data$needs[this.label])
print(line)
}
make_label_B <- function(this.label) {
line <- paste0(data$name[this.label], " is feeling left out!")
print(line)
}
for (i in seq(1,dim(data)[1], 5)) {
sapply(1:4, \(x) make_label_A(i x))
make_label_B(i 4)
}
希望這與您使用的 LaTeX 引擎一致。有時列印陳述句需要更小心地從回圈內顯示。
uj5u.com熱心網友回復:
這是基本 R 中的一個解決方案,需要對現有代碼進行最少的修改。
解決方案
在您的for回圈中,只需this.label使用模運算子測驗您的索引,%%如下所示:
for (this.label in seq_len(nrow(data))) {
if (this.label %% 5 == 0) {
# example of alternate function for every fourth row of the data frame:
line <- paste0(data$name[this.label], " is feeling left out!")
} else {
# main function I want for every label
line <- paste0(data$name[this.label], " the ", data$title[this.label], " needs ", data$needs[this.label])
}
print(line)
}
本次測驗
this.label %% 5 == 0
將TRUE每五個回圈評估一次(對于this.label∈ { 5, 10, 15, ...}),因此執行備用“函式”(技術上的“運算式”);而其他四個回圈將簡單地執行“主函式”(運算式)。
結果
鑒于data您的樣本
data <- data.frame(
title = c("big Cactus", "little cactus", "bad cactus", "skinny cactus", "round cactus", "spiny cactus", "twisty cactus", "bizarre cactus"),
name = c("Franklin", "Stephanie", "Megan", "Mark", "Patricia", "KD", "Claude", "Audrey"),
needs = c("nothing", "some love", "too much", "some water", "some sunlight", "new soil", "transplanted", "a friend")
)
此解決方案應產生以下輸出:
[1] "Franklin the big Cactus needs nothing"
[1] "Stephanie the little cactus needs some love"
[1] "Megan the bad cactus needs too much"
[1] "Mark the skinny cactus needs some water"
[1] "Patricia is feeling left out!"
[1] "KD the spiny cactus needs new soil"
[1] "Claude the twisty cactus needs transplanted"
[1] "Audrey the bizarre cactus needs a friend"
為了獲得更清晰的輸出,您還可以將最后一個命令print(line)替換為 command cat(line, sep = "\n"),這將產生以下輸出:
Franklin the big Cactus needs nothing
Stephanie the little cactus needs some love
Megan the bad cactus needs too much
Mark the skinny cactus needs some water
Patricia is feeling left out!
KD the spiny cactus needs new soil
Claude the twisty cactus needs transplanted
Audrey the bizarre cactus needs a friend
筆記
我已經用 替換了笨重1:dim(data)[1]的seq_len(nrow(data)),這是迭代表行的正確方法。在表為空(0行)的情況下,這可以防止任何以不存在的行為目標的回圈1:0(即c(1, 0))。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/409149.html
標籤:
