Как запустить цикл for (или другой подход) в диапазоне двух столбцов - PullRequest
0 голосов
/ 01 февраля 2019

У меня есть цикл for, который я использую для некоторого просмотра веб-страниц.Например, скажем, он собирает исторические данные о запасах.

start <- 1533103200
end <- 1549004400

company <- c("fb","amzn","f")

for (i in company){
    print(paste('https://finance.yahoo.com/quote/',i, '/history?period1=',start,'&period2=',maxDate,'&interval=1d&filter=history&frequency=1d',sep=""))
}

Start и End - это коды даты.Теперь у меня есть data.frame кодов даты начала и окончания (100-дневные интервалы), которые я также хочу включить в список печатных ссылок, а это означает, что вместо трех ссылок мне нужно 3 x nrow из следующего data.frame.В этом примере это будет 6 ссылок ...

start <- c(1533193200,1541833200)
end <- c(1541746800,1549004400)
dates <- as.data.frame(cbind(start,end))

Список динамический и длинный, поэтому мне, вероятно, придется встроить цикл for в другой цикл for, но у меня нетбольшой опыт использования двух переменных для этой цели.Любая помощь будет отличной!

Ожидаемые результаты будут ....

[1] "https://finance.yahoo.com/quote/fb/history?period1=1533193200&period2=1541746800&interval=1d&filter=history&frequency=1d"
[1] "https://finance.yahoo.com/quote/amzn/history?period1=1533193200&period2=1541746800&interval=1d&filter=history&frequency=1d"
[1] "https://finance.yahoo.com/quote/f/history?period1=1533193200&period2=1541746800&interval=1d&filter=history&frequency=1d"
[1] "https://finance.yahoo.com/quote/fb/history?period1=1541833200&period2=1549004400&interval=1d&filter=history&frequency=1d"
[1] "https://finance.yahoo.com/quote/amzn/history?period1=1541833200&period2=1549004400&interval=1d&filter=history&frequency=1d"
[1] "https://finance.yahoo.com/quote/f/history?period1=1541833200&period2=1549004400&interval=1d&filter=history&frequency=1d"

... вместо результатов только первого цикла, как ...

[1] "https://finance.yahoo.com/quote/fb/history?period1=1533103200&period2=1548918000&interval=1d&filter=history&frequency=1d"
[1] "https://finance.yahoo.com/quote/amzn/history?period1=1533103200&period2=1548918000&interval=1d&filter=history&frequency=1d"
[1] "https://finance.yahoo.com/quote/f/history?period1=1533103200&period2=1548918000&interval=1d&filter=history&frequency=1d"

Ответы [ 2 ]

0 голосов
/ 01 февраля 2019

Я немного упростил вашу data.frame конструкцию:

df <- data.frame(
  start = c(1533193200, 1541833200),
  end = c(1541746800, 1549004400)
)

Тогда я бы выделил новые столбцы в этом data.frame для каждой компании:

companies <- c("fb", "amzn", "f")
df[, companies] <- ""

Теперь вы можете выполнять итерациинад новыми столбцами и заполните их ссылками:

for (i in companies) {
  df[, i] <- paste0(
    'https://finance.yahoo.com/quote/',
    i, '/history?period1=',
    df$start,
    '&period2=',
    df$maxDate,
    '&interval=1d&filter=history&frequency=1d')
}

Вы получите хороший чистый data.frame со ссылками для каждой компании в отдельном столбце:

> df
       start        end
1 1533193200 1541746800
2 1541833200 1549004400


fb
1 https://finance.yahoo.com/quote/fb/history?period1=1533193200&period2=&interval=1d&filter=history&frequency=1d
2 https://finance.yahoo.com/quote/fb/history?period1=1541833200&period2=&interval=1d&filter=history&frequency=1d
                                                                                                              amzn
1 https://finance.yahoo.com/quote/amzn/history?period1=1533193200&period2=&interval=1d&filter=history&frequency=1d
2 https://finance.yahoo.com/quote/amzn/history?period1=1541833200&period2=&interval=1d&filter=history&frequency=1d
                                                                                                              f
1 https://finance.yahoo.com/quote/f/history?period1=1533193200&period2=&interval=1d&filter=history&frequency=1d
2 https://finance.yahoo.com/quote/f/history?period1=1541833200&period2=&interval=1d&filter=history&frequency=1d

Вы можете«аккуратно», если вы предпочитаете столбец со ссылками, а другие столбцы служат метаинформацией о ссылках:

df_tidy <- tidyr::gather(df, company, url, -start, -end)

> df_tidy$url
[1] "https://finance.yahoo.com/quote/fb/history?period1=1533193200&period2=&interval=1d&filter=history&frequency=1d"  
[2] "https://finance.yahoo.com/quote/fb/history?period1=1541833200&period2=&interval=1d&filter=history&frequency=1d"  
[3] "https://finance.yahoo.com/quote/amzn/history?period1=1533193200&period2=&interval=1d&filter=history&frequency=1d"
[4] "https://finance.yahoo.com/quote/amzn/history?period1=1541833200&period2=&interval=1d&filter=history&frequency=1d"
[5] "https://finance.yahoo.com/quote/f/history?period1=1533193200&period2=&interval=1d&filter=history&frequency=1d"   
[6] "https://finance.yahoo.com/quote/f/history?period1=1541833200&period2=&interval=1d&filter=history&frequency=1d"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...