使用 for 循环对环境中的多个对象执行相同的任务
Perform same task on multiple objects in the environment using for loop
我有这些数据:
u1 <- 'www.link1.com'
u2 <- 'www.link2.com'
u3 <- 'www.link3.com'
u4 <- 'www.link4.com'
我想对它们做一些工作,如何使用 for-loop 来做这个?!
for (i in u1 : ur4)
{
texti <- gettxt(i)
}
正如@slowowl 和@r2evans 所指出的,您不需要循环,因为 R 是矢量化的。考虑将 URL 存储在向量中。
您可以按如下方式进行:
URLs <- c('www.link1.com', 'www.link2.com', 'www.link3.com', 'www.link4.com')
然后你可以在这个向量上使用 lapply
,如下所示:
text <- lapply(URLs, gettext)
要从单个变量创建向量,您可以使用以下代码:
vec <- c()
for (i in 1:4) {
vec <- c(vec, get(sprintf("u%i", i)))
}
[1] "www.link1.com" "www.link2.com" "www.link3.com" "www.link4.com"
您可以使用 sapply
和 get
来获取对象的值。
sapply
的第一个参数取决于你有多少个url。
sapply(1:4, function(x) gettext(get(paste0("u", x))))
[1] "www.link1.com" "www.link2.com" "www.link3.com" "www.link4.com"
已更新
如果您希望将 gettxt
结果导出到单个向量,您可以使用以下内容(请注意,此处使用 lapply
而不是 sapply
):
setNames(lapply(1:4, function(x) gettxt(get(paste0("u", x)))), paste0("u", 1:4, "_txt")) %>%
list2env(envir = globalenv())
它将输出四个名称为u1_txt
到u4_txt
的向量
grep("^u", ls(), value = T)
[1] "u1" "u1_txt" "u2" "u2_txt" "u3" "u3_txt" "u4"
[8] "u4_txt"
例子
我使用了一些虚拟站点来测试代码。
u1 <- "https://CRAN.R-project.org/package=htm2txt"
u2 <- "https://CRAN.R-project.org/package=dplyr"
u3 <- "https://CRAN.R-project.org/package=tidyverse"
u4 <- "https://CRAN.R-project.org/package=tidyr"
setNames(lapply(1:4, function(x) gettxt(get(paste0("u", x)))), paste0("u", 1:4, "_txt")) %>%
list2env(envir = globalenv())
grep("^u", ls(), value = T)
[1] "u1" "u1_txt" "u2" "u2_txt" "u3" "u3_txt" "u4"
[8] "u4_txt"
u1_txt
[1] "htm2txt: Convert Html into Text\n\nConvert a html document to simple plain texts by removing all html tags. This package utilizes regular expressions to strip off html tags. It also offers gettxt() and browse() function, which enables you to get or browse texts at a certain web page.\n\nVersion: 2.1.1\n\nDepends: R (≥ 3.0.0)\n\nPublished: 2017-10-19\n\nAuthor: Sangchul Park [aut, cre]\n\nMaintainer: Sangchul Park <mail at sangchul.com>\n\nBugReports: https://github.com/sangchulpark/htm2txt/issues\n\nLicense: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]\n\nURL: https://github.com/sangchulpark\n\nNeedsCompilation: no\n\nIn views: WebTechnologies\n\nCRAN checks: htm2txt results\n\nDocumentation:\n\nReference manual: htm2txt.pdf\n\nDownloads:\n\nPackage source: htm2txt_2.1.1.tar.gz\n\nWindows binaries: r-devel: htm2txt_2.1.1.zip, r-release: htm2txt_2.1.1.zip, r-oldrel: htm2txt_2.1.1.zip\n\nmacOS binaries: r-release (arm64): htm2txt_2.1.1.tgz, r-release (x86_64): htm2txt_2.1.1.tgz, r-oldrel: htm2txt_2.1.1.tgz\n\nOld sources: htm2txt archive\n\nReverse dependencies:\n\nReverse imports: getDEE2\n\nLinking:\n\nPlease use the canonical form https://CRAN.R-project.org/package=htm2txt to link to this page."
我有这些数据:
u1 <- 'www.link1.com'
u2 <- 'www.link2.com'
u3 <- 'www.link3.com'
u4 <- 'www.link4.com'
我想对它们做一些工作,如何使用 for-loop 来做这个?!
for (i in u1 : ur4)
{
texti <- gettxt(i)
}
正如@slowowl 和@r2evans 所指出的,您不需要循环,因为 R 是矢量化的。考虑将 URL 存储在向量中。
您可以按如下方式进行:
URLs <- c('www.link1.com', 'www.link2.com', 'www.link3.com', 'www.link4.com')
然后你可以在这个向量上使用 lapply
,如下所示:
text <- lapply(URLs, gettext)
要从单个变量创建向量,您可以使用以下代码:
vec <- c()
for (i in 1:4) {
vec <- c(vec, get(sprintf("u%i", i)))
}
[1] "www.link1.com" "www.link2.com" "www.link3.com" "www.link4.com"
您可以使用 sapply
和 get
来获取对象的值。
sapply
的第一个参数取决于你有多少个url。
sapply(1:4, function(x) gettext(get(paste0("u", x))))
[1] "www.link1.com" "www.link2.com" "www.link3.com" "www.link4.com"
已更新
如果您希望将 gettxt
结果导出到单个向量,您可以使用以下内容(请注意,此处使用 lapply
而不是 sapply
):
setNames(lapply(1:4, function(x) gettxt(get(paste0("u", x)))), paste0("u", 1:4, "_txt")) %>%
list2env(envir = globalenv())
它将输出四个名称为u1_txt
到u4_txt
grep("^u", ls(), value = T)
[1] "u1" "u1_txt" "u2" "u2_txt" "u3" "u3_txt" "u4"
[8] "u4_txt"
例子
我使用了一些虚拟站点来测试代码。
u1 <- "https://CRAN.R-project.org/package=htm2txt"
u2 <- "https://CRAN.R-project.org/package=dplyr"
u3 <- "https://CRAN.R-project.org/package=tidyverse"
u4 <- "https://CRAN.R-project.org/package=tidyr"
setNames(lapply(1:4, function(x) gettxt(get(paste0("u", x)))), paste0("u", 1:4, "_txt")) %>%
list2env(envir = globalenv())
grep("^u", ls(), value = T)
[1] "u1" "u1_txt" "u2" "u2_txt" "u3" "u3_txt" "u4"
[8] "u4_txt"
u1_txt
[1] "htm2txt: Convert Html into Text\n\nConvert a html document to simple plain texts by removing all html tags. This package utilizes regular expressions to strip off html tags. It also offers gettxt() and browse() function, which enables you to get or browse texts at a certain web page.\n\nVersion: 2.1.1\n\nDepends: R (≥ 3.0.0)\n\nPublished: 2017-10-19\n\nAuthor: Sangchul Park [aut, cre]\n\nMaintainer: Sangchul Park <mail at sangchul.com>\n\nBugReports: https://github.com/sangchulpark/htm2txt/issues\n\nLicense: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]\n\nURL: https://github.com/sangchulpark\n\nNeedsCompilation: no\n\nIn views: WebTechnologies\n\nCRAN checks: htm2txt results\n\nDocumentation:\n\nReference manual: htm2txt.pdf\n\nDownloads:\n\nPackage source: htm2txt_2.1.1.tar.gz\n\nWindows binaries: r-devel: htm2txt_2.1.1.zip, r-release: htm2txt_2.1.1.zip, r-oldrel: htm2txt_2.1.1.zip\n\nmacOS binaries: r-release (arm64): htm2txt_2.1.1.tgz, r-release (x86_64): htm2txt_2.1.1.tgz, r-oldrel: htm2txt_2.1.1.tgz\n\nOld sources: htm2txt archive\n\nReverse dependencies:\n\nReverse imports: getDEE2\n\nLinking:\n\nPlease use the canonical form https://CRAN.R-project.org/package=htm2txt to link to this page."