R Programming Articles

Page 109 of 174

Create scatterplot for two dependent variables and one independent variable in R.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 2K+ Views

To create scatterplot for two dependent variables and one independent variable, we can use geom_point function and geom_smooth function of ggplot2 package. Both of these functions will be used twice, where we can define the aesthetics of the plot for each dependent variable as shown in the Example below.ExampleFollowing snippet creates a sample data frame −x

Read More

Create line chart for mean covered with minimum and maximum in R.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 1K+ Views

To create line chart for mean covered with minimum and maximum in R, we first need to create columns for row means, row minimum, and row maximum after that geom_line function can be used along with geom_ribbon function of ggplot2 package as shown in the below example.ExampleFollowing snippet creates a sample dataframe.x

Read More

How to add two columns if both contains missing values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 1K+ Views

If we want to add two columns of an R data frame and each of them contains missing values then the addition of columns can be done in one of the following ways −Adding both the column values if they are numeric.Returning numeric if one of the columns has missing value.Returning NA if both the columns have missing value.To this we can take help of apply function and ifelse function as shown in the below given examples.Example 1Following snippet creates a sample data frame −x1

Read More

How to take transpose of vectors stored in an R list?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 343 Views

We can take the transpose of vectors stored in an R list by using Map function with do.call. For example, if we have a list called LIST that contains say ten vectors then we can transpose these vectors by using the command given below − do.call(Map,c(f=c,LIST))Check out the below examples to understand how it works.Example 1To take transpose of vectors stored in an R list, use the command given below −List1

Read More

How to subset a matrix based on values in a particular column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 3K+ Views

To subset a matrix based on values in a particular column, we can use single square brackets and provide the row and column values. The column values will be set for the columns that we want to subset and the row value will be set for the values of the column using which we want to subset the matrix.Check out the below example to understand how it works.ExampleFollowing snippet creates a matrix −M

Read More

How to highlight outliers in a boxplot in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 738 Views

To highlight outliers in a boxplot, we can create the boxplot with the help of Boxplot function of car package by defining the id.method.For example, if we have a vector called V then the boxplot of V with highlighted outliers can be created by using the below given command −Boxplot(~V,id.method="y")Example 1To highlight outliers in a boxplot, use the command given below −library(car) x

Read More

How to check if a string contains only one type of character in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 512 Views

If a string contains more than one character then all of them could be same or different. If we want to check if a string contains only one type of character, then stri_count_fixed function of stringi package will be used with nchar function.Check out the below given examples to understand how it can be done.Example 1Following snippet creates a sample data frame −Countries

Read More

How to split a vector into smaller vectors of consecutive values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 872 Views

To split a vector into smaller vectors of consecutive values, we can use split function with cumsum function.For example, if we have a vector called X that contains some consecutive values then we split X into smaller vectors of consecutive values by using the command given below −split(x,cumsum(c(1,diff(x)!=1)))Example 1Following snippet creates a vector −x1

Read More

How to find the mean based on single group value in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 1K+ Views

To find the mean based on single group value in an R data frame, we can use mean function with subsetting through single square brackets.For example, if we have a data frame called df that contains a categorical column say C having three groups Low, Medium, High and a numerical column say Num then mean of Num for Medium group can be found by using the command given below −mean(df$C[df$Num=="Medium"])Example 1Following snippet creates a sample data frame −Group

Read More

How to fill a data.table row with missing values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 578 Views

Instead of filling missing values, we sometimes need to replace the data with missing values. This might be required in situations when missing values are coded with a number or the actual values are not useful or sensible for the data study. Also, we might want to replace the values with something else in the future.Check out the below given examples to understand how we can fill data.table row with missing values.Example 1Following snippet creates a data.table object −library(data.table) x1

Read More
Showing 1081–1090 of 1,740 articles
« Prev 1 107 108 109 110 111 174 Next »
Advertisements