Hi Martin, Yeah, you can't replace values like that... here is a way to think about it. Consider:
stfdf[,1] This returns a *completely new* spacetime object. You cannot set anything in this object because it has not been assigned to any variable. So, if you select stfdf[ , 1]@data$values You are saying: create a completely new spacetime object and then select the 'data' data frame, and select the 'values' column. Now, if you try to say: stfdf[,1]@data$values = stfdf@data[,1] + 1 Then you are saying: create a *completely new* spacetime object, select a column on its 'data' dataframe and then try to *assign* some value to this completely new object. But you haven't assigned this new object to any variable! So how can you assign a value in it? It would be like saying c(1,2,3) <- c(4,5,6) You could say: x=c(1,2,3) x=c(4,5,6) In the same way, you can't assign a value to a variable that hasn't already been 'set' somewhere. (It is a little more complicated than that, but it is a good way to think about it). So, if you wanted, you could do x=stfdf[,1]@data$values x=stfdf[,1]@data$values +1 But that isn't what you want. Your problem is that you want to change a subset of the *data frame that exists within stfdf*. The only way to assign to that data frame is to index to it directly. stfdf@data[*some row*,*some column*] = whatever. But the problem is that you don't know which rows and columns correspond to the time and space subset you want, right? So you find those rows with some comparisons. So, first try just taking the subset you want stfdf[,'2010-08-05 11:00:00::2010-08-05 12:00:00'] Now, you'll notice that it only retained the data for the time that you subsetted. You can use the column ID to set the values in the original! So, subset.ids = stfdf[,'2010-08-05 11:00:00::2010-08-05 12:00:00']@data$ID # That will get you a list of the IDS of the rows you want to change. stfdf@data$ID %in% subset.ids # That is a list of true and false, TRUE where your data frame row is in the subset, and FALSE when it is not You can use this vectors of logicals to change the values you want, and not the others. Like say you wanted to increase the value of just the subset by one: subset= stfdf@data$ID %in% subset.ids stfdf@data[subset,'value'] = stfdf@data[subset,'value'] + 1 Now, you could argue that doesn't stfdf@data[subset,'value'] return a *completely new* object, that cannot be assigned to? The answer is no. It appears that for simple objects, like data.frames, you can assign things in this way, but for complex objects, like spacetime objects, you can't. I don't know exactly why... my intuition is that it would be very complicated to allow this behaviour. I think this is what the error is trying to tell you: spacetime is an 'S4' object, which is not subsettable like a data frame is. aman -----Original Message----- From: Roth, M. [mailto:m.r...@tue.nl] Sent: March 11, 2011 4:14 PM To: Aman Verma Subject: RE: replace values in spacetime (STFDF) object Dear Aman, sorry, I thought the example in the first mail would explain what I want to do. But yes, I can subset my stfdf object by this: stfdf[ , 1]@data$values (thats all points for the first time) So I just get three values - but if I try to replace this values by other three values I get the error I mentioned below. My problem is also not subsetting the stfdf object in the right manner but replace the data with other data. Do you know why this is different from selecting values? Cheers, Martin ________________________________________ From: Aman Verma [aman.ve...@mcgill.ca] Sent: Friday, March 11, 2011 9:54 PM To: Roth, M.; r-sig-geo@r-project.org Subject: RE: replace values in spacetime (STFDF) object Hi Martin, It would be very helpful if you provided a clear example of exactly what you wanted to extract from the object, but I'll make a guess. As the vignette (of the class STFDF) mentions, you can select a specific time range and spatial points very easily by the normal index mechanism: the first index is the time, and the second is the space. Continuing the example below, if you run stfdf It will give you three spatial points, four times, and 12 data points, one for each space time combination. Now, if you wanted to subset this object to select the just the first two points, and the first two times, then you could simply say: stfdf[1:2,1:2] And you'll see that there are exactly two points, two times, and four data points. Now, you might say, I don't want to select the "first" or the "second" time, but I want to select by a certain time itself, like after 11:30 on 2010-08-05. Take a look at the class of the stfdf@time object class(stfdf@time) You can see that it is a xts object. Check out the help file for this class: ?xts At the bottom, it gives you some great examples of how to subset these kind of time objects. So, now, you can try using these indices instead of the "number" of the time in your set. stfdf[,'2010-08-05 11:00:00::2010-08-05 12:00:00'] So that is how to select everything from 11:00 to 12:00 on that day. Now, you have to figure out how to subset the space object. You should be able to get the idea from above though: read the help files and google how to subset that specific object. You'll then be able to subset by index. Or, you could find all the points within a certain window (created by a SpatialPolygon) and then use the vector of logical as you index. aman -----Original Message----- From: Roth, M. [mailto:m.r...@tue.nl] Sent: March 11, 2011 3:17 PM To: Aman Verma; r-sig-geo@r-project.org Subject: RE: replace values in spacetime (STFDF) object Hi, thank you very much. But actually that is not exactly what I want to do. I want to select a specific time range and spatial points and manipulate the corresponding data. When I subset the dataframe I must do the selection manually and that is not that nice. Any idea how I could do this? Cheers, Martin ________________________________________ From: Aman Verma [aman.ve...@mcgill.ca] Sent: Friday, March 11, 2011 7:16 PM To: Roth, M.; r-sig-geo@r-project.org Subject: RE: replace values in spacetime (STFDF) object Hi Martin, You put the brackets in the wrong place. You want to subset the dataframe, right, not the stfdf object, so: stfdf@data$values[1] <- stfdf@data$values[1] + 1 would increase the "value" in the first row of the stfdf@data data frame. stfdf@data[,1] would get you the first column of the data frame. If you wanted to increase the whole column by one: stfdf@data[,1] = stfdf@data[,1] + 1 aman -----Original Message----- From: r-sig-geo-boun...@r-project.org [mailto:r-sig-geo-boun...@r-project.org] On Behalf Of Roth, M. Sent: March 11, 2011 10:00 AM To: r-sig-geo@r-project.org Subject: [R-sig-Geo] replace values in spacetime (STFDF) object Hello, I want to replace specific values in a STFDF object from the spacetime package. As example consider the following stfdf object out of the vignette. sp = cbind(x = c(0,0,1), y = c(0,1,1)) row.names(sp) = paste("point", 1:nrow(sp), sep="") sp = SpatialPoints(sp) time = xts(1:4, as.POSIXct("2010-08-05", tz = "GMT")+3600*(10:13)) m = c(10,20,30) # means for each of the 3 point locations mydata = rnorm(length(sp)*length(time),mean=rep(m, 4)) IDs = paste("ID",1:length(mydata), sep = "_") mydata = data.frame(values = signif(mydata,3), ID=IDs) stfdf = STFDF(sp, time, mydata) Now I want to do something like this: stfdf[ , 1]@data$values <- stfdf[ , 1]@data$values + 1 I get the following error: Error in stfdf[, 1]@data$values <- stfdf[, 1]@data$values + 1 : object of type 'S4' is not subsettable Does anybody know how to do that in a correct way? Thanks a lot, Martin Roth _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo