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
Articles by Nizamuddin Siddiqui
Page 50 of 196
How to select columns in R without missing values?
There are two easy methods to select columns of an R data frame without missing values, first one results in a vector and other returns a matrix. For example, if we have a data frame called df then the first method can be used as df[,colSums(is.na(df))==0] and the second method will be used as t(na.omit(t(df))).ExampleConsider the below data frame −df1
Read MoreHow to display mean in a boxplot with cross sign in base R?
To display mean in a boxplot with cross sign in base R, we can use the points function and pass the mean with pch = 4 that represents a star, also we can change the color to highlight the mean using col argument and the size of the start can be changed using lwd argument as shown in the below examples.Examplex
Read MoreHow to create a bar plot with bars for missing values in R?
To create a bar plot in R, we can use barplot function but if there exist some missing values in the data then we can use ggplot2 package. For example, if we have a data frame having two vectors say x and y, x containing categorical values with NA as one of the values and y having counts/frequency for each of the categories then the bar plot will be created by using the command ggplot(df, aes(x, y))+geom_bar(stat="identity").ExampleConsider the below data frame −> x y df dfOutput x y 1 A 24 2 B 21 3 45Creating ...
Read MoreHow to check if a vector exists in a list in R?
To check if a vector exists in a list, we can use %in%, and read the vector as list using list function. For example, if we have a list called LIST and a vector called V then we can check whether V exists in LIST using the command LIST %in% list(V).ExampleConsider the below list −List
Read MoreHow to split a data frame using row number in R?
To split a data frame using row number, we can use split function and cumsum function. The split function will split the rows and cumsum function will select the rows. For example, if we have a data frame called df that contains twenty rows then we can split into two data frames at row 11 by using the below command −split(df,cumsum(1:nrow(df)%in%11)).ExampleConsider the below data frame −x1
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 remove starting and ending zeros in an R vector?
To remove starting and ending zeros in an R vector, we can use min and max function to access values except 0 and then subsetting with single square brackets. For example, if we have a vector called x then we can remove starting and ending zeros by using the command −x[min(which(x!=0)):max(which(x!=0))]Examplex1
Read MoreHow to extract data frame columns stored in a list in R?
Suppose we have two frames each having 5 columns that are stored in a list in R and the data that belongs to same columns has some kind of inherent relationship or we want to check whether there exists a relationship between them then we might want to extract those columns. Therefore, we can use lapply function for this extraction. For example, if we have a list called LIST that store two data frames then column 3 of each data frame can be extracted by using the command lapply(LIST, "[", 3).ExampleConsider the below data frames and list of these data ...
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 More