【发布时间】:2016-06-23 02:13:37
【问题描述】:
我想从一个文件创建一个数据框。首先,我从互联网将ftp 文件下载到我的电脑中。文件可在此链接中找到:data from ftp。我想在一个列上创建一个带有日期的数据框(取自每个文件的Launch date 行),并在其他列上创建一个数据。
为了抓取所有文件,我使用了以下代码:
`setwd("C:/Users/")
path = "~C:/Users/"
files <- lapply(list.files(pattern = '\\.l100'), readLines)
test.sample<-do.call(rbind, lapply(files, function(lines){
# for each file, return a data.frame of the datetime, pulled with regex
data.frame(datetime = lubridate::dmy(sub('^.*Launch Date* : ', '', lines[6])),
# and the data, read in as text
read.table(text = lines[13:length(lines)]))
}))`
我想知道上面的代码有什么问题。如果您可以编写新代码也很棒。提前谢谢你。
数据如下:
National Oceanic and Atmospheric Administration, U.S. Department of Commerce
Station : Pago Pago, American Samoa
Station Height : 5 meters
Latitude : -14.33
Longitude : -170.71
Flight Number : ASA016
Launch Date : 18 July 1986
Launch Time : 02:31:00 GMT
Radiosonde Type:
Radiosonde Num :
O3 Sonde ID : 4A1834 Background: 0.050 microamps (0.20 mPa) Flowrate: 29.90 sec/100ml RH Corr: 2.00 %
Sonde Total O3 : 268 (51) DU Sonde Total O3 (SBUV): 251 (35) DU
Level Press Alt Pottp Temp FtempV Hum Ozone Ozone Ozone Ptemp O3 # DN O3 Res
Num hPa km K C C % mPa ppmv atmcm C 10^11/cc DU
0 1007.7 0.005 304.0 31.5 26.0 73 1.63 0.016 0.0000 38.4 3.883 268
1 9999.9 0.100 9999.9 999.9 999.9 999 99.90 99.999 99.9990 999.9 999.999 9999
2 982.0 0.200 301.2 26.5 21.4 74 1.69 0.017 0.0003 38.4 4.096 267
3 9999.9 0.300 9999.9 999.9 999.9 999 99.90 99.999 99.9990 999.9 999.999 9999
4 960.0 0.400 301.2 24.6 20.6 79 1.64 0.017 0.0006 38.4 3.983 267
5 9999.9 0.500 9999.9 999.9 999.9 999 99.90 99.999 99.9990 999.9 999.999 9999
6 946.0 0.600 301.1 23.2 20.2 83 1.65 0.017 0.0008 38.4 4.021 267
7 932.0 0.700 300.9 21.8 19.9 89 1.65 0.018 0.0010 38.5 4.040 267
8 920.9 0.800 301.1 21.0 16.4 75 1.65 0.018 0.0012 38.5 4.051 266
9 9999.9 0.900 9999.9 999.9 999.9 999 99.90 99.999 99.9990 999.9 999.999 9999
10 9999.9 1.000 9999.9 999.9 999.9 999 99.90 99.999 99.9990 999.9 999.999 9999
11 893.0 1.100 302.0 19.2 12.1 64 1.58 0.018 0.0016 38.5 3.923 266
12 9999.9 1.200 9999.9 999.9 999.9 999 99.90 99.999 99.9990 999.9 999.999 9999
我有很多这样的文件。再次感谢。
【问题讨论】:
-
您的代码几乎没有问题,但我不确定是因为复制/粘贴错误还是代码本身存在问题。例如,您的第一个带有
read.table的代码永远不会完成。然而,你得到的实际错误是什么?你能提供一个文件包含的小样本吗? -
@r2evans 我做了第一个作为一个简单的试验。但是你可以在问题中提供的链接中找到文件(files)的内容。
-
@alistaire 你怎么看?感谢您上次提供的精彩代码。
-
问题解决了吗?你得到什么错误?
标签: r dataframe ftp read.table data-extraction