尝试将文件加载到r(跳过前4行) http://www.cpc.ncep.noaa.gov/data/indices/wksst8110.for
这是固定宽度的文件,我不知道如何从文件中计算宽度。
有人能告诉我如何将一个固定宽度的文件加载到R中吗?
发布于 2014-08-17 20:09:52
在控制台上创建一个标尺:
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 ...发布于 2020-08-14 17:16:08
我在R区是个新手,所以别太苛刻。我也被困在做这个测试上了,并且寻找我所能找到的一切。尽管如此,我仍然找不到一个函数可以完全以编程的方式计算这个参数(例如,在上面的注释中,我怎么知道有一些小值应该处理它们?)所以我给自己写了一个简单的函数。我认为文件中的每个新列都以符号开头,如果某些标题中的符号数小于相应列的宽度,则在标题末尾添加空空间。我并不否认它起作用,也许是尴尬的,但对于我的任务,它是有帮助的。无论如何,我们欢迎你看看我的"widths.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)发布于 2021-07-11 09:39:24
您可以使用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)https://stackoverflow.com/questions/25352632
复制相似问题