R Programming Articles

Page 70 of 174

How to display R-squared value on scatterplot with regression model line in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 9K+ Views

The R-squared value is the coefficient of determination, it gives us the percentage or proportion of variation in dependent variable explained by the independent variable. To display this value on the scatterplot with regression model line without taking help from any package, we can use plot function with abline and legend functions.Consider the below data frame −Exampleset.seed(1234) x

Read More

How to convert a numerical column into factor column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 7K+ Views

Often, we find that the values that represent factor levels are recorded as numerical values, therefore, we need to convert those numerical values to factor. In this way, we can use the factor column properly in our analysis otherwise R program will treat the factors as numerical values and the analysis output will be incorrect.Exampledata(mtcars) str(mtcars)Output'data.frame': 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... $ disp: num 160 160 108 258 360 ... $ hp : num 110 110 93 110 175 105 245 62 95 123 ... $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... $ wt : num 2.62 2.88 2.32 3.21 3.44 ... $ qsec: num 16.5 17 18.6 19.4 17 ... $ vs : num 0 0 1 1 0 1 0 1 1 1 ... $ am : num 1 1 1 0 0 0 0 0 0 0 ... $ gear: num 4 4 4 3 3 3 3 4 4 4 ... $ carb: num 4 4 1 1 2 1 4 2 2 4 ... mtcars$cyl

Read More

How to find unique permutations if a vector contains repeated elements in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 417 Views

We can use permn function from combinat package to find the permutations but if we have repeated elements in the vector then the result will not have unique permutations, therefore, we need to use unique function along with the permn function. For example, if we have a vector 1, 2, 1 then the permutations will be (1 2 1), (1 1 2), (1 1 2), (1 2 1), (2 1 1), (2 1 1) and the unique permutations will be (1 2 1), (1 1 2), (2 1 1).Examplex1

Read More

How to create a horizontal bar chart using ggplot2 with labels at inside end of the bars in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To create a horizontal bar chart using ggplot2 package, we need to use coord_flip() function along with the geom_bar and to add the labels geom_text function is used. These two functions of ggplot2 provides enough aesthetic characteristics to create the horizontal bar chart and put the labels at inside end of the bars.Examplex

Read More

How to find the sum of consecutive values considering two values each time in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 697 Views

Finding the sum of consecutive value while considering the sum of two values each time means the sum of first two values, then the sum of second value and the third value, then the sum of third value and the fourth value, then the sum of fourth value and the fifth value, and so on. For this purpose, we can use rollapply function from zoo package.Loading zoo packageibrary(zoo)Examplex1

Read More

How to set NA values to TRUE for a Boolean column in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 988 Views

Dealing with NA values is one of the boring and almost day to day task for an analyst and hence we need to replace it with the appropriate value. If in an R data frame, we have a Boolean column that represents TRUE and FALSE values, and we have only FALSE values then we might want to replace NA’s with TRUE. In this case, we can use single square bracket and is.na to set all NA’s to TRUE.Exampleset.seed(999) S.No.

Read More

How to find the number of NA’s in each column of an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 927 Views

Sometimes the data frame is filled with too many missing values/ NA’s and each column of the data frame contains at least one NA. In this case, we might want to find out how many missing values exists in each of the columns. Therefore, we can use colSums function along with is.na in the following manner: colSums(is.na(df)) #here df refers to data frame name.Consider the below data frame −Exampleset.seed(109) x1

Read More

How to find contingency table of means from an R data frame using cast function?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 321 Views

The contingency table considers the numerical values for two categorical variables. Often, we require contingency table for counts, especially in non-parametric analysis but it is also possible that we want to use means for our analysis. Hence, we can use cast function from reshape package which solves the problem of creating contingency table easily.Consider the below data frame −Exampleset.seed(99) x1

Read More

How to subset an R data frame based on string values of a columns with OR condition?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

We might want to create a subset of an R data frame using one or more values of a particular column. For example, suppose we have a data frame df that contain columns C1, C2, C3, C4, and C5 and each of these columns contain values from A to Z. If we want to select rows using values A or B in column C1 then it can be done as df[df$C1=="A"|df$C1=="B",].Consider the below data frame −Exampleset.seed(99) x1

Read More

How to create a transparent polygon using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 865 Views

A transparent polygon just represents the border lines and a hollow area; thus, we can only understand the area covered but it becomes a little difficult to understand the scales. Hence, this visualisation technique is not as useful as others that fills the area with a different color. But it could be used if the range of the data is not large.Consider the below data frame −Exampleset.seed(123) x

Read More
Showing 691–700 of 1,740 articles
« Prev 1 68 69 70 71 72 174 Next »
Advertisements