0
我正在处理别人的代码,并重新运行它以将它们做的事情拼凑在一起。这似乎是我正在运行的代码部分是返回一个空的data.table,这是不理想的。下面的代码可以运行。子集化时返回空数据表
url = "http://www.cpc.ncep.noaa.gov/products/analysis_monitoring/ensostuff/ensoyears.shtml"
page <- readLines(url)
ONI_data_raw <- data.table (readHTMLTable(page, which=8))
headers <- c ("Year", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
setnames(ONI_data_raw, headers)
ONI_data_raw = melt(ONI_data_raw, id.vars = "Year")
names(ONI_data_raw)[2] = "Month"
names(ONI_data_raw)[3] = "ONI_Value"
ONI_data_raw$Month = as.numeric(ONI_data_raw$Month)
ONI_data_raw$ONI_Value= as.numeric(ONI_data_raw$ONI_Value)
ONI_data_raw = subset(ONI_data_raw, ONI_data_raw$Year %in% time)
head(ONI_data_raw)
结果我得到的是:
Empty data.table (0 rows) of 3 cols: Year,Month,ONI_Value
理想我想看到的是这样的:
Year Jan Feb March etc.
1950 -1.4 -1.2 -1.1
1951 -0.8 -0.6 -0.2
我认为这是代码的目标,但如果你运行第三行到最后一行(在它是子集之前),输出是有点时髦的
Year Month ONI_Value
1: 1950 1 -1.4
2: 1951 1 -0.8
3: 1952 1 0.5
4: 1953 1 0.5
5: 1954 1 0.7
6: 1955 1 -0.6
7: 1956 1 -0.9
8: 1957 1 -0.3
9: 1958 1 1.7
10: 1959 1 0.6
11: Year 1 NA
12: 1960 1 -0.1
第11行是奇数行。还有更多的行也可以。不知道是否会导致问题,但是一旦倒数第二行发生 - 数据表就会变空。
任何见解都会有所帮助!