使用不同的变量撰写大量链接

时间:2015-04-06 10:17:25

标签: r

我需要使用时间相关的变量来编写链接列表。到目前为止,我已经设法与paste()合作,但结果并不令人满意。链接遵循以下结构:

http://www.somesite/year/month/day/hour/minute.txt

我试图为每个部分创建一个变量。

link <-'http://www.somesite'

year<-as.character(c("2010","2011","2012","2013","2014"))
month<-as.character(c("01","02","03","04","05","06","07","08","09","10","11","12"))
day<-as.character(c("01","02","03","04","05","06","07","08","09","10","11","12",
                    "13","14","15","16","17","18","19","20","21","22","23","24",
                    "25","26","27","28","29","30","31"))
hour<-as.character(c("01","02","03","04","05","06","07","08","09","10","11","12",
                     "13","14","15","15","16","17","18","19","20","21","22","23",
                     "24"))
min <-as.character(c("00","10","20","30","40","50"))
ext <-as.character("ext")

使用paste()

link2 <-paste(link,year,"/",month,"/",day,"/",hour,"/",minute,".",ext)

我明白了:

  [1]"http:/somesite/2010/01/01/01/10.ext"
    [2]"http:/somesite/2011/02/02/02/20.ext"
    [3]"http:/somesite/2012/03/03/03/30.ext"
    [4]"http:/somesite/2013/04/04/04/40.ext"
    ****************************************

我想得到这样的东西:

 [1]"http:/somesite/2010/01/01/01/10.ext"
    [2]"http:/somesite/2010/02/01/01/20.ext"
    [3]"http:/somesite/2010/03/01/01/30.ext"
    [4]"http:/somesite/2010/04/01/01/40.ext"
    ****************************************

有什么想法吗?我不想手动输入它。 :)

感谢。

1 个答案:

答案 0 :(得分:1)

expand.grid可以让你开始......

dat  <- list(link  = 'http://www.somesite',
             year = as.character(c("2010","2011","2012","2013","2014")),
             month = as.character(c("01","02","03","04","05","06","07","08","09","10","11","12")),
             day = as.character(c("01","02","03","04","05","06","07","08","09","10","11","12",
                    "13","14","15","16","17","18","19","20","21","22","23","24",
                    "25","26","27","28","29","30","31")),
             hour = as.character(c("01","02","03","04","05","06","07","08","09","10","11","12",
                     "13","14","15","15","16","17","18","19","20","21","22","23",
                     "24")),
             min  = as.character(c("00","10","20","30","40","50")),
             ext  = as.character("ext"))

gr <- expand.grid(dat)

# sort results...
gr <- gr[order(gr$year, gr$month, gr$day, gr$hour, gr$min),]

head(gr)

#                       link year month day hour min ext
# 1      http://www.somesite 2010    01  01   01  00 ext
# 46501  http://www.somesite 2010    01  01   01  10 ext
# 93001  http://www.somesite 2010    01  01   01  20 ext
# 139501 http://www.somesite 2010    01  01   01  30 ext
# 186001 http://www.somesite 2010    01  01   01  40 ext
# 232501 http://www.somesite 2010    01  01   01  50 ext