Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 1123 of 2109
How to standardize only numerical columns in an R data frame if categorical columns also exist?
The standardization of a numerical column can be easily done with the help of scale function but if we want to standardize multiple columns of a data frame if categorical columns also exist then mutate_if function of dplyr package will be used. For example, if we have a data frame df then it can be done as df%>%mutate_if(is.numeric, scale)Example1Consider the below data frame −> x1 x2 df1 df1Output x1 x2 1 c 4 2 c 1 3 a 4 4 a 1 5 b 0 6 c 4 7 c 2 8 ...
Read MoreHow to find the index of n number of maximums in each row of a matrix in R?
If a matrix has multiple columns and values in each row are different then there will be number of maximums equal to the number of columns. Suppose, we want to extract the index of two maximums in each row in a matrix called M then we can use the below command −t(apply(M,1,order,decreasing=TRUE)[1:2,]) Example1> M1 M1Output [,1] [,2] [,3] [,4] [1,] 7 4 4 10 [2,] 7 1 4 3 [3,] 9 6 6 4 [4,] 6 5 3 3 [5,] 3 3 2 4 [6,] 5 6 2 2 [7,] 4 7 3 10 [8,] 6 0 11 6 [9,] 1 6 2 2 [10,] 4 9 4 9 [11,] 4 10 3 6 [12,] 3 9 3 11 [13,] 5 4 7 2 [14,] 6 7 6 6 [15,] 2 5 6 3 [16,] 2 5 8 2 [17,] 0 3 1 6 [18,] 6 10 6 4 [19,] 3 4 5 5 [20,] 4 5 8 4Finding the index of two maximums in each of M1 −> t(apply(M1,1,order,decreasing=TRUE)[1:2,])Output [,1] [,2] [1,] 4 1 [2,] 1 3 [3,] 1 2 [4,] 1 2 [5,] 4 1 [6,] 2 1 [7,] 4 2 [8,] 3 1 [9,] 2 3 [10,] 2 4 [11,] 2 4 [12,] 4 2 [13,] 3 1 [14,] 2 1 [15,] 3 2 [16,] 3 2 [17,] 4 2 [18,] 2 1 [19,] 3 4 [20,] 3 2Example2> M2 M2Output [,1] [,2] [,3] [,4] [1,] 65 52 42 63 [2,] 52 49 43 54 [3,] 50 35 49 57 [4,] 52 42 36 52 [5,] 48 36 45 43 [6,] 49 65 62 51 [7,] 52 46 56 51 [8,] 43 51 41 53 [9,] 53 40 51 55 [10,] 52 48 48 41 [11,] 54 44 48 42 [12,] 43 34 58 54 [13,] 41 50 51 45 [14,] 47 40 56 39 [15,] 49 48 42 38 [16,] 50 56 47 56 [17,] 55 48 39 52 [18,] 49 39 48 37 [19,] 53 49 58 50 [20,] 38 57 48 59Finding the index of two maximums in each of M2 −> t(apply(M2,1,order,decreasing=TRUE)[1:2,])Output [,1] [,2] [1,] 1 4 [2,] 4 1 [3,] 4 1 [4,] 1 4 [5,] 1 3 [6,] 2 3 [7,] 3 1 [8,] 4 2 [9,] 4 1 [10,] 1 2 [11,] 1 3 [12,] 3 4 [13,] 3 2 [14,] 3 1 [15,] 1 2 [16,] 2 4 [17,] 1 4 [18,] 1 3 [19,] 3 1 [20,] 4 2
Read MoreHow to check if a matrix in R is in binary form?
A binary matrix contains values in form of twos such as 0/1, 1/2, Yes/No etc. If we have a matrix that has some values and we expect that there are only two values in the whole matrix then we can check whether only those two values exist in the matrix or not. For example, if we have a matrix called M then we can check whether it contains only 0/1 in the matrix by using the command all(M %in% 0:1).Example1> M1 M1Output [, 1] [, 2] [, 3] [, 4] [1, ] 0 0 0 ...
Read MoreHow to find the location of a string in an R data frame?
To find the location of a numerical value in an R data frame we use which function and if the value is string then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called df that contains a value say tutor then we can find the location of tutor by using the command which(df=="tutor", arr.ind=TRUE).Example1Consider the below data frame −> x1 x2 x3 df1 df1Output x1 x2 x3 1 2018 2020 2018 2 2020 2020 2015 3 2018 2020 2015 4 2018 2015 2020 ...
Read MoreHow to extract vector using different index for columns in an R matrix?
Suppose we have a matrix and a vector containing indices of equal size as the matrix then we can extract the vector from matrix using the index vector. For this purpose, we can use cbind function as shown in the below examples.Example1> M1 M1Output [,1] [,2] [1,] 4 0 [2,] 1 1 [3,] 1 2 [4,] 2 0 [5,] 3 2 [6,] 2 2 [7,] 1 6 [8,] 1 2 [9,] 3 1 [10,] 1 2 [11,] 2 3 [12,] 2 0 [13,] 3 0 [14,] 0 1 [15,] 2 4 [16,] 1 1 [17,] 3 1 [18,] 0 2 [19,] 2 1 [20,] 2 0Example> Index_M1 Index_M1Output[1] 2 1 2 1 2 2 1 1 2 1 1 2 1 1 1 1 2 2 1 1Example> M1[cbind(seq_along(Index_M1),Index_M1)]Output[1] 0 1 2 2 2 2 1 1 1 1 2 0 3 0 2 1 1 2 2 2Example2> M2 M2Output [,1] [,2] [,3] [,4] [1,] 10 9 9 11 [2,] 13 6 16 8 [3,] 11 11 8 10 [4,] 15 11 9 9 [5,] 10 8 9 9 [6,] 7 14 9 15 [7,] 8 6 8 7 [8,] 4 8 9 12 [9,] 7 12 11 10 [10,] 8 8 9 13 [11,] 9 13 11 6 [12,] 12 5 11 8 [13,] 8 6 15 8 [14,] 6 17 12 7 [15,] 8 10 9 8 [16,] 13 7 11 13 [17,] 5 10 7 7 [18,] 10 11 8 8 [19,] 5 9 9 13 [20,] 5 10 7 6Example> Index_M2 Index_M2Output[1] 3 4 3 3 3 1 3 4 4 3 1 4 3 4 4 1 2 1 1 2Example> M2[cbind(seq_along(Index_M2),Index_M2)]Output[1] 9 8 8 9 9 7 8 12 10 9 9 8 15 7 8 13 10 10 5 10
Read MoreHow to convert a matrix column into list in R?
To convert a matrix column into list can be done by using the apply function. We will have to read the columns of the matrix as list by using as.list function. For example, if we have a matrix called M then the columns in M can be converted into list by using the command apply(M, 2, as.list).Example1> M1 M1Output [, 1] [, 2] [1, ] -1.3256074 -0.07328026 [2, ] 1.1997584 -1.06542989 [3, ] -0.2214659 -1.75903298 [4, ] 1.4446361 -0.12859397 [5, ] -0.1504967 0.97264445Converting M1 columns to a list −> apply(M1, 2, as.list)Output[[1]] [[1]][[1]] ...
Read MoreHow to find the number of unique values in each row of an R data frame?
To find the number of unique values in each row of an R data frame, we can use apply function with length and unique function. For example, if we have a data frame called df that contains multiple columns then the number of unique values in each row of df can be found by using the command apply(df, 1, function(x) length(unique(x))).Example1Consider the below data frame −> x1 x2 x3 x4 df1 df1Output x1 x2 x3 x4 1 3 1 1 2 2 3 2 0 2 3 3 2 0 1 4 3 0 3 1 ...
Read MoreHow to convert a column with missing values to binary with 0 for missing values in R?
To convert a column with missing values to binary with 0 for missing values, we can use as.integer function with complete.cases for the data frame column. For example, if we have a data frame called df that contains a column x which has some missing values then the column x can be converted to binary with 0 for missing values by using the command −as.integer(complete.cases(df$x))Example1Consider the below data frame −> x1 y1 df1 df1Output x1 y1 1 NA 2 2 2 5 3 2 10 4 2 2 5 2 4 6 NA 7 7 NA ...
Read MoreHow to find the column that has the largest sum in R?
To find the column that has the largest sum, we can use sort function for sorting in decreasing order with colSums and accessing the first element of the output which will be the largest sum. For example, if we have a data frame called df that contains multiple columns then the column that has the largest sum can be found by using the command −str(sort(colSums(df[, 1:length(df)]), decreasing=TRUE)[1])Example1Consider the below data frame −> x1 x2 x3 x4 df1 df1Output x1 x2 x3 x4 1 3 4 4 5 2 6 10 3 3 3 6 5 2 5 ...
Read MoreHow to calculate monthly average for time series object in R?
To calculate monthly average for time series object, we can use tapply function with mean. For example, if we have a time series object called TimeData then the monthly average for this series can be found by using the command tapply(TimeData, cycle(TimeData), mean).Example1Consider the below time series object −> Data1 Data1Output Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1 988 695 867 211 915 348 729 518 592 447 448 880 2 551 410 427 134 133 572 637 800 630 878 642 940 3 603 335 638 639 595 512 671 863 752 568 ...
Read More