2013-02-05 94 views
5

我有一個氣象站及其位置的經緯度列表。有格式問題,其中一些有幾個小時和幾分鐘,而其他有幾個小時,幾分鐘和幾秒鐘。我可以使用正則表達式來查找模式,但是我無法解壓縮單個部分。R正則表達式/ gsub:提取模式的一部分

這裏的數據:

> head(wthrStat1) 
    Station  lat  lon 
1940 K01R 31-08N 092-34W 
1941 K01T 28-08N 094-24W 
1942 K03Y 48-47N 096-57W 
1943 K04V 38-05-50N 106-10-07W 
1944 K05F 31-25-16N 097-47-49W 
1945 K06D 48-53-04N 099-37-15W 

我想是這樣的:

Station  latHr latMin latSec latDir lonHr lonMin lonSec lonDir 
    1940 K01R 31 08  00  N  092 34  00  W 
    1941 K01T 28 08  00  N  094 24  00  W 
    1942 K03Y 48 47  00  N  096 57  00  W 
    1943 K04V 38 05  50  N  106 10  07  W 
    1944 K05F 31 25  16  N  097 47  49  W 
    1945 K06D 48 53  04  N  099 37  15  W 

我能得到匹配這個正則表達式:

data.format <- "\\d{1,3}-\\d{1,3}(?:-\\d{1,3})?[NSWE]{1}" 
grep(data.format, wthrStat1$lat) 

但我不確定如何獲得個別部分分欄。我試過幾件事:

wthrStat1$latHr <- ifelse(grepl(data.format, wthrStat1$lat), gsub(????), NA) 

但沒有運氣。

這裏有一個dput():

> dput(wthrStat1[1:10,]) 
structure(list(Station = c("K01R", "K01T", "K03Y", "K04V", "K05F", 
"K06D", "K07G", "K07S", "K08D", "K0B9"), lat = c("31-08N", "28-08N", 
"48-47N", "38-05-50N", "31-25-16N", "48-53-04N", "42-34-28N", 
"47-58-27N", "48-18-03N", "43-20N"), lon = c("092-34W", "094-24W", 
"096-57W", "106-10-07W", "097-47-49W", "099-37-15W", "084-48-41W", 
"117-25-42W", "102-24-23W", "070-24W")), .Names = c("Station", 
"lat", "lon"), row.names = 1940:1949, class = "data.frame") 

有什麼建議?

+0

您是否嘗試過*使用正則表達式中的pharentesis對每列進行分組? –

+0

@OscarMederos:不,你會怎麼做? – screechOwl

+0

我不知道如何使用正則表達式在'R'中工作,但是在大多數語言中,您可以通過在pharentesis中包含所需的內容來創建組。像'id =(\ d +)'。如果你的比賽是'id = 1234',那麼第一組將是'1234'。 –

回答

6

這是極其低效的,我希望別人有更好的解決辦法:

dat <- read.table(text =' Station  lat  lon 
1940 K01R 31-08N 092-34W 
1941 K01T 28-08N 094-24W 
1942 K03Y 48-47N 096-57W 
1943 K04V 38-05-50N 106-10-07W 
1944 K05F 31-25-16N 097-47-49W 
1945 K06D 48-53-04N 099-37-15W', head=T) 


pattern <- '([0-9]+)[-]([0-9]+)([-|A-Z]+)([0-9]*)([A-Z]*)' 

dat$latHr <- gsub(pattern,'\\1',dat$lat) 
dat$latMin <- gsub(pattern,'\\2',dat$lat) 

latSec <- gsub(pattern,'\\4',dat$lat) 
latSec[nchar(latSec)==0] <- '00' 
dat$latSec <- latSec 

latDir <- gsub(pattern,'\\5',dat$lat) 
latDir[nchar(latDir)==0] <- latDir[nchar(latDir)!=0][1] 
dat$latDir <- latDir 

dat 
    Station  lat  lon latHr latMin latSec latDir 
1940 K01R 31-08N 092-34W 31  08  00  N 
1941 K01T 28-08N 094-24W 28  08  00  N 
1942 K03Y 48-47N 096-57W 48  47  00  N 
1943 K04V 38-05-50N 106-10-07W 38  05  50  N 
1944 K05F 31-25-16N 097-47-49W 31  25  16  N 
1945 K06D 48-53-04N 099-37-15W 48  53  04  N 
7

strapplyc在gsubfn包解壓每一組正則表達式用括號括起來:

library(gsubfn) 
data.format <- "(\\d{1,3})-(\\d{1,3})-?(\\d{1,3})?([NSWE]{1})" 
parts <- strapplyc(wthrStat1$lat, data.format, simplify = rbind) 
parts[parts == ""] <- "00" 

這給:

> parts 
     [,1] [,2] [,3] [,4] 
[1,] "31" "08" "00" "N" 
[2,] "28" "08" "00" "N" 
[3,] "48" "47" "00" "N" 
[4,] "38" "05" "50" "N" 
[5,] "31" "25" "16" "N" 
[6,] "48" "53" "04" "N" 
[7,] "42" "34" "28" "N" 
[8,] "47" "58" "27" "N" 
[9,] "48" "18" "03" "N" 
[10,] "43" "20" "00" "N" 
+0

感謝您指出gsubfn軟件包。我喜歡通過擴展* apply函數來適應R的方式。順便說一句,對我來說,strapplyc返回一個數據框,其中包含'c'的6行1列。 strapply,而部分< - strapply(df $ lat,pattern,FUN = c,simplify = rbind)按預期工作。 (R 2.15.1上gsubfn的版本0.6-5,它沒有找到tcltk)。 – cbare

+1

@cbare,它的一個錯誤。它現在固定在顛覆回購。要使用它:'library(gsbufn);來源(「http://gsubfn.googlecode.com/svn/trunk/R/strapplyc.R」)。另外還有關於在常見問題解答中安裝tcltk的信息:https://code.google.com/p/gsubfn/#FAQs –

2

另一個答案,使用stringr

# example data 
data <- 
"Station  lat  lon 
1940 K01R 31-08N 092-34W 
1941 K01T 28-08N 094-24W 
1942 K03Y 48-47N 096-57W 
1943 K04V 38-05-50N 106-10-07W 
1944 K05F 31-25-16N 097-47-49W 
1945 K06D 48-53-04N 099-37-15W" 

## read string into a data.frame 
df <- read.table(text=data, head=T, stringsAsFactors=F) 

pattern <- "(\\d{1,3})-(\\d{1,3})(?:-(\\d{1,3}))?([NSWE]{1})" 

library(stringr) 
str_match(df$lat, pattern) 

這產生與用於整個匹配的字符串一列併爲每個捕獲基團的附加列中的data.frame。

 [,1]  [,2] [,3] [,4] [,5] 
[1,] "31-08N" "31" "08" "" "N" 
[2,] "28-08N" "28" "08" "" "N" 
[3,] "48-47N" "48" "47" "" "N" 
[4,] "38-05-50N" "38" "05" "-50" "N" 
[5,] "31-25-16N" "31" "25" "-16" "N" 
[6,] "48-53-04N" "48" "53" "-04" "N" 

R的string processing能力在過去幾年裏進步很多。