Create Boxplot for a List Object in Base R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:31:26

1K+ Views

A list object can contain multiple elements of data, also the size of the data may vary. If a list object has numerical vectors then the boxplot for each of the elements can be created simply by using boxplot function. For example, if we have a list object called LIST that contains five numerical vectors then the boxplot for each of the vectors can be created by using the command boxplot(LIST)Example Live DemoList

Change First Value for Each Group in Data Table Object in R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:29:14

719 Views

To change the first value for each group in data.table object, we can use single square brackets for accessing and changing the value to desired value. For example, if we have a data.table object called DT that contains a group column defined by Class and a numerical column defined by Response then the first value of Response for each Class can be set to say 5 by using the command DT[,Response:=c(2,Response[-]),by=Class]Consider the below data.table object −Examplelibrary(data.table) Group

Display Text in Base R Plot with 180 Degrees Rotation

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:25:39

2K+ Views

To display text in base R plot with 180 degrees rotation, we can use text function. We would need to define both the axes values for the position where we need the text to be rotated inside the plot. For the rotation of the text, srt argument will be used and set to the value equals to 270. Check out the below example to understand how it works.Examplex

Create a Plot of Binomial Distribution in R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:24:09

2K+ Views

A binomial distribution is based on the distribution of success and failure, the other two parameters of binomial distribution are the sample size and the probability of success. To create a plot of binomial distribution, we first need to define the density of the binomial distribution using dbinom function. The plotting can be done by using plot function with success and the density as shown in the below examples.Examplex

Remove Duplicate Rows in R Data Frame Based on Two Columns

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:21:56

1K+ Views

If two values are repeated in a column that means there are many same values in that column but if those values are repeated in column as well as rows then they are called duplicated rows in two columns. To remove duplicate rows in an R data frame if exists in two columns, we can use duplicated function as shown in the below examples.Consider the below data frame −Example Live Demox1

Display Negative Labels Below Bars in Barplot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:11:16

1K+ Views

Be default, the labels on the plot are represented without sign in a barplot that is created by using ggplot2 but we might want to display the sign of the labels especially in cases where we have some negative values. This can be done with the help of geom_text function of ggplot2 package as shown in the below example.Consider the below data frame −Example Live Demox

Add a Row to a Data Frame from Another Data Frame in R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:08:41

1K+ Views

Sometimes we want to add new data to original data frame in situations such as we need more data for analysis, looking for comparison between small size and large size data, or some data is missing in the original data and hence need more to be added from other data sets. One such thing would be adding a new to an existing data frame from another data frame. It can be done with the help of rbind function as shown in the below example.Consider the below data frames df1 and df2 −Example Live Demox

Deal with Missing Column for Row Names in R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:01:56

502 Views

To deal with missing column of row names when converting data frame in R to data.table object, we need to use keep.rownames argument while converting the data frame. For example, if we have a data frame called df that needs to be converted to a data.table object without missing row names then we can use the below command −data.table(df,keep.rownames=TRUE)Examplelibrary(data.table) head(mtcars)Output            mpg     cyl    disp   hp    drat     wt      qsec   vs     am   gear carb Mazda RX4    21.0    6     160    110    3.90   2.620     16.46  0      1    4     4 Mazda RX4 Wag 21.0   6     160   110   3.90   2.875     17.02    0      1    4     4 Datsun     710      22.8   4    108 93 3.85   2.320    18.61     1      1    4     1 Hornet 4 Drive 21.4    6   258  110   3.08     3.215     19.44   1      0    3     1 Hornet Sportabout 18.7  8  360  175  3.15     3.440      17.02   0      0    3     2 Valiant      18.1    6    225  105   2.76     3.460      20.22   1      0    3     1Examplemtcars_data_table

Remove Plot Margin in Base R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:40:39

1K+ Views

To remove the plot margin in base R between the axes and the points inside the plot, we can use xaxs and yaxs argument in plot function. Depending on the choices of the arguments xaxs and yaxs, the plot region in the respective direction is 4% larger than specified by these limits or exactly matches the "i" limits.Examplex

Remove Rows from R Data Frame Containing NaN Values

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:37:52

423 Views

The NA values and NaN values are very different in nature, therefore, removal of rows containing NA values is different from removal of rows containing NaN values. For example, if we have a data frame that has NaN values the rows will be removed by using the is.finite function as shown in the below examples.Consider the below data frame −Example Live Demox1

Advertisements