R:循环遍历表以创建URL以进一步操作?

时间:2018-04-30 17:37:18

标签: r loops for-loop

所以我有一个表:100行和3列,我想使用R从它们创建url字符串,这些字符串用于进一步处理。

ffmpeg -i uservid1.mov -i uservid2.mp4 -i mask.png -i title1.mp4 -f lavfi -t 1 -i anullsrc 
-filter_complex
"[2:v][0:v]scale2ref[s1][s2];
 [s2][s1]overlay,hue=s=0,scale=720x400,setsar=1[v0];
 [2:v][1:v]scale2ref[s3][s4];
 [s4][s3]overlay,hue=s=0,scale=720x400,setsar=1[v1];
 [3:v]scale=720x400,setsar=1[v3];
 [v0][0:a][v3][4:a][v1][1:a]concat=n=3:v=1:a=1"
-preset ultrafast finalCV.mp4

我想获得这样的格式:从第一行开始,16个url以以下形式创建:baseurl + text1 + 1,baseurl + text1 + 2等通过16然后继续第2行相同的进程,但是1到154

我想象了下面这样的东西,但无论哪种方式都不起作用,我得到空洞的结果。

first rows of the Table is:
X1 X2         X3
1  text1      16
2  text2      154
etc. text etc 21

有人有什么建议吗?

调整帖子:

最后,我希望创建一个url列表,我可以将其用于进一步的操作。网址列表需要如下所示:(我为了清晰的阅读原因添加了[],因此它们不得出现在最终结果中......)

    baseurl <- "link"
#creating a dynamic i which goes from 1 trough input at intersection rownumber k, column 3
    i<- 1 in a1[k,2]
# creating a loop on rownumber k from table a1    
    for (k in 1:nrow(a1)) {
# pasting together baseurl, contents at intersection row k, column 2
      mydata <- fromJSON(paste0(baseurl,a1[k,2],i,"0"), flatten=TRUE)

    }

2 个答案:

答案 0 :(得分:1)

我一起攻击了这个功能,但它并不漂亮,可能不会很快。它还取决于一些外包装。

df <- data.frame(X1 = 1:2, X2 = c("text1", "text2"), X3 = c(16,154))
base_url <- "https://www.your_url_here.com"

create_urls <- function(input_tbl, base_url) {
  require(magrittr)
  require(tidyr)
  require(dplyr)

  # create a list of numeric sequences from 1 to length specified in X3
  x3_sequences <- lapply(input_tbl$X3, FUN = seq)

  # combine the sequences with each row, this will create a list of lists.
  # be sure toe comment out/remove the "/" row after base_url if your url already
  # has it.
  url_lists <- mapply(FUN = paste0, 
                      base_url, 
                      "/", 
                      input_tbl$X2, 
                      "/", 
                      x3_sequences,
                      SIMPLIFY = TRUE)

  # jump through some hoops to turn the list of lists into a dataframe
  # with a single column of url values. I use the tidyr and dplyr packages here
  # for expediency. I'm sure it's possible to accomplish the same things in base,
  # but i'd rather not spend the time to figure out how.
  url_df <- url_lists %>%
    sapply(FUN = strsplit, split = " ", simplify = "vector") %>%
    lapply(FUN = as.data.frame) %>%
    lapply(FUN = tidyr::gather, key = "n", value = "url") %>% #this is necessary because the strsplt and as.data.frame functions cause each url to be placed in their own column. the gather turns these columns into unique rows
    do.call(rbind, .) %>% 
    dplyr::select(url)

  #row names are automatically added by the do.call rbind function above. They
  #only serve to clutter the dataframe, so I've removed them.              
  row.names(url_df) <- NULL

  return(url_df)
}

运行create_urls(input_tbl = df, base_url = base_url)返回

                                         url
1     https://www.your_url_here.com/text1/1
2     https://www.your_url_here.com/text1/2
...
15   https://www.your_url_here.com/text1/15
16   https://www.your_url_here.com/text1/16
17    https://www.your_url_here.com/text2/1
18    https://www.your_url_here.com/text2/2
...
169 https://www.your_url_here.com/text2/153
170 https://www.your_url_here.com/text2/154

答案 1 :(得分:0)

您的问题有点不清楚,但听起来您需要以下内容:对于数据框的每一行,创建“[base_url] / text1 / n”形式的字符串,其中“n”取值1到X3的值。

通过调用mapply

,您可以相当轻松地完成此操作
df <- read.table(text = 'X1 X2 X3
1 text1 16
2 text2 154', header = T)

urls <- mapply(function(x, y) {sprintf('base_url/%s/%i', x, 1:y)}, x = df$X2, y = df$X3)

这将返回一个列表,其中每个元素都包含一个对应于X2值的向量,其中的字符串对应于X3的最大值。