如何计算读取的宽度.如果不同的列在r中有不同的宽度



试图将文件加载到r(跳过前4行)http://www.cpc.ncep.noaa.gov/data/indices/wksst8110.for

这是一个固定宽度的文件,我不知道如何从文件中计算宽度。

有人能告诉如何加载一个固定的宽度文件到R?

在控制台上创建标尺:

cat(">",paste0(rep(c(1:9,"+"),6),collapse=""))

粘贴第一行,然后计数:

> cat(">",paste0(rep(c(1:9,"+"),6),collapse=""))
> 123456789+123456789+123456789+123456789+123456789+123456789+
> 03JAN1990     23.4-0.4     25.1-0.3     26.6 0.0     28.6 0.3
Error: unexpected symbol in "03JAN1990"

如果您查看文件,您会发现唯一缺少空格的地方是带有负号的列。因此,另一种方法是将"-"的所有实例替换为"-",即在需要的地方创建空白,然后使用read.table:

读取
dat <- read.table(text= gsub("\-", " -", 
                             readLines(url("http://www.cpc.ncep.noaa.gov/data/indices/wksst8110.for"))),
                  skip=4)
> str(dat)
'data.frame':   1284 obs. of  9 variables:
 $ V1: Factor w/ 1284 levels "01APR1992","01APR1998",..: 98 394 689 984 1266 265 560 855 1150 279 ...
 $ V2: num  23.4 23.4 24.2 24.4 25.1 25.8 25.9 26.1 26.1 26.7 ...
 $ V3: num  -0.4 -0.8 -0.3 -0.5 -0.2 0.2 -0.1 -0.1 -0.2 0.3 ...
 $ V4: num  25.1 25.2 25.3 25.5 25.8 26.1 26.4 26.7 26.7 26.7 ...
 $ V5: num  -0.3 -0.3 -0.3 -0.4 -0.2 -0.1 0 0.2 -0.1 -0.2 ...
 $ V6: num  26.6 26.6 26.5 26.5 26.7 26.8 26.9 27.1 27.2 27.3 ...
 $ V7: num  0 0.1 -0.1 -0.1 0.1 0.1 0.2 0.3 0.3 0.2 ...
 $ V8: num  28.6 28.6 28.6 28.4 28.4 28.4 28.5 28.9 29 28.9 ...
 $ V9: num  0.3 0.3 0.3 0.2 0.2 0.3 0.4 0.8 0.8 0.7 ...

你甚至可以只跳过前三行,得到标题:

> dat <- read.table(text= gsub("\-", " -", readLines(url("http://www.cpc.ncep.noaa.gov/data/indices/wksst8110.for"))),
                   header=TRUE, skip=3)
> str(dat)
'data.frame':   1284 obs. of  9 variables:
 $ Week  : Factor w/ 1284 levels "01APR1992","01APR1998",..: 98 394 689 984 1266 265 560 855 1150 279 ...
 $ SST   : num  23.4 23.4 24.2 24.4 25.1 25.8 25.9 26.1 26.1 26.7 ...
 $ SSTA  : num  -0.4 -0.8 -0.3 -0.5 -0.2 0.2 -0.1 -0.1 -0.2 0.3 ...
 $ SST.1 : num  25.1 25.2 25.3 25.5 25.8 26.1 26.4 26.7 26.7 26.7 ...
 $ SSTA.1: num  -0.3 -0.3 -0.3 -0.4 -0.2 -0.1 0 0.2 -0.1 -0.2 ...
 $ SST.2 : num  26.6 26.6 26.5 26.5 26.7 26.8 26.9 27.1 27.2 27.3 ...
 $ SSTA.2: num  0 0.1 -0.1 -0.1 0.1 0.1 0.2 0.3 0.3 0.2 ...
 $ SST.3 : num  28.6 28.6 28.6 28.4 28.4 28.4 28.5 28.9 29 28.9 ...
 $ SSTA.3: num  0.3 0.3 0.3 0.2 0.2 0.3 0.4 0.8 0.8 0.7 ...

我完全是R的新手,所以不要太苛刻。我在做这个测试时也卡住了,于是我搜索了所有我能找到的东西。尽管如此,我还是找不到一个可以完全以编程方式计算这个参数的函数(例如,我怎么知道上面的注释中有减号应该处理它们?)。我写了一个简单的函数。我认为文件中的每个新列都以一个符号开始,如果某些标题中的符号数量小于相应列的宽度,则在标题末尾添加空白空间。我不否认它是有效的,也许有些笨拙,但对于我的任务来说,它很有帮助。不管怎样,欢迎你来看看我的"宽度"。如果你觉得合适,就使用它,纠正它,等等。//示例url: https://d396qusza40orc.cloudfront.net/getdata%2Fwksst8110.for或者(相同)http://www.cpc.ncep.noaa.gov/data/indices/wksst8110.for//

myurl <- "url"
l <- readLines(myurl)
head(l) ## looking for headers line number
myh <- NUMBER ## WRITE your headers line NUMBER (in my ex. myh <- 4)
widths.fwf <- function(url = myurl, h = myh) ## h: headers line number
    {
    x <- readLines(url, n = h)
    y <- strsplit(x[[h]], "") ## headers line, splitted into characters
    v <- as.vector(y[[1]]) ## vector of headers line characters 
    b <- ifelse(v[[1]] == " ", 0,1) ##binary var: empty (0) and filled (1) places in headers line
    p <- numeric() ## vector to find the places of every header start
    for (i in 2:length(b)) if (b[i] == 0 & b[i+1] == 1) p[i] <- i else p[i] <- 0
    pp <- which(p !=0) ## only places of every header start
    ppp <- numeric() ## to be vector of "widths"
    ppp[1] <- pp[1]
    for(i in 2:length(pp)) ppp[i] <- pp[i] - pp[i-1]
    ppp[length(pp)+1] <- length(p) - pp[length(pp)]
    return(ppp)}
library(foreign)
myppp <- widths.fwf()
t <- read.fwf(myurl, widths = myppp, skip = myh) ## our table ".for"
head(t)

您可以使用dyplr::read_fwf

根据要解析的向量的字段固定宽度

nao <- read_fwf("https://www.cpc.ncep.noaa.gov/data/indices/wksst8110.for",
                fwf_widths(c(15, 4, 9, 4, 9, 4, 9, 4,4), 
                col_names = c("week",
                              "Nino1+2_sst",
                              "Nino1+2_ssta",
                              "Nino3_sst",
                              "Nino3_ssta",
                              "Nino34_sst",
                              "Nino34_ssta",
                              "Nino4_sst",
                              "Nino4_ssta")),
                skip =4)

相关内容

最新更新