R Programming Articles

Page 63 of 174

How to name a data frame column with a vector value that has the same name in R?

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

To change the column name of a data frame in R, we can use setNames function. For example, if we have a data frame called df that contains column x and we want to change it to value “Ratings” which is stored in a vector called x then we can use the code df x y yOutput x 1 3 2 8 3 3 4 9 5 5 6 5 7 10 8 2 9 6 10 6 11 3 12 5 13 9 14 1 15 1 16 6 17 2 18 6 19 10 20 6Changing x in y to Ratings:Example> y yOutputRatings 1 3 2 8 3 3 4 9 5 5 6 5 7 10 8 2 9 6 10 6 11 3 12 5 13 9 14 1 15 1 16 6 17 2 18 6 19 10 20 6Let’s have a look at another example:Example> S df_Salary df_SalaryOutput S 1 31827 2 24697 3 45790 4 45345 5 22294 6 30749 7 37721 8 33535 9 45941 10 24028 11 48927 12 33818 13 49152 14 43334 15 20294 16 29664 17 23358 18 20475 19 39355 20 40386Changing S in df_Salary to Salary:Example> df_Salary df_SalaryOutputSalary 1 31827 2 24697 3 45790 4 45345 5 22294 6 30749 7 37721 8 33535 9 45941 10 24028 11 48927 12 33818 13 49152 14 43334 15 20294 16 29664 17 23358 18 20475 19 39355 20 40386

Read More

How to generate orthogonal polynomials in R?

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

We can say that orthogonal is a synonym of perpendicular. If the inner product (inner product is generalization of dot product) of two polynomials is zero then we call them orthogonal polynomials. In R, we can find the orthogonal product by using poly function as shown in the below examples.Example1> x xOutput[1] 1.53798786 -0.85463326 2.39444451 0.82559418 -2.22197322 -1.04243823 [7] -0.04693054 -0.68691236 -1.63040923 -1.42408865Example> orthogonal_x orthogonal_xOutput 1 2 [1, ] 0.41743651 -0.01687537 [2, ] -0.12158589 -0.21414848 [3, ] 0.61038362 0.54027924 [4, ] ...

Read More

How to replace $ sign combined with some specific values in an R data frame?

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

Sometimes we get very dirty data and that is the reason data analysis is a difficult task. Most of the data scientists look for clean data but it is almost impossible due to data warehouses often just focus on the data availability instead of the quality of data. One of the head scratching situations is getting an unnecessary value placed at different position in a random manner, $ sign is also a that type of value. We can remove this from an R data frame by using lapply function.ExampleConsider the below data frame:> x y df1 df1Outputx y 1 C ...

Read More

How to create a matrix using vector generated with rep function in R?

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

Matrix can be only generated if we pass an even number of elements for it. If we want to create a matrix using vector generated with rep function then the length of this vector must be divisible by 2. For example, if we have a vector x that is created with rep function and it’s length is 20 then the matrix say M of size 10x2 using that vector can be constructed by using matrix(x, ncol=2).Example 1> x M1 M1Output[, 1] [, 2] [1, ] 10 10 [2, ] 4 4 [3, ] 7 7 [4, ] 3 3 [5, ...

Read More

How to create a data frame in R with list elements?

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

If a list has the same length of elements (not sub-elements) as the length of each vector for which we want to create the data frame then we first need to create the data frame of vectors then we can easily add the list into the data frame. But if we have a list and other vectors then data frame cannot be created as data.frame function will read each value of the list separately.Example> df1 df1Output   x y 1  6 1 2  8 1 3  6 2 4  8 1 5  5 1 6  3 1 7  6 1 8 ...

Read More

How to replace missing values in a column with corresponding values in other column of an R data frame?

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

Often, we get missing data for analysis and we need to replace those missing values with something. Sometimes, we might want to replace them with the corresponding values in other column especially in situations when both the columns depict similar characteristics. This type of replacement can be easily done with the help of mutate function of dplyr package as shown in the below examples.Example1Consider the below data frame:> set.seed(214) > x1 x2 df1 df1Output x1 x2 1 4 75 2 8 24 3 5 38 4 4 38 5 7 NA 6 6 24 7 10 75 8 4 75 9 ...

Read More

How to extract columns based on particular column values of an R data frame that match\\na pattern?

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

The column values of an R data frame can be easily extracted by subsetting with single square brackets but if we want to extract the column values that match a pattern then we need to use grepl function inside single square brackets, this will help us to match the pattern of the values in the data frame columns.ExampleConsider the below data frame:> set.seed(271) > x1 x2 df1 df1Output x1 x2 1 A242 B71 2 A123 B71 3 A242 B81 4 A242 B87 5 A123 B71 6 A321 B71 7 A187 ...

Read More

How to create an empty plot using ggplot2 in R?

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

The two most easy ways to create an empty plot using ggplot2 are using geom_blank function and also adding the theme_bw along with the geom_blank. The geom_blank will create an empty plot with white gridlines and grey background, on the other hand, addition of theme_bw will create the empty plot with grey gridlines and white background.ExampleConsider the below data frame:> set.seed(151) > x y df dfOutput      x         y 1  -0.05153895 0.3139643 2   0.76573738 0.1816184 3  -0.14673959 0.8201743 4  -0.11318581 1.6005576 5  -0.39551140 0.6770630 6  0.78227595 0.7446956 7  -1.39747811 0.7004385 8  -1.01883832 1.2728014 9 ...

Read More

How to perform post hoc test for Kruskal-Wallis in R?

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

The Kruskal-Wallis test is the non-parametric analogue of one-way analysis of variance. The non-parametric tests are used in situations when the assumptions of parametric tests are not met. If we find significant difference in Kruskal-Wallis then post hoc tests are done to find where the difference exists. For this purpose, we can perform dunn test. The function of dunn test can be accessed through FSA package.Example1Loading FSA package:> library(FSA)Consider the below data frame:> x1 y1 df1 df1Output  x1 y1 1 E 1.1191117 2 D 1.1276032 3 D 1.5610692 4 E 1.1585054 5 E 1.0239322 6 C 0.8000165 ...

Read More

How to check if some specific columns of an R data frame are equal to a column or not?

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

If we have a large amount of data in a data frame and we suspect that some of the data columns are repeated or some of them are equal to a particular column then we can use sapply function in base R to figure it out. In this way, we can remove duplicated columns that does not suppose to help in our data analysis objective.Example1Consider the below data frame:> set.seed(354) > x1 x2 x3 x4 x5 df1 df1Outputx1 x2 x3 x4 x5 1 4 5 4 4 6 2 6 4 8 7 5 3 5 6 4 7 6 ...

Read More
Showing 621–630 of 1,740 articles
« Prev 1 61 62 63 64 65 174 Next »
Advertisements