Extract Row for Groupwise Maximum in R Data Table

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:39:10

462 Views

To extract the row for groupwise maximum in another column of an R data.table object, can make use of which.max function by defining the grouping column. It means that if we have a categorical/grouping column and a numerical column then we groupwise maximum will be the maximum for each grouping level in the numerical column and we can extract the row based on these two columns. Check out the examples to understand how it works.Example1Loading data.table package and creating a data.table object −> library(data.table) > x1 x2 x3 DT1 DT1Output   x1 x2 x31:  B  3  2 2:  C  6  0 ... Read More

Find Number of Non-Empty Values in R Data Frame Column

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:38:24

3K+ Views

To find the number of non-empty values, we can find the negation of the sum of empty values which is actually the total number of non-empty values. For example, if we have a data frame df that contains a column x which has some empty values then to find the total number of non-empty values we can find the opposite/negation of total empty values. This can be done with the help of sum function and negation operator as shown in the below examples.Example1Consider the below data frame −Live Demo> x df1 df1Output   x 1  1 2  2 3   4 ... Read More

Sort a Vector in R Based on Manual Position of Elements

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:37:40

677 Views

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

Create Boxplot of Vectors with Different Lengths in R

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:31:19

2K+ Views

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

Change Row Values Based on Column Values in R Data Frame

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:31:00

529 Views

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

Display Raise to the Power on X-Axis in Base R Plot

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:25:13

430 Views

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

Create Bar Plot with Log Values Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:23:39

1K+ Views

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

Find Significant Correlation in an R Data Frame

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:23:21

620 Views

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

Get Colour Name from Colour Code in R

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:22:31

608 Views

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

Extract Unique Combination of Rows in an R Data Frame

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:20:12

872 Views

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

Advertisements