0
我寫了下面的函數。不幸的是,當我運行它,它運行約2秒,然後返回絕對沒有,但是當我運行它使用調試它似乎運行完美。該代碼還會返回正確讀取的文件的頭部,但它不會將任何內容分配給對象,因此在運行該函數後,我可以再次輸入文件頭並返回錯誤。我有點難住,所以任何指針將不勝感激!我的功能不會運行,但通過調試時運行良好
感謝
Extremes <- function (siteno){
# reads in the weather data for the specipied site
Butterfly_data <- read.csv(paste("~/Project Data/Site Subsets/SITE", siteno, ".csv", sep = ""))
Precip <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/rr/Weather.Site.", siteno, ".csv", sep = ""))
Tmin <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/tn/Weather.Site.", siteno, ".csv", sep = ""))
Tmax <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/tx/Weather.Site.", siteno, ".csv", sep = ""))
for (i in 1:length(Precip[,1])){ # turns missing values into NA values for precip, max and min temp
if (Precip[i,1] < -900){
Precip[i,1] <- NA
}}
for (i in 1:length(Tmax[,1])){
if (Tmax[i,1] < -900){
Tmax[i,1] <- NA
}}
for (i in 1:length(Tmin[,1])){
if (Tmin[i,1] < -900){
Tmin[i,1] <- NA
}}
ExtPrecip <- mean(Precip[,1], na.rm = TRUE) + sd(Precip[,1], na.rm = TRUE) # finds the extreme of each weather variable
ExtTmax <- mean(Tmax[,1], na.rm = TRUE) + sd(Tmax[,1], na.rm = TRUE)
ExtTmin <- mean(Tmin[,1], na.rm = TRUE) + sd(Tmin[,1], na.rm = TRUE)
return(c(head(Precip),head(Tmin),head(Tmax)))
}
例如,'Precip'或' ExtPrecip'只存在於你的函數中。在函數之外,只有函數的返回值存在,它需要分配給一個對象,或者它在打印後不見了。另外,你不需要那些for循環。子集是矢量化的,這意味着你可以做一些像'Precip [Precip [,1] <-900,1] < - NA',這更快。 – Roland
非常感謝!愚蠢的疏忽!此外,它不允許我爲您的答案投票! – user2961410