-5

This is my data:

enter image description here

ens1 <- read.csv("G:/ABV_DATA/ABV_2/book3.csv", header=TRUE, sep=",", stringsAsFactors = FALSE)
    attach(ens1)        

dates = as.Date(ens1$date, "%m/%d/%y")

#write.csv(dates, file = "myfile.csv")
ens1 = ens1[,2:4] #remove column 1 and kip columns 2 to 4

ens1 = data.frame(date=dates, ens1)

month.year <- function (x) {12*(as.POSIXlt(x)$year-1)+as.POSIXlt(x)$mon+1}
month = month.year(ens1$date) #the one that works is to use the full ens1$date

mean.TREFHT_MAX = tapply(TREFHT_MAX, month, mean)
sd.TREFHT_MAX= tapply(TREFHT_MAX,month,sd)

mean.RH=tapply(RH,month,mean)
sd.RH=tapply(RH, month,sd)
month = month.name #names from jan or dec

for(i in 1:length(mean.TREFHT_MAX)) {month.observed[i]=as.numeric(names(mean.TREFHT_MAX[i]))%%12

} #first get the months as numbers 0-11
month.observed[month.observed==0]<-12   # make the decembers 12 instead of 0
month.names <- months[as.numeric(month.observed)]


stats <- data.frame(mean.TREFHT_MAX,sd.TREFHT_MAX,mean.RH,sd.RH,month=month.names)
print(stats,digits=3)
Stepan Novikov
  • 1,402
  • 12
  • 22
  • 2
    You have to initiate `month.observed <- numeric(length(mean.TREFHT_MAX))` before the loop. – jogo Nov 03 '18 at 21:57
  • i tried your suggestion and this is what i get " Error in months[as.numeric(month.observed)] : object of type 'closure' is not subsettable" – SADIYA BABA TIJJANI Nov 04 '18 at 15:59
  • You presented your data in a nonreproducible way, please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Further more your code is not minimal. SO is not for code revision. – jogo Nov 04 '18 at 16:04
  • Voting to reopen: The error is rather fundamental in R programming and is easily illustrated with `rm(x); x[5] <- 2`, so even though it has no [MCVE] it can be easily answered by any experienced R programmer. – IRTFM Nov 04 '18 at 16:46

1 Answers1

2

Basically you are trying to build an object with the [<- function and it's not designed for that purpose. An object needs to exist before you can make an assignment to a subset of that object. You probably thought that the <- function existed as a single entity, but when it is used alone, it's really a shorthand for the assign-function using its default values for the pos and environment parameters. So this:

x <- 5

... is really getting translated to:

assign("x", 5, pos=-1)

And in the absence of an already defined value for x, it further means there is no way to execute using <- as a shorthand for assign:

 x[2] <- 5

... since "x[2]" is not a valid R name.

However:

See the help page for <-

?`<-`   # need the backticks or quotes

When you are attempting to run your code, it really results in the [<- function being called. The help page calls this "subassignment" and gives you a link to a different help page for "Extract or Replace Parts of an Object". The code above now translates to this after assembling all the object names:

`[<-`(x, 2, 5)

... and then, when x cannot be found, that error message is "thrown".

(I hope this explains why I disagreed about this not being a "programming question". The question arose because of a misunderstanding of the actual functions being used and how R code is parsed. And didn't really need a reproducible example .... because rm(x); x[5] <-2 was enough to illustrate the error.)

Community
  • 1
  • 1
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Do you reflect on the part `for(i in 1:length(mean.TREFHT_MAX)) {month.observed[i]=... }` of the question? – jogo Nov 05 '18 at 07:28
  • I'm not sure what your concern is. Are you worried that `=` rather than `<-` was used, or is it the fact than it _should_ have been done with a vectorized operation and that the loop was completely unnecessary? Both would have valid but would not have answered the question. – IRTFM Nov 05 '18 at 15:38
  • You gave a long and good description what caused the error. But you did not gave a solution to OPs code, i.e. which part of the code to change and how the code could look like. – jogo Nov 05 '18 at 21:22
  • 1
    @jojo: I thought that it was implied that there needed to be an object named `month.observed` in order for there to be a successful indexed assignment. The question was "why". As is my usual practice, I often wait until there is a [MCVE] (not in a picture) before I offer tested code. – IRTFM Nov 06 '18 at 00:20