我使用下面的代碼按年獲得累積降雨量,並將它們繪製爲每年的行數,但是當我從plyr
軟件包運行ddply
函數時,出現以下錯誤。在R中使用plyr獲取累積降雨時出錯
錯誤:「名稱」屬性[9]必須是相同的長度作爲矢量[3]
library(plyr)
# Setting work directory
setwd("d:\\ClimData")
# Reading and reformatting raw data downloaded from NCDC
dat<-read.table("CDO2812586929956.txt",header=F,skip=1)
colnames(dat)<-c("stn","wban","yearmoda","temp","tempc","dewp","dewpc","slp","slpc","stp","stpc","visib","visibc","wdsp","wdspc","mxspd","gust","maxtemp","mintemp","prcp","sndp","frshtt")
dat$yearmoda <- strptime(dat$yearmoda,format="%Y%m%d")
dat$prcp <- as.character(dat$prcp)
dat$prcp1 <-as.numeric(substr(dat$prcp,1,4))
dat$prcpflag <- substr(dat$prcp,5,5)
dat$rain <- dat$prcp1*25.4
dat$rain[dat$rain > 1000 ] <- NA
dat$year <- as.numeric(format(dat$yearmoda,"%Y"))
dat$month <- as.numeric(format(dat$yearmoda,"%m"))
dat$day <- as.numeric(format(dat$yearmoda,"%d"))
# Getting cumulative sum of rain/year
dat <- ddply (dat,.(year), transform, cumRainfall = cumsum (rain))
厚福有人能指出在哪裏出現錯誤。
輸入文件位於下面的鏈接。
https://dl.dropboxusercontent.com/u/81632971/CDO2812586929956.txt
在將列名分配給列之前使用dim(dat),以驗證您具有與分配列相同數量的列。 – Chitrasen
我沒有問題分配名稱的列。只有在代碼末尾執行'plyr'功能才能獲得累計降雨總和時,纔會出現錯誤。 –
當轉換爲'as.numeric(dat $ prcp)'時,原來的'dat $ prcp'實際數值是否一路貫穿? –