有没有一种方法可以使用 foreach 循环根据一个数据框中的其他数字列将值按行分配给一列?
Is there a way to use foreach loop to assign values row-wise to a column based on other numeric column within one dataframe?
我有一个包含两列的数据框,一列是数字列,另一列是字符列
我有将近 300 行数字和 url 链接
基于数字列,我想遍历每个 url 从 1 到每行 num 中值的次数
我尝试使用此代码:
sites <- for (i in 1:df$v1) {
foreach (n = i, .combine = cbind) %dopar% {data.frame( paste(df$url, n,sep= ""))}}
但是,此代码仅采用第一个数值并重复所有 url,而不是采用逐行数值
我希望这样:
https://forums.vwvortex.com/showthread.php?9147410-Durango/page1
https://forums.vwvortex.com/showthread.php?9147410-Durango/page2
And so on ...
https://forums.vwvortex.com/showthread.php?9147410-Durango/page14
https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page1
https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page2
And so on ...
https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page5
感谢您对此提供的任何帮助或建议。提前致谢。
如果您想使用 sapply
以一种方式遍历每一行
sapply(1:nrow(df), function(i) paste0(df$url[i], seq_len(df$V1[i])))
#[[1]]
# [1] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page1"
# [2] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page2"
# [3] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page3"
# [4] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page4"
# [5] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page5"
# [6] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page6"
# [7] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page7"
# [8] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page8"
# [9] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page9"
#[10] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page10"
#[11] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page11"
#[12] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page12"
#[13] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page13"
#[14] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page14"
#[[2]]
#[1] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page1"
#[2] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page2"
#[3] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page3"
#[4] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page4"
#[5] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page5"
这为您提供了一个字符向量列表,但如果您只想将所有内容都作为一个向量,则可以 unlist
sapply
调用
unlist(sapply(1:nrow(df), function(i) paste0(df$url[i], seq_len(df$V1[i]))))
假设您的初始数据是这样的:
df<- data.frame(url=c(
"https://forums.vwvortex.com/showthread.php?9147410-Durango/page",
"https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page"),
V1 = c(14, 5))
我们可以用 rep
和 sequence
从 base R
做到这一点
with(df, paste0(rep(url, V1), sequence(V1)))
# [1] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page1"
# [2] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page2"
# [3] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page3"
# [4] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page4"
# [5] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page5"
# [6] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page6"
# [7] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page7"
# [8] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page8"
# [9] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page9"
#[10] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page10"
#[11] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page11"
#[12] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page12"
#[13] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page13"
#[14] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page14"
#[15] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page1"
#[16] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page2"
#[17] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page3"
#[18] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page4"
#[19] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page5"
或使用 foreach
library(foreach)
out <- foreach(i = seq_len(nrow(df)), .combine = rbind) %dopar%
data.frame(Col1 = paste0(df$url[i], seq(df$V1[i])))
数据
df <- structure(list(url = structure(2:1,
.Label = c("https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page",
"https://forums.vwvortex.com/showthread.php?9147410-Durango/page"
), class = "factor"), V1 = c(14, 5)), class = "data.frame", row.names = c(NA,
-2L))
我有将近 300 行数字和 url 链接
基于数字列,我想遍历每个 url 从 1 到每行 num 中值的次数
我尝试使用此代码:
sites <- for (i in 1:df$v1) {
foreach (n = i, .combine = cbind) %dopar% {data.frame( paste(df$url, n,sep= ""))}}
但是,此代码仅采用第一个数值并重复所有 url,而不是采用逐行数值
我希望这样:
https://forums.vwvortex.com/showthread.php?9147410-Durango/page1
https://forums.vwvortex.com/showthread.php?9147410-Durango/page2
And so on ...
https://forums.vwvortex.com/showthread.php?9147410-Durango/page14
https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page1
https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page2
And so on ...
https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page5
感谢您对此提供的任何帮助或建议。提前致谢。
如果您想使用 sapply
sapply(1:nrow(df), function(i) paste0(df$url[i], seq_len(df$V1[i])))
#[[1]]
# [1] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page1"
# [2] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page2"
# [3] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page3"
# [4] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page4"
# [5] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page5"
# [6] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page6"
# [7] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page7"
# [8] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page8"
# [9] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page9"
#[10] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page10"
#[11] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page11"
#[12] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page12"
#[13] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page13"
#[14] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page14"
#[[2]]
#[1] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page1"
#[2] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page2"
#[3] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page3"
#[4] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page4"
#[5] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page5"
这为您提供了一个字符向量列表,但如果您只想将所有内容都作为一个向量,则可以 unlist
sapply
调用
unlist(sapply(1:nrow(df), function(i) paste0(df$url[i], seq_len(df$V1[i]))))
假设您的初始数据是这样的:
df<- data.frame(url=c(
"https://forums.vwvortex.com/showthread.php?9147410-Durango/page",
"https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page"),
V1 = c(14, 5))
我们可以用 rep
和 sequence
从 base R
with(df, paste0(rep(url, V1), sequence(V1)))
# [1] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page1"
# [2] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page2"
# [3] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page3"
# [4] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page4"
# [5] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page5"
# [6] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page6"
# [7] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page7"
# [8] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page8"
# [9] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page9"
#[10] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page10"
#[11] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page11"
#[12] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page12"
#[13] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page13"
#[14] "https://forums.vwvortex.com/showthread.php?9147410-Durango/page14"
#[15] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page1"
#[16] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page2"
#[17] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page3"
#[18] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page4"
#[19] "https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page5"
或使用 foreach
library(foreach)
out <- foreach(i = seq_len(nrow(df)), .combine = rbind) %dopar%
data.frame(Col1 = paste0(df$url[i], seq(df$V1[i])))
数据
df <- structure(list(url = structure(2:1,
.Label = c("https://forums.vwvortex.com/showthread.php?8943521-Larger-tires/page",
"https://forums.vwvortex.com/showthread.php?9147410-Durango/page"
), class = "factor"), V1 = c(14, 5)), class = "data.frame", row.names = c(NA,
-2L))