Server Side Programming Articles

Page 1121 of 2109

How to create a frequency table in data frame format in R?

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

To create a frequency table in R, we can simply use table function but the output of table function returns a horizontal table. If we want to read the table in data frame format then we would need to read the table as a data frame using as.data.frame function. For example, if we have a table called T then to convert it into a data frame format we can use the command as.data.frame(T).Example1> x1 x1Output[1] 2 0 2 3 2 3 1 2 1 4 0 0 4 4 1 3 1 2 1 3 2 3 2 1 4 ...

Read More

How to remove rows using character column that has elements of size less than 3 in an R data frame?

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

To find the number of characters in character vector elements or the elements in a character column of an R data frame, we can use nchar function. Therefore, if we want to remove rows that has elements of size less than 3 we would need to use the same function and then subset function will be used to remove the required rows as shown in the below examples.Example1Consider the below data frame −> x1 x2 df1 df1Output    x1   x2 1  India 1 2  India 2 3  UK    1 4  UK    2 5  China 1 6  India ...

Read More

How to add a new column to a data frame using mutate in R?

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

The mutate function of dplyr package in R can help us to add a new column to a data frame and the benefit of using mutate is that we can decide the position of the new column during the addition. For example, if we have a data frame called df that contains three columns say x, y, a then we can add a new column say z after y using mutate function. To understand how it can be done, check out the below examples.Example1Consider the below data frame −> x1 x3 df1 df1Output   x1 x3 1  2  3 2  1 ...

Read More

How to preserve data frame structure after applying a function in R?

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

When we apply a function using apply family, by default the output is not in the form of a data frame. If we want to preserve the original data frame structure then we need to set the application of the apply family by setting it to the original data frame with single brackets and no arguments as shown in the below examples.Example1Consider the below data frame −> df1 df1Output   x1 x2 1  4 2 2  6 2 3  5 2 4  2 1 5  8 4 6  7 2 7  5 3 8 ...

Read More

How to convert a binary matrix to logical matrix in R?

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

A binary matrix contains values such as Yes or NO, 1 or 0, or any other two values that represents opposite mostly and the globally accepted logical values are FALSE and TRUE. Therefore, to convert a binary matrix to logical matrix, we can use ifelse function and convert the one category of binary variable to appropriate logical value and for the rest returns the left-out value. This is a very easy task in R, check out the below examples to understand how it can be done.Example1> M1 M1Output[, 1] [, 2] [1, ] "No" "Yes" [2, ] "No" "No" [3, ...

Read More

How to find the unique rows in an R data frame?

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

A unique row in an R data frame means that all the elements in that row are not repeated with the same combination in the whole data frame. In simple words, we can say that if we have a data frame called df that contains 3 columns and 5 rows then all the values in a particular row are not repeated for any other row. The search of this type of rows might be required when we have a lot of duplicate rows in our data set. To do this, we can use group_by_all function of dplyr package as shown ...

Read More

How to find the row and column index of a character value in an R data frame?

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

To find the row and column index for a numerical value in an R data frame we use which function and if the value is character then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called df that contains a value say Data then we can find the row and column index of Data by using the command as which(df=="Data", arr.ind=TRUE).Example1Consider the below data frame −> x1 x2 df1 df1Output    x1    x2 1 Female  5 2 Female  5 3 Female  6 4 Female  6 ...

Read More

How to split month and year from 6-digit numbers in an R data frame column?

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

Sometimes we get data that is not in the form to proceed with the analysis and one such situation is dates stored in 6-digit numbers as 202105 that represents fifth month of year 2021 instead of date format as 2021/05. Therefore, we need to split the date and extract the month and year from the number. This can be done easily with the help of transform function as shown in the below examples.Example1Consider the below data frame −> Date Response1 df1 df1Output   Date    Response1 1 202103   0.946367628 2 202103   1.241718518 3 202101  -0.657920816 4 202103  -0.809622853 5 ...

Read More

How to filter single column of a matrix with column name in R?

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

To filter a single column of a matrix in R if the matrix has column names, we can simply use single square brackets but this will result in a vector without the column name. If we want to use the column name then column name or column number needs to be passed with drop=FALSE argument as shown in the below examples.Example1> M1 colnames(M1) M1Output      V1 V2 V3 V4 [1, ]  0  0  1  0 [2, ]  1  1  1  1 [3, ]  0  0  0  0 [4, ]  0  1  1  0 [5, ]  1  1  1  1 ...

Read More

How to find the row sum for each column by row name in an R matrix?

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

To find the row sum for each column by row name, we can use rowsum function. For example, if we have a matrix called M then the row sums for each column with row names can be calculated by using the command rowsum(M, row.names(M)).Example1> M1 rownames(M1) colnames(M1) M1Output       V1 V2 Male    3  6 Female  6  5 Female  7  3 Female  2  5 Female  5  3 Female  4  4 Female  1  4 Female  4  4 Female  7  5 Male    2  5 Female  5  5 Male    7  1 Female  5  6 Male    6  5 Female  3 ...

Read More
Showing 11201–11210 of 21,090 articles
Advertisements