Programming Articles - Page 1764 of 3363

How to extract first two characters from a string in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:04:35

2K+ Views

A string can be short or long, also we can have a vector or list of strings as well in R. Extraction of partial string is common when we want to use the strings for single or multiple comparisons. If we want to extract first two characters from a string, we can use substr function and the syntax is substr(“String_object Or String”,start=1,stop=2)Examplesx1

How to create boxplot with horizontal lines on the minimum and maximum in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:01:22

1K+ Views

A boxplot shows the minimum, first quartile, median, third quartile, and maximum. When we create a boxplot with ggplot2 it shows the boxplot without horizontal lines on the minimum and maximum, if we want to create the horizontal lines we can use stat_boxplot(geom= 'errorbar') with ggplot function of ggplot2.ExampleConsider the below data frame −set.seed(101) Gender

How to create a scatterplot with colors of the group of points in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:56:05

745 Views

A scatterplot is the plot that has one dependent variable plotted on Y-axis and one independent variable plotted on X-axis. Sometimes the pair of dependent and independent variable are grouped with some characteristics, thus, we might want to create the scatterplot with different colors of the group based on characteristics. For this purpose, we can use colour argument in ggplot function.ExampleConsider the below data frame −set.seed(123) x

How to reverse the bars of a bar plot a using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:54:00

1K+ Views

The bars of a bar plot are generally vertical from bottom to top but we can reverse them as well. Although, this is not a normal practice but we can do it if we want to. For this purpose, we will have to reverse the values on the Y-axis, as a result the bars will be reversed. It can be achieved by using scale_y_continuous.ExampleConsider the below data frame −Salary_Group

How to perform mathematical operations on elements of a list in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:36:04

939 Views

A list can contain many elements and each of them can be of different type but if they are numerical then we can perform some mathematical operations on them such as addition, multiplication, subtraction, division, etc. To do this, we can use Reduce function by mentioning the mathematical operation and the list name as Reduce(“Mathematical_Operation”, List_name).Examplex1

How to write the plot title in multiple lines using plot function in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:34:17

2K+ Views

Mostly, the main title of a plot is short but we might have a long line to write for the main title of the plot. For example, a short version might be “Scatterplot” and a longer version might be “Scatterplot between X and Y”. Therefore, in plot function of R we can use line breaks for the main title as "Scatterplot between X and Y".Exampleset.seed(123) x

How to fill the missing values of an R data frame from the mean of columns?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:32:26

339 Views

Dealing with missing values is one of the initial steps in data analysis and it is also most difficult because we don’t fill the missing values with the appropriate method then the result of the whole analysis might become meaningless. Therefore, we must be very careful about dealing with missing values. Mostly for learning purposes, people use mean to fill the missing values but can use many other values depending on our data characteristic. To fill the missing value with mean of columns, we can use na.aggregate function of zoo package.ExampleConsider the below data frame −x1

How to create a scatterplot with log10 of dependent variable in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:30:28

499 Views

Most of the times, the relationship between independent variable and dependent variable is not linear. Therefore, we want to transform the dependent variable or independent variable based on our experiences. Hence, we also want to plot those transformations to visualize the relationship, one such transformation is taking log10 of the dependent variable. To plot this transformation of the dependent variable, we can use scale_y_continuous(trans='log10').ExampleConsider the below data frame −set.seed(10) x

What is the difference between NA and in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:25:13

480 Views

The missing values are represented by NA but if we read them as "NA" then it becomes a level of a factor variable. If we believe that a vector is numeric and we have an "NA" in that vector then it will not be a numeric vector. On the other hand, if we have a vector with NA then it will be a numeric vector.Examplesx1

How to find the 95% confidence interval for the slope of regression line in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:22:57

5K+ Views

The slope of the regression line is a very important part of regression analysis, by finding the slope we get an estimate of the value by which the dependent variable is expected to increase or decrease. But the confidence interval provides the range of the slope values that we expect 95% of the times when the sample size is same. To find the 95% confidence for the slope of regression line we can use confint function with regression model object.ExampleConsider the below data frame −set.seed(1) x

Advertisements