2015-04-06 28 views
2

我需要使用時間相關變量編寫鏈接列表。到目前爲止,我已經設法使用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" 
    **************************************** 

任何想法?我不想手動輸入。 :)

謝謝。

+0

我不知道如果我明白你的問題。預期的輸出以我想要的方式輸入,而不是paste()輸出。 – julics 2015-04-06 10:22:37

回答

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 
+0

你節省了我的一天。非常感謝! – julics 2015-04-06 10:31:37

+0

非常歡迎! :) – 2015-04-06 10:33:20

+0

如果你想通過* all *列來命令,絕對不需要命名所有的列名,只需要'gr < - gr [do.call(order,gr),]'' – 2015-04-06 10:51:11