Deal with Missing Column for Row Names in R

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

511 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

438 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

Increase Thickness of Histogram Lines in Base R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:32:14

1K+ Views

To increase the thickness of histogram lines in base R, we would need to use par function by defining the thickness size of the line. If we want to do so then line thickness must be defined first before creating the histogram. An example of line size could be line

Find Row Mean for Columns in R Data Frame Ignoring Missing Values

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:30:25

6K+ Views

To find the row mean for columns by ignoring missing values, we would need to use rowMeans function with na.rm. For example, if we have a data frame called df that contains five columns and some of the values are missing then the row means will be calculated by using the command: rowMeans(df,na.rm=TRUE).Consider the below data frame −Example Live Demox1

Deal with Error in Shapiro Test: Sample Size Must Be Between 3 and 5000 in R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:22:48

4K+ Views

The shapiro.test has a restriction in R that it can be applied only up to a sample of size 5000 and the least sample size must be 3. Therefore, we have an alternative hypothesis test called Anderson Darling normality test. To perform this test, we need load nortest package and use the ad.test function as shown in the below examples.Consider the below data frame −Example Live Demox

Display Positive Sign for X-Axis Labels in R Using ggplot2

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:20:04

548 Views

By default, the positive signs are not displayed in any plot in R. It is well known that if there is no sign seen with any value then it is considered positive, therefore, we do not need the sign but to distinguish between 0 and positive values it could be done. To display positive sign for X-axis labels, we can use scale_x_continuous function.Consider the below data frame −Example Live Demox

Change Size of Plots Using grid.arrange in R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:15:24

10K+ Views

To change the size of plots arranged using grid.arrange, we can use heights argument. The heights argument will have a vector equal to the number of plots that we want to arrange inside grid.arrange. The size of the plots will vary depending on the values in this vector.Consider the below data frame −Example Live Demox

Create Cumulative Sum Plot in Base R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:11:00

3K+ Views

To create a cumulative sum plot in base R, we can simply use plot function. For cumulative sums inside the plot, the cumsum function needs to be used for the variable that has to be summed up with cumulation. For example, if we have two vectors say x and y then the plot with cumulative sum plot can be created as plot(x,cumsum(y)).Examplex1

Create a Plot in Base R with Larger Tick Marks

Nizamuddin Siddiqui
Updated on 08-Feb-2021 05:08:48

2K+ Views

To create a plot in base R with tick marks of larger size, we can make use of axis function tck argument. The tck argument value will decide the size of the tick mark but since the ticks lie below the plot area hence the value will have a negative associated with it. Therefore, it will be like -0.05. Check out the below examples to understand how it works.Exampleplot(1:10,axes=FALSE,frame=TRUE) axis(1,1:10,tck=-0.02) axis(2,1:10,tck=-0.02)OutputExampleplot(1:10,axes=FALSE,frame=TRUE) axis(1,1:10,tck=-0.05) axis(2,1:10,tck=-0.05)Output

Advertisements