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
Articles by Nizamuddin Siddiqui
Page 79 of 196
How to change row values based on column values in an R data frame?
Changing row values based on column values means that we want to change the row values for a particular column if the column values satisfy a certain condition. For example, if we have a data frame called df that contains a column say x and we want to set all the values in x to 5 if they are greater than 5 then it can be done as df[df$x>5, ] x1 x2 df1 df1Output x1 x2 1 3 10 2 3 3 3 1 8 4 2 4 5 1 7 6 1 4 ...
Read MoreHow to display raise to the power on X-axis in base R plot?
To display anything different than the vector or column names on the axes, we need to use xlab for X-axis and ylab for Y-axis. Therefore, if we want to display raise to the power on X-axis then xlab argument will be along with the plot function. For example, if we have a vector called x and we want to create a point chart for x -square with X-axis showing x^2 then it can be done as plot(x^2,xlab="x^2").Example> x y plot(x,y)OutputExample> plot(x/1000,y,xlab="x/10^3")Output
Read MoreHow to create bar plot with log values using ggplot2 in R?
To create the bar plot using ggplot2, we simply need to use geom_bar function and if we want to have the log scale of y variable then it can be set with aes under geom_bar. For example, if we have a data frame called df that contains a categorical column x and a numerical column y then the bar plot with log of y can be created by using the below command −ggplot(df, aes(x, y))+geom_bar(stat="identity", aes(y=log(y)))ExampleConsider the below data frame −Live Demo> x y df dfOutput x y 1 S1 53347 2 S2 84208 3 S3 12140 4 ...
Read MoreHow to find the significant correlation in an R data frame?
To find the significant correlation in an R data frame, we would need to find the matrix of p-values for the correlation test. This can be done by using the function rcorr of Hmisc package and read the output as matrix. For example, if we have a data frame called df then the correlation matrix with p-values can be found by using rcorr(as.matrix(df)).Example1Consider the below data frame −Live Demo> x1 x2 x3 df1 df1Output x1 x2 x3 1 -0.96730523 -1.73067540 -0.01974065 2 0.08564529 -0.05200856 0.76356487 3 ...
Read MoreHow to get the colour name from colour code in R?
To get the color name from color code, we can use the color_id function of plotrix package. If we have a vector of colour codes say x then the colour name can be found by using the command sapply(x, color.id).ExampleLive Demo> x xOutput[1] "#FF0000" "#FF1F00" "#FF3D00" "#FF5C00" "#FF7A00" "#FF9900" "#FFB800" [8] "#FFD600" "#FFF500" "#EBFF00" "#CCFF00" "#ADFF00" "#8FFF00" "#70FF00" [15] "#52FF00" "#33FF00" "#14FF00" "#00FF0A" "#00FF29" "#00FF47" "#00FF66" [22] "#00FF85" "#00FFA3" "#00FFC2" "#00FFE0" "#00FFFF" "#00E0FF" "#00C2FF" [29] "#00A3FF" "#0085FF" "#0066FF" "#0047FF" "#0029FF" "#000AFF" "#1400FF" [36] "#3300FF" "#5200FF" "#7000FF" "#8F00FF" "#AD00FF" "#CC00FF" "#EB00FF" [43] "#FF00F5" "#FF00D6" "#FF00B8" "#FF0099" "#FF007A" "#FF005C" "#FF003D" [50] "#FF001F"Loading ...
Read MoreHow to find the row sum for each column by row name in an R matrix?
To find the row sum for each column by row name, we can use rowsum function. For example, if we have a matrix called M then the row sums for each column with row names can be calculated by using the command rowsum(M, row.names(M)).Example1Live Demo> M1 rownames(M1) colnames(M1) M1Output V1 V2 Male 3 6 Female 6 5 Female 7 3 Female 2 5 Female 5 3 Female 4 4 Female 1 4 Female 4 4 Female 7 5 Male 2 5 Female 5 5 Male 7 1 Female 5 6 Male 6 5 Female ...
Read MoreHow to change the legend title in ggplot2 in R?
In ggplot2, by default the legend title is the title of the grouping column of the data frame. If we want to change that title then scale_color_discrete function. For example, if we have a data frame called df that contains two numerical columns x and y and one grouping column say group then the scatterplot with a different legend title can be created by using the below command −ggplot(df, aes(x, y, color=group))+geom_point()+scale_color_discrete("Gender")ExampleConsider the below data frame −Live Demo> x y grp df dfOutput x y grp 1 -2.27846496 0.8121008 ...
Read MoreHow to create group names for consecutively duplicate values in an R data frame column?
The grouping of values can be done in many ways and one such way is if we have duplicate values or unique values then the group can be set based on that. If all the values are unique then there is no sense for grouping but if we have varying values then the grouping can be done. For this purpose, we can use rleid function as shown in the below examples.Example1Consider the below data frame −Live Demo> x df1 df1Output x 1 2 2 1 3 2 4 2 5 1 6 ...
Read MoreHow to find the row-wise mode of a matrix in R?
There is no in-built function to find the mode in R, hence we need to create one and then apply it to the rows of the matrix. The function for mode is created as follows −mode M1 M1Output [,1] [,2] [,3] [,4] [,5] [1,] 2 2 1 2 2 [2,] 2 2 2 2 1 [3,] 2 2 1 1 1 [4,] 2 1 1 1 1 [5,] 2 1 1 2 2> apply(M1,1,mode)Output[1] 2 2 1 1 2Example2Live Demo> M2 M2Output [,1] [,2] [,3] [,4] [,5] [1,] 1 1 2 2 1 [2,] 2 1 1 2 1 [3,] 2 2 1 1 1 [4,] 2 1 1 2 2 [5,] 2 1 1 2 2 [6,] 1 2 1 1 2 [7,] 1 1 2 1 2 [8,] 2 2 1 2 1 [9,] 2 1 1 2 2 [10,] 1 1 2 2 2 [11,] 1 1 2 1 2 [12,] 1 2 2 2 1 [13,] 2 2 2 2 1 [14,] 2 1 2 2 1 [15,] 1 2 1 1 2 [16,] 2 2 1 2 1 [17,] 2 2 1 1 1 [18,] 2 1 1 2 1 [19,] 1 1 1 2 1 [20,] 2 1 1 2 2> apply(M2,1,mode)Output[1] 1 1 1 2 2 1 1 2 2 2 1 2 2 2 1 2 1 1 1 2Example3Live Demo> M3 M3Output [,1] [,2] [,3] [,4] [,5] [1,] 1 3 3 2 1 [2,] 2 3 1 2 2 [3,] 2 2 3 3 1 [4,] 1 3 1 3 2 [5,] 3 1 2 1 2 [6,] 2 3 1 1 1 [7,] 2 2 2 3 1 [8,] 1 2 2 2 2 [9,] 2 1 2 1 2 [10,] 1 3 1 2 1 [11,] 2 1 3 1 1 [12,] 1 1 3 2 2 [13,] 2 1 1 1 2 [14,] 2 1 3 3 2 [15,] 1 2 3 1 2 [16,] 1 2 1 2 1 [17,] 3 1 1 3 2 [18,] 3 3 3 3 1 [19,] 3 2 3 1 1 [20,] 3 3 2 2 1> apply(M3,1,mode)Output[1] 1 2 2 1 1 1 2 2 2 1 1 1 1 2 1 1 1 3 1 2Example4Live Demo> M4 M4Output [,1] [,2] [,3] [,4] [,5] [1,] 10 10 9 10 9 [2,] 9 9 10 9 9 [3,] 9 9 9 10 10 [4,] 10 9 9 10 10 [5,] 10 10 9 10 9 [6,] 10 10 9 10 10 [7,] 9 9 9 10 9 [8,] 9 10 9 10 9 [9,] 9 9 9 9 9 [10,] 9 10 9 10 9 [11,] 10 10 9 9 9 [12,] 9 9 9 9 9 [13,] 10 10 10 9 10 [14,] 10 9 10 10 10 [15,] 9 10 9 10 9 [16,] 9 10 9 10 9 [17,] 9 10 10 9 10 [18,] 9 9 9 9 10 [19,] 10 9 9 10 9 [20,] 10 9 9 10 9> apply(M4,1,mode)Output[1] 10 9 9 10 10 10 9 9 9 9 9 9 10 10 9 9 10 9 9 9
Read MoreHow to find the frequency table for factor columns in an R data frame?
If we have factor columns in an R data frame then we want to find the frequency of each factor level for all the factor columns. This can be done with the help of sapply function with table function. For example, if we have a data frame called df that contains some factor columns then the frequency table for factor columns can be created by using the command sapply(df, table).Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output x1 x2 1 D a 2 D b 3 D c 4 D b 5 D c 6 C a ...
Read More