Found 2038 Articles for R Programming

How to remove duplicate rows in an R data frame if exists in two columns?

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

978 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

How to display negative labels below bars in barplot using ggplot2 in R?

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

800 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

How to add a row to a 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

How to deal with missing column for row names when converting data frame to data.table object in R?

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

375 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

How to remove the plot margin in base R between the axes and the points inside the plot?

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

788 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

How to remove rows from an R data frame that contains at least one NaN?

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

282 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

How to increase the 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

How to find the row mean for columns in an R data frame by ignoring missing values?

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

5K+ 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

How to deal with error “Error in shapiro.test(…) : sample size must be between 3 and 5000” in R?

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

3K+ 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

How to display positive sign for X-axis labels in R using ggplot2?

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

371 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

Advertisements