To sort a vector based on manual position of elements, we can use order function along with the factor function. The factor function will help us to arrange the vector elements in the order we want by defining the levels as vector elements and order function will order them. Check out the below examples to understand how it works.Example1Live Demo> x1 x1Output[1] 0 1 0 0 1 2 1 2 3 1 2 2 2 4 3 1 4 1 0 1 1 3 3 0 0 4 4 2 4 2 4 2 0 4 0 1 1 [38] ... Read More
If we have multiple vectors of different lengths then the boxplot for such vectors can be created by creating a single data frame using those vectors with a categorical column showing the name of the vectors and a numerical column having the corresponding values. Then boxplot function will be used as shown in the below example.ExampleConsider the below vector x and y and create the data frame using them −Live Demo> x y df dfOutput X Grp 1 4 x 2 2 x 3 1 x 4 2 x 5 0 x 6 2 ... Read More
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 More
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
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 More
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 More
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 More
To extract the unique combination of rows, we can subset the data frame with single square brackets and use the negation of the duplicated function after sorting the rows in the data frame. The sorting of the data frame can be done with the help of apply function and we will have to transpose the sorting as shown in the below examples. To understand how it works, do it in parts.Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output x1 x2 1 3 1 2 1 0 3 1 0 4 1 3 5 ... Read More
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 More
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 More
 Data Structure
 Networking
 RDBMS
 Operating System
 Java
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP