R language - Get data at the specified location
In R, data object + [ , ] is used to obtain data at the corresponding position. According to the different types of index parameters, they can be divided into:
Positive integer, negative integer, zero, space, logical value, name
> matrix [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20
1. Positive integer index
Because the starting position in R is 1, which is different from the general programming language, this type of index is the most common.
It should be noted that if there are duplicate values in the index, R will continue to execute and then repeatedly extract some values, such as:
> matrix[c(1,1),1:5] [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 1 5 9 13 17
2. Negative integer index
Using a negative integer index is exactly the opposite of a positive integer. R will return elements that do not contain the corresponding position of a negative integer. When there are many rows or columns that need to be selected, using this index will be faster.
It should be noted that positive integer indexes and negative integer indexes can appear at different index positions at the same time, but they cannot appear at the same position at the same time. Otherwise, an error will be reported, as follows:
> matrix[c(-1,-1),1] [1] 2 3 4 > matrix[c(-1,1),1] Error in matrix[c(-1, 1), 1] : only 0's may be mixed with negative subscripts
3. Zero index
To be honest, the existence of zero index does not make any sense. It is very likely that designers are just a protective measure that often leads to errors in order to prevent many programmers from being unable to avoid counting from 0. Uses are as follows:
> matrix[1,0] integer(0) > matrix[0,0] <0 x 0 matrix>
4. Space index
Spaces indicate that all dimensions corresponding to the index position are extracted, but the effect is the same without writing spaces, as follows:
> matrix[1,] [1] 1 5 9 13 17 > matrix[1, ] [1] 1 5 9 13 17
5. Logical value index
When the provided index position is a vector containing the logical values of TRUE and FALSE, R will match the column with the index value of TRUE and take out the corresponding element.
Using this method, the vector length needs to be the same as the dimension of the index position, otherwise the desired effect will not be achieved. The specific details are as follows:
> matrix[c(T,F,F,F),] [1] 1 5 9 13 17
This method seems bulky, but it is very effective in special circumstances
6. Name index
When the indexed object has a name attribute, the name can be used as the index to extract relevant elements. This is a common method for extracting columns, because the column always has a name, as follows:
> colnames(matrix)<-c("a","b","c","d","e") > matrix[,"b"] [1] 5 6 7 8
Supplement: R language obtains data for a specific time period
How to get tabular data for a specific time period:
1. Method 1: Regular Expression
data <- ('F:/') date <- (data$Date) #Get the data from column 3-5 between 2018-3-1 and 2018-3-10subT <- date[(format(date,format = "%Y")=="2018"&(format(date,format = "%m"))=="3" &(format(date,format = "%d"))>=1&(format(date,format = "%d"))<=10),3:5] #Average the data in column 3-5 between 2018-3-1 and 2018-3-10mean <- apply(subT,2, mean)
2. Method 2:
date1 <- (paste("2018-3-1","08:00:00")) date2 <- (paste("2018-3-1","17:00:00")) int <- interval(date1, date2) #Get the data from column 3-5 between 2018-3-1 08:00:00 to 2018-3-1 17:00:00subT <- data[ymd_hms(data$Date) %within% int,3:5]
Notice:
1. within determines whether the data is within this time period, including the start time and not the end time.
2. paste0(num_year[y],'_',m,'_1') implements the merge of strings, where m and num_year[y] are variables.
There are spaces when paste splicing, and there are no spaces when paste0
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.