Display Zero Frequency for Bars in Base R Barplot

Nizamuddin Siddiqui
Updated on 05-Mar-2021 07:03:59

834 Views

When we create a barplot in base R, the bars are plotted for all the values in the vector but if we have a gap in the values then the bar with zero frequency for that gap is not plotted. For example, if we have a vector called x that contains 100 values consisting of 0, 1, 3 then the barplot will not represent zero frequency for 2. To solve this problem, we can use factor function in the barplot function as shown in the below examples.Example1Live Demo> x xOutput  [1] 0 1 1 1 3 1 3 1 0 ... Read More

Apply One Sample T-Test on All Columns of an R Data Frame

Nizamuddin Siddiqui
Updated on 05-Mar-2021 07:03:42

1K+ Views

When we want to apply t-test on columns of a data frame then we generally perform them one by one by accessing the appropriate columns but if we want to apply the test on all columns of the data frame then we can take the help of sapply function. For example, if we have a data frame called df that contains multiple columns then the one sample-test can be applied to all columns using the command sapply(df, t.test).Example1Consider the below data frame −Live Demo> x1 x2 x3 df1 df1Output            x1        x2   ... Read More

Plot Rows of a Data Frame as Lines in R

Nizamuddin Siddiqui
Updated on 05-Mar-2021 07:03:19

4K+ Views

To plot row of a data frame as lines, we can use matplot function but we would need to transpose the data frame because transposed values of the data frame will be read as columns and the matplot function plot the columns not rows. For example, if we have a data frame called df then the plot of rows as lines can be created by using the command −matplot(t(df), type="l")Example1Consider the below data frame −Live Demo> x1 x2 x3 df1 df1Output  x1 x2 x3 1  0  9  5 2  3  4  2 3  0  2  1 4  3  7  3 ... Read More

Find Inverse of Log10 for an R Data Frame Column

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:55:42

2K+ Views

To find the log10 of a data frame column then log10 function will be used but to find the inverse of the log10 can be found by putting 10 raises to the power of the log10 column. For example, if we have a data frame called df that contains a column x then the log10 will be found by usinglog10(df$x)after that the inverse will be found by using 10^(df$x).Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output      x1 x2 1  66210  2 2  42033  2 3  39309  2 4  80353  3 5  92864  2 6  48621 ... Read More

Split a Vector by Equal and Different Number of Elements in R

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

1K+ Views

To split a vector by equal and different number of elements, we can use split function along with rep function. The rep function will define the repetition of the divisions for equal as well as different number of elements. For example, if a vector say x contains fifty values then splitting of x with different number of elements as 20, 10, 10, 5, 5 this can be done by using the command split(x, rep(1:5, c(20, 10, 10, 5, 5))).Example1Live Demo> x1 x1Output [1]  1.30316414 -0.80488291  0.23170812 -0.07318560 -0.73388857 -0.85952329  [7] -0.88713465 -0.26618866  1.45634603  0.31282735  1.39285785  0.32501145 [13] -1.72088389 -0.20699097 -0.37173907  0.03042574 ... Read More

Extract Row for Groupwise Maximum in R Data Table

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

436 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

644 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

509 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

Advertisements