Found 2038 Articles for R Programming

How to create a group column in an R data frame?

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:13:42

780 Views

Suppose we have a data frame called df that contains two columns say X and Y then we can create a group column based on X and Y by converting df into a data.table object and creating list of values in X and Y with list function.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −x1

How to multiply two matrices in R if they contain missing values?

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:04:41

640 Views

If we want to multiply two matrices if they contain missing values then we first need to convert the missing values to zeros and then the multiplication can be easily done. If we do not do so then the Output of the multiplication will have NAs at all positions.Check out the Examples given below to understand the correct of multiplication if NAs are present in the matrices.Example 1Following snippet creates a sample matrix −M1

How to hide outliers in base R boxplot?

Nizamuddin Siddiqui
Updated on 05-Nov-2021 06:54:45

248 Views

To hide outliers in base R boxplot, we can use range argument inside boxplot function but we will have to play with range argument. The range argument can take many values therefore, we would need to find the correct one that removes all the outliers. To understand how it works check out the Example given below −ExampleTo hide outliners in base R boxplot, use the following snippet −x

Create darker gridlines in theme_bw for a ggplot2 graph in R.

Nizamuddin Siddiqui
Updated on 05-Nov-2021 06:47:02

930 Views

To create darker gridlines in theme_bw for a ggplot2 graph, we can use theme function, where we can use major and minor gridlines element line to black color with the help of panel.grid.major and panel.grid.minor argument as shown in the below Example.We can use any other color but black is the most preferred one as it matches with the border color.ExampleFollowing snippet creates a sample data frame −x

How to check if a matrix has any missing value in R?

Nizamuddin Siddiqui
Updated on 05-Nov-2021 06:41:26

1K+ Views

To check if a matrix has any missing value in R, we can use any function along with is.na function.For Example, if we have a matrix called M then we can use the below command to check whether M contains any missing value or not −any(is.na(M))Example 1Following snippet creates a sample matrix −M1

How to remove line numbers from data.table object in R?

Nizamuddin Siddiqui
Updated on 05-Nov-2021 06:31:38

2K+ Views

To remove line numbers from data.table object in R, we can set row.names to FALSE and print the data.table object.For Example, if we have a data.table object called DT then we can remove line numbers from DT by using the command given below −print(DT,row.names=FALSE)Example 1To load data.table object and create an object use the following snippet to create a data frame −library(data.table) x1

Create stacked bar plot for one categorical variable in an R dataframe.

Nizamuddin Siddiqui
Updated on 05-Nov-2021 06:20:11

1K+ Views

To create stacked bar plot for one categorical variable in an R data frame, we can use ggplot function and geom_bar function of ggplot2 and provide 1 as the X variable inside aes.For Example, if we have a data frame called df that contains a one categorical column say C and one numerical column Num then we can create the stacked bar plot by using the below command −ggplot(df,aes(1,Num,fill=C))+geom_bar(stat="identity")ExampleFollowing snippet creates a sample data frame −x

Find the value in an R data frame column that exist n times.

Nizamuddin Siddiqui
Updated on 05-Nov-2021 06:15:19

119 Views

To find the value in an R data frame column that exist n times, we first need to tabulate the column with factor then extracting the levels of the column after that reading them with as.numeric.Check out the Examples given below to understand how it can be done.Example 1Following snippet creates a sample data frame −x

How to round matrix values in R?

Nizamuddin Siddiqui
Updated on 05-Nov-2021 06:06:36

3K+ Views

To round matrix values, we can use round function.For Example, if we have a matrix called M and we want to round the value in M to 2 decimal places by using the below command −M

How to display numbers with decimal in R data frame column?

Nizamuddin Siddiqui
Updated on 05-Nov-2021 05:59:31

6K+ Views

To display numbers with decimal in R data frame column, we can use format function with round function and nsmall argument.For Example, if we have a data frame called df that contains an integer column say X then we can display numbers in X with 2 decimal places by using the below command −df$X

Advertisements