-1

我有一个运行良好的脚本,除了在我的 R cbind 操作中,与我在第一行中需要的数值相邻的是“X”。

这是我的脚本:

library(ncdf)
library(Kendall)
library(forecast)
library(zoo)
setwd("/home/cohara/RainfallData")

files=list.files(pattern="*.nc")

j=81
for (i in seq(1,9))
{
        file<-open.ncdf(sprintf("/home/cohara/RainfallData/%s.nc",i))
        year<-get.var.ncdf(file,"time")
        data<-get.var.ncdf(file,"var61")
        fit<-lm(data~year)              #least sqaures regression
        mean=rollmean(data,4,fill=NA)
        kendall<-Kendall(data,year)
        write.table(kendall[[2]],file="/home/cohara/RainfallAnalysis/Kendall_p-value_for_10%_increase_over_81_-_89_years.csv",append=TRUE,quote=FALSE,row.names=FALSE,col.names=FALSE)
        write.table(kendall[[1]],file="/home/cohara/RainfallAnalysis/Kendall_tau_for_10%_increase_over_81_-_89_years.csv",append=TRUE,quote=FALSE,row.names=FALSE,col.names=FALSE)
        png(sprintf("./10 percent increase over %s years.png",j))
        par(family="serif",mar=c(4,6,4,1),oma=c(1,1,1,1))
        plot(year,data,pch="*",col=4,ylab="Precipitation (mm)",main=(sprintf("10 percent increase over %s years",j)),cex.lab=1.5,cex.main=2,ylim=c(800,1400),abline(fit,col="red",lty=1.5))
        par(new=T)
        plot(year,mean,type="l",xlab="year",ylab="Precipitation (mm)",cex.lab=1.5,ylim=c(800,1400),lty=1.5)
        legend("bottomright",legend=c("Kendall tau = ",kendall[[1]]))
        legend("bottomleft",legend=c("Kendall 2-tailed p-value = ",kendall[[2]]))
        legend(x="topright",c("4 year moving average","Simple linear trend"),lty=1.5,col=c("black","red"),cex=1.2)
        legend("topleft",c("Annual total"),pch="*",col="blue",cex=1.2)
        dev.off()
        j=j+1
}
tmp<-read.csv("/home/cohara/RainfallAnalysis/Kendall_p-value_for_10%_increase_over_81_to_89_years.csv")
tmp2<-read.csv("/home/cohara/RainfallAnalysis/Kendall_p-value_for_10%_increase_over_81_-_89_years.csv")
tmp<-cbind(tmp,tmp2)
tmp3<-read.csv("/home/cohara/RainfallAnalysis/Kendall_tau_for_10%_increase_over_81_to_89_years.csv")
tmp4<-read.csv("/home/cohara/RainfallAnalysis/Kendall_tau_for_10%_increase_over_81_-_89_years.csv")
tmp3<-cbind(tmp3,tmp4)
write.table(tmp,"/home/cohara/RainfallAnalysis/Kendall_p-value_for_10%_increase_over_81_to_89_years.csv",sep="\t",row.names=FALSE)
write.table(tmp3,"/home/cohara/RainfallAnalysis/Kendall_tau_for_10%_increase_over_81_to_89_years.csv",sep="\t",row.names=FALSE)

输出如下所示,来自创建的 .csv 文件:

X0.0190228056162596 X0.000701081415172666
0.0395622998    0.00531819
0.0126547674    0.0108218994
0.0077754743    0.0015568719
0.0001407317    0.002680057
0.0096391216    0.012719159
0.0107234037    0.0092436085
0.0503448173    0.0103918528
0.0167525802    0.0025036721

我希望能够在数据上使用 excel 函数,所以,为简单起见,我不想要行名(我可能会运行这个循环一百次),但我需要列名,否则第一组值被切断。

谁能告诉我“X”来自哪里以及如何摆脱它?

在此先感谢,席亚拉

4

1 回答 1

2

这是我认为正在发生的事情。首先运行这些小示例:

df1 <- read.csv(text = "0.0190228056162596, 0.000701081415172666
0.0395622998,    0.00531819
0.0126547674,    0.0108218994")

df2 <- read.csv(text = "0.0190228056162596, 0.000701081415172666
0.0395622998,    0.00531819
0.0126547674,    0.0108218994", header = FALSE)

df1
df2
str(df1)
str(df2)
names(df1)
names(df2)

make.names(c(0.0190228056162596, 0.000701081415172666))

请阅读?read.csv和关于header论点。如您所见,header = TRUEread.csv. 因此,如果您读取的 csv 文件缺少标题,read.csv仍会“假定”该文件具有标题,并将第一行中的值用作标题。中的另一个参数read.csvcheck.names,默认为 TRUE:
If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names. If necessary they are adjusted (by make.names)

在您的情况下,您读取的数据似乎缺少标题,并且第一行只是数字。read.csv将默认将此行视为标题。make.names取第一行中的值(此处为数字 0.0190228056162596、0.000701081415172666),并吐出“语法上有效的变量名”X0.0190228056162596 和 X0.000701081415172666。这不是你想要的。

因此,您需要显式设置header = FALSE以避免read.csv将第一行转换为(有效)变量名。

下次,请提供一个最小的、独立的示例。检查这些链接以获得一般想法,以及如何在 R 中执行此操作:herehereherehere

于 2014-04-16T12:43:02.590 回答