Reputation: 1772
I am trying to plot histograms with long term (several years) mean precipitation (pp) for each day of the month from a series of files. Each file has data collected from a different place (and has a different code). Each of my files looks like this:
X code year month day pp
1 2867 1945 1 1 0.0
2 2867 1945 1 2 0.0
...
And I am using the following code:
files <- list.files(pattern=".csv")
par(mfrow=c(4,6))
for (i in 1:24) {
obs <- read.table(files[i],sep=",", header=TRUE)
media.dia <- ddply(obs, .(day), summarise, daily.mean<-mean(pp))
codigo <- unique(obs$code)
hist(daily.mean, main=c("hist per day of month", codigo))
}
I get 24 histograms with 24 different codes in the title, but instead of 24 DIFFERENT histograms from 24 different locations, I get the same histogram 24 times (with 24 different titles). Can anybody tell me why? Thanks!
Upvotes: 1
Views: 1308
Reputation: 179468
There are at least two errors I can see in your code.
ddply
statement.The problem in your ddply
statement is that you are doing an invalid assign (using <-
). Fix this by using =
:
media.dia<- ddply(obs, .(day),summarise, daily.mean = mean(pp))
Then edit your hist
statement:
hist(media.dia$daily.mean,main=c("hist per day of month",codigo))
I suspect the problem is that you are not passing the correct parameter to hist
. The reason that your code actually produces a plot at all, is because in some previous step in your session you must have created a variable called daily.mean
(as Brandon points out in the comment.)
Upvotes: 5
Reputation: 66844
I think the daily.mean
calculated in the ddply
function is assigned in a separate environment, and does not exist in an environment hist
can see.
Try daily.mean<<-mean(pp)
Upvotes: 0