-4

我的数据的列名是 LABELXYZ123_river、LABELXYZ123_lake、LABELXYZ345_river、LABELXYZ345_lake 等......我有一个实际变量 LABELXYZ123、LABELXYZ345 等的列表......我想为每个标签绘制河流和湖泊数据在单独的数字上。我带着那个代码来了,那行不通。我认为变量及其名称的问题。你能建议我解决方案吗?谢谢...

data <- read.table("data.txt",header=T,row.names=1)
lst  <- c("LABELXYZ123","LABELXYZ345")
for(i in lst) {
lbl_river <- paste("data","$",LABELXYZ123,"_river",sep="")
lbl_lake <- paste("data","$",LABELXYZ123,"_lake",sep="")
allt <- c(lbl_lake,lbl_river)
png(filename=i".png")
plot(paste(lbl_river,xaxt='n',xlab="",ylab="i",type="b",col="darkgreen",lwd=3,ylim=c(min(allt),max(allt)))
lines(paste(lbl_lake,type="b",col="darkred",lwd=3)
dev.off()
}

lbl_river 其实是data$LABELXYZ123_river

4

2 回答 2

3

努力挽救一些东西....更多的情节在整理后

data <- read.table("data.txt",header=T,row.names=1)
lst  <- c("LABELXYZ123","LABELXYZ345")
for(label_name in lst) {
  lbl_river <- data[[paste(label_name, "river", sep = "_"]]
  lbl_lake  <- data[[paste(label_name, "lake", sep = "_"]]

}
于 2012-07-19T01:21:19.097 回答
0

使用 mnel 建议...

data <- read.table("data.txt",header=T,row.names=1)
lst  <- c("LABELXYZ123","LABELXZZ123","LABELXYZ345","LABELXYZ567","LABELXYY1234","LABELXXX123")
labs <- row.names(data)
for(i in lst) {
    png(filename=paste(i,".png",sep=""),width = 700, height = 350)
    river  <- data[[paste(i, "_river", sep = "")]]
    lake <- data[[paste(i, "_lake", sep = "")]]
    allt <- c(river,lake)
    plot(river, xaxt='n',xlab="",ylab=i,type="b",col="darkred",lwd=3,ylim=c(min(allt)*1.05,max(allt)*0.95))
    lines(lake,type="b",col="darkgreen",lwd=3)
    axis(1,at=1:16,labels=labs,las=2)
    dev.off()
}
于 2012-07-19T11:47:13.243 回答