Indexing matrix in R

R-user newbie here. Therefore, I have a dataset created as:

Date Temp Month 1-Jan-90 10.56 1 2-Jan-90 11.11 1 3-Jan-90 10.56 1 4-Jan-90 -1.67 1 5-Jan-90 0.56 1 6-Jan-90 10.56 1 7-Jan-90 12.78 1 8-Jan-90 -1.11 1 9-Jan-90 4.44 1 10-Jan-90 10.00 1 

In the R syntax:

 datacl <- structure(list(Date = structure(1:10, .Label = c("1990/01/01", "1990/01/02", "1990/01/03", "1990/01/04", "1990/01/05", "1990/01/06", "1990/01/07", "1990/01/08", "1990/01/09", "1990/01/10"), class = "factor"), Temp = c(10.56, 11.11, 10.56, -1.67, 0.56, 10.56, 12.78, -1.11, 4.44, 10), Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("Date", "Temp", "Month"), class = "data.frame", row.names = c(NA, -10L)) 

I would like to multiply the data for a specific month and apply the rate of change to temp, and then save the results. so i have something like

 idx <- subset(datacl, Month == 1) # Index results[idx[,2],1] = idx[,2]+change # change applied to only index values 

but i keep getting error like

 Error in results[idx[, 2], 1] = idx[, 2] + change: only 0 may be mixed with negative subscripts 

Any help would be appreciated.

+6
source share
3 answers

First give the coefficient of change a value:

 change <- 1 

Now, here's how to create an index:

 # one approach to subsetting is to create a logical vector: jan.idx <- datacl$Month == 1 # alternatively the which function returns numeric indices: jan.idx2 <- which(datacl$Month == 1) 

If you want only a subset of data from January,

 jandata <- datacl[jan.idx,] transformed.jandata <- transform(jandata, Temp = Temp + change) 

To save the entire data frame, but add only the rate of change in Jan temps:

 datacl$Temp[jan.idx] <- datacl$Temp[jan.idx] + change 
+2
source

First, note that subset does not create an index; it creates a subset of the original frame containing all rows with Month == 1 .

Then, when you do idx[,2] , you select the Temp column.

 results[idx[,2],1] = idx[,2] + change 

But then you use them as an index in results , i.e. use them as line numbers. Line numbers cannot be like 10.56 or -1.11 , therefore, your mistake. In addition, you select the first results column, which is Date , and tries to add temperatures to it.

There are several ways to do this.

You can create a logical index TRUE for a row with Month == 1 and FALSE otherwise:

 idx <- datac1$Month == 1 

Then you can use this index to select the rows in datac1 that you want to change (this is exactly what I tried to do initially):

 datac1$Temp[idx] <- datac1$Temp[idx] + change # or 'results' instead of 'datac1'? 

Note that datac1$Temp[idx] selects the Temp column in the datac1 and idx columns.

You can also do

 datac1[idx,'Temp'] 

or

 datac1[idx,2] # as Temp is the second column. 

If you want results be a subset where Month == 1 , try:

 results <- subset(datac1, Month == 1) results$Temp <- results$Temp + change 

This is because the results contain only the lines you are interested in, so there is no need to make a subset.

+1
source

Personally, I would use ifelse() and use syntactic beauty, which within() for a nice liner datacl <- within(datacl, Temp <- ifelse(Month == 1, Temp + change,Temp)) . Well, I said one liner, but you will need to define change elsewhere.

+1
source

Source: https://habr.com/ru/post/916311/


All Articles