当前位置: 首页 > 面试题库 >

R如何在不知道长度的情况下进行for循环?

太叔高义
2023-03-14
问题内容

目前,我正在此站点上抓取图表图表信息。要抓取信息,我需要为有很多镜头的情况做一个for循环。我可以通过单击“
Team Stats”并找到射门次数来找到射门次数。

我想做出适当的选择for loop而不必找出拍摄数量。

我目前正在做什么:

shotchart <- data.frame(shot=as.vector(0), class=as.vector(0), data_homeaway=as.vector(0), 
                    data_period=as.vector(0), player_id=as.vector(0), data_text=as.vector(0),
                    location=as.vector(0), gamenumber= as.vector(0))

for (i in 1:54)
{
 text <-paste0("//*[(@class='shots home-team')]//*[(@id)][",i,"]")
 shotchart[nrow(shotchart)+1,1]<- unlist(re$findElement(using='xpath',text)$getElementAttribute('id'))
shotchart[nrow(shotchart),2]<- unlist(re$findElement(using='xpath', text)$getElementAttribute('class'))
shotchart[nrow(shotchart),3]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-homeaway'))
shotchart[nrow(shotchart),4]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-period'))
shotchart[nrow(shotchart),5]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-shooter'))
shotchart[nrow(shotchart),6]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-text'))
shotchart[nrow(shotchart),7]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('style'))
shotchart[nrow(shotchart),8]<-k-1

}

任何帮助将不胜感激。请让我知道是否需要进一步的信息。


问题答案:
library(RSelenium)
checkForServer()
startServer()
Sys.sleep(5)
re<-remoteDriver()
re$open()
re$navigate("http://espn.go.com/mens-college-basketball/playbyplay?gameId=400830392")

shotchart <- data.frame(shot=as.vector(0), class=as.vector(0), data_homeaway=as.vector(0), 
                        data_period=as.vector(0), player_id=as.vector(0), data_text=as.vector(0),
                        location=as.vector(0), gamenumber= as.vector(0))
error="Error : \t Summary: NoSuchElement\n \t Detail: An element could not be located on the page using the given search parameters.\n \t class: org.openqa.selenium.NoSuchElementException\n"
i<-1
element=0
while ((i>0)&(element[1]!=error))
{
  text <-paste0("//*[(@class='shots home-team')]//*[(@id)][",i,"]")
  element<- try(unlist(re$findElement(using='xpath', text)$getElementAttribute('id')),silent = TRUE)
  if (element[1]==error)
    break;
  shotchart[nrow(shotchart)+1,1]<- unlist(re$findElement(using='xpath',text)$getElementAttribute('id'))
  shotchart[nrow(shotchart),2]<- unlist(re$findElement(using='xpath', text)$getElementAttribute('class'))
  shotchart[nrow(shotchart),3]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-homeaway'))
  shotchart[nrow(shotchart),4]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-period'))
  shotchart[nrow(shotchart),5]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-shooter'))
  shotchart[nrow(shotchart),6]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('data-text'))
  shotchart[nrow(shotchart),7]<-unlist(re$findElement(using='xpath', text)$getElementAttribute('style'))
  shotchart[nrow(shotchart),8]<-i-1
  i<-i+1
}

我不确定您的变量k是什么意思。

我从所有评论中获得了建议。



 类似资料:
  • 我使用以下命令行与ffmpeg一起向没有任何音轨的视频添加音轨。

  • 我有一个非常基本的函数,它搜索的数组列表,并返回与传递给它的参数匹配的帐户。但是,一旦抛出CustomerAccountNotFoundException,我的for循环就会中断。 我通过在异常后打印的值来测试这一点,该值一直被重置为0。如何在抛出异常后继续循环?我希望每次帐户不匹配时都抛出它,当它匹配时返回帐户。我还尝试过但不起作用。

  • 问题内容: 如果在使用for- each循环遍历Collection的同时修改了它,它会给出。有什么解决方法吗? 问题答案: 使用。 这是在迭代过程中修改集合的唯一安全方法。有关更多信息,请参见“收集接口”教程。 如果还需要在迭代时 _添加_元素的功能,请使用。

  • 我必须在不知道列类型的情况下使用preparedstatement/statement运行jdbc更新查询。我有一个查询说“更新表集状态=?”?其中id=?'我得到了一张{(“status”=“123”),(“id”=“546”)} 现在我不知道列类型,有没有通用的方法来使用jdbc运行这个查询? 而不是运行-ps.setString(1,map.get((状态));因为我不知道列类型的状态字段在

  • 问题内容: 我有个问题。我正在尝试将一些字符串转换为日期,但我不知道日期到达的格式。 这或许让他们或等。 如何将这些字符串转换为Date?我尝试了这个: 但是,当我打印出someDate时,它的打印方式是这样的:2019-08-05 12:42:48.638 CEST这意味着,但是当我运行以上代码时,日期对象现在变成了,至少可以这样说。 有什么想法可以正确格式化日期格式吗? 问题答案: 你不能!

  • 假设我在java中有一个主类,它接收一个矩阵[][],比如: ` '然后用户输入输入: 我知道这是一个5x5的矩阵,但只是因为我看到了用户输入的内容,我如何获得矩阵的大小- 我什么也没试过,我是Java初学者,所以请耐心等待。