Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文本文件导入具有非新列空格的R_R_Read.table - Fatal编程技术网

将文本文件导入具有非新列空格的R

将文本文件导入具有非新列空格的R,r,read.table,R,Read.table,我有一个来自NOAA的文本文件,其中有一个气象站列表,应该有8列,没有标题。它们都用空格隔开,但在下面的例子中,有些名字像“ABBEVILLE”,有些名字像“ANDALUSIA 3 W”。当我用read.table将其读入R时,我得到了10列 USC00008 31.5702-85.2482 139.0阿尔阿贝维尔15-6 USC00010252 31.3071-86.5226 76.2安达卢西亚3 W 15-6 precip_stations <- read.table("hpd-sta

我有一个来自NOAA的文本文件,其中有一个气象站列表,应该有8列,没有标题。它们都用空格隔开,但在下面的例子中,有些名字像“ABBEVILLE”,有些名字像“ANDALUSIA 3 W”。当我用read.table将其读入R时,我得到了10列

USC00008 31.5702-85.2482 139.0阿尔阿贝维尔15-6

USC00010252 31.3071-86.5226 76.2安达卢西亚3 W 15-6

precip_stations <- read.table("hpd-stations.txt", sep = "", header = FALSE, fill = T) 

> V1           V2       V3     V4  V5     V6    V7  V8 V9  V10
>USC00010008 31.5702 -85.2482 139  AL ABBEVILLE 15  -6
>USC00010252 31.3071 -86.5226 76.2 AL ANDALUSIA  3  W  15  -6
precip_站V1 V2 V4 V6 V7 V8 V9 V10
>USC00008 31.5702-85.2482 139 AL-ABBEVILLE 15-6
>USC00010252 31.3071-86.5226 76.2安达卢西亚3 W 15-6
有没有办法让它在一列中导入“安达卢西亚3W”之类的内容?我认为fill=FALSE可能有效,但如果我这样做,我会得到一个错误

丑陋,但有效:

x <- readLines("hpd-stations.txt")

l <- list()

for (i in 1:length(x)) {
  a <- strsplit(x[i], " ")
  b <- t(sapply(a, "[", 1:5))
  c <- length(a[[1]]) - 5 - 2
  d <- t(sapply(a, "[", 6:(5+c)))
  e <- t(sapply(a, "[", (5+c+1):length(a[[1]])))
  res <- as.data.frame(cbind(b, paste0(d, collapse = " "), e))
  l[[i]] <- res
}

data <- dplyr::bind_rows(l)
使用虚拟数据进行测试


编辑:刚刚看到一个指向该文件的链接出现。还没有测试过,但是现在你应该考虑使用<代码> Read。FWF()/<代码>。< /P> < P>读取固定宽度的格式文件需要首先认识到没有有效的分隔符,然后使用'Read .FWF。这反过来要求您确定间距以计算宽度:

inp <- "
USC00010008  31.5702  -85.2482  139.0 AL ABBEVILLE                                                                                 15    -6
USC00010063  34.2553  -87.1814  249.3 AL ADDISON                                                                                   15    -6"
> cat( paste0( rep( 0:9, 13), collapse=""))
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
事实上,我刚刚意识到我应该做一个函数来创建一个可调标尺:

ruler <- function(len=7){ cat( paste0( rep( c(1:9,0), len), collapse=""))
                          cat("\n")
                          cat(paste0( sprintf( "% 10s", 1:len), collapse="")) }
> ruler(13)  # could not get a proper display of the output in SO
所以需要
comment.char=”“

>mytbl str(mytbl)
“数据帧”:1920 obs。共有7个变量:
$V1:系数w/1920等级“CQC0914080”,…:23242526272829319332。。。
$V2:num 31.6 34.3 32.2 31.3 33.3。。。
$V3:num-85.2-87.2-87.4-86.5-85.8。。。
$V4:num 139 249.3 53.3 76.2 311.5。。。
$V5:系数w/1920级“AK中央2号”,8 9 10 11 12 13 14 16 17。。。
$V6:系数w/9级“1”、“0 1”、“1”。。。
$V7:系数w/2级“5”,“5-”:1。。。

使用
readLines
读入,并使用regex插入引号进行预处理。然后传递到
read.table
。然而,我猜在开始使用它们之前,这些文件实际上是固定宽度的格式化文件,这样保存它们和使用
read.fwf
也可能是一个制表符分隔的文件。使用
sep=\t
是否会更改导入?\t将其全部放在一列中,并且我没有更改从Noaah下载的文件中的任何内容。有指向该文件的链接,请参见?这是一个FWF文件。请一遍又一遍地阅读我之前的评论,直到它的价值被理解。@Jeffkrop:差不多了。请参阅抑制默认注释字符的需要。做得很好。您可以添加
mytbl$V5
cat( paste0( rep( c(1:9,0), 7), collapse="")); cat("\n"); 
cat(paste0( sprintf( "% 10s", 1:7), collapse=""))
#---------------------------------------
#1234567890123456789012345678901234567890123456789012345678901234567890
#         1         2         3         4         5         6         7
ruler <- function(len=7){ cat( paste0( rep( c(1:9,0), len), collapse=""))
                          cat("\n")
                          cat(paste0( sprintf( "% 10s", 1:len), collapse="")) }
> ruler(13)  # could not get a proper display of the output in SO
> read.fwf(textConnection(inp), widths=c(10, 10,10, 7, 90, 5, 5) )
          V1         V2       V3    V4                                                                                         V5 V6 V7
1       <NA>       <NA>       NA    NA                                                                                       <NA> NA NA
2 USC0001000 8  31.5702 -85.2482 139.0  AL ABBEVILLE                                                                               1  5
3 USC0001006 3  34.2553 -87.1814 249.3  AL ADDISON                                                                                 1  5
 myfwf <- readLines("ftp://ftp.ncdc.noaa.gov/pub/data/hpd/auto/v1/beta/hpd-stations.txt" )
> mytbl <- read.fwf(textConnection(myfwf[1:12]), widths=c(11, 10,10, 7, 89, 5, 5) )
> mytbl
            V1      V2       V3    V4                                                                                        V5 V6 V7
1  USC00010008 31.5702 -85.2482 139.0 AL ABBEVILLE                                                                               1  5
2  USC00010063 34.2553 -87.1814 249.3 AL ADDISON                                                                                 1  5
3  USC00010140 32.2322 -87.4104  53.3 AL ALBERTA                                                                                 1  5
4  USC00010252 31.3071 -86.5226  76.2 AL ANDALUSIA 3 W                                                                           1  5
5  USC00010369 33.2941 -85.7788 311.5 AL ASHLAND 3 ENE                                                                           1  5
6  USC00010390 34.7752 -86.9508 210.0 AL ATHENS                                                                                  1  5
7  USC00010402 31.1820 -87.4390  91.4 AL ATMORE                                                                                  1  5
8  USC00010425 32.5992 -85.4653 166.1 AL AUBURN NO.2                                                                             1  5
9  USC00010748 33.6972 -87.6491 157.9 AL BERRY 3 NW                                                                              1  5
10 USC00010957 34.2008 -86.1633 326.1 AL BOAZ                                                                                    1  5
11 USC00011099 34.9786 -85.8008 204.2 AL BRIDGEPORT 5 NW                                                                         1  5
12 USC00012124 32.8622 -85.7358 223.4 AL DADEVILLE 2                                                                             1  5
USC00012172  30.2505  -88.0775    2.4 AL DAUPHIN ISLAND #2                                                                         15    -6
> mytbl <- read.fwf(textConnection(myfwf), widths=c(11, 10,10, 7, 89, 5, 5) , comment.char="")
> str(mytbl)
'data.frame':   1920 obs. of  7 variables:
 $ V1: Factor w/ 1920 levels "CQC00914080",..: 23 24 25 26 27 28 29 30 31 32 ...
 $ V2: num  31.6 34.3 32.2 31.3 33.3 ...
 $ V3: num  -85.2 -87.2 -87.4 -86.5 -85.8 ...
 $ V4: num  139 249.3 53.3 76.2 311.5 ...
 $ V5: Factor w/ 1920 levels "AK CENTRAL NO 2                                                                          ",..: 8 9 10 11 12 13 14 15 16 17 ...
 $ V6: Factor w/ 9 levels "    1","0   1",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ V7: Factor w/ 2 levels "5    ","5   -": 1 1 1 1 1 1 1 1 1 1 ...