Programming Articles - Page 916 of 3363

How to change a text value in an R data frame?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:57:50

3K+ Views

To change a text value in an R data frame, we can simply use replace function.For Example, if we have a data frame called df that contains a column say Names and one of the names say Raj is misspelled as Raaj then we can replace Raaj with Raj by using the command given below −df$Names

How to find mathematical set using a data frame column in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:45:44

142 Views

A mathematical set is a collection of unique elements or a collection of elements that are different from each other. If we want to find a mathematical set using a data frame column then we can simply use unique function.For Example, if we have a data frame called df that contains a column say X then we can find the mathematical set using X with the help of below command −unique(df$X)Example 1Following snippet creates a sample data frame −x

How to change the color of gridlines of a ggplot2 graph in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:32:22

4K+ Views

To change the color of gridlines of a ggplot2 graph in R, we can use theme function with panel.grid.major and panel.grid.minor arguments where we can set the minor and major gridlines color of the plot panel to desired color.To understand how it can be done, check out the below Example.ExampleFollowing snippet creates a sample data frame −x

How to change the plot border color of a ggplot2 graph in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:25:46

5K+ Views

To change the plot border color of a ggplot2 graph in R, we can use theme function with panel.background argument where we can set the border of the plot panel using element_rect to desired color.To understand how it can be done, check out the below Example.ExampleFollowing snippet creates a sample data frame −x

Find the sum of a column values based on another numerical column in R.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:16:35

601 Views

To find the sum of a column values based on another numerical column in R, we can use with function and define the sum by subsetting the column with the help of single square brackets.For Example, if we have a data frame called df that contains two columns say X and Y then we can find the sum of values in X when Y is greater than 10 by using the command with the following −(df,sum(X[Y10]))Example 1Following snippet creates a sample data frame −x1

How to create horizontal line in xyplot in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:03:24

796 Views

To create horizontal line in xyplot, we can use abline function.For Example, if we have a data frame called df that contains two columns say X and Y and we want to create a scatterplot between X and Y using xyplot with a horizontal line at Y = 2 then we can use the command given below −xyplot(Y~X,df,abline=c(h=2))ExampleFollowing snippet creates a sample data frame −x

R Programming to find the column name for row maximum in a data.table object.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:49:43

466 Views

If we have a data.table object and we want to find the column name for row maximum then we can use max.col function.For Example, if we have a data.table object called DT then we can find the column name for row maximum by using the below command −DT[,names(.SD)[max.col(.SD,ties.method="first")]]Example 1Following snippet creates a sample data frame −x1

Remove rows from a data frame that exists in another data frame in R.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:34:33

6K+ Views

To remove rows from a data frame that exists in another data frame, we can use subsetting with single square brackets. This removal will help us to find the unique rows in the data frame based on the column of another data frame.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −x

Add values in sequence to the previous value with a constant.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:15:59

603 Views

To add values in sequence to the previous value with a constant, we can find the cumulative sum of values and add the constant to the Output.For Example, if we have a vector called X and we want to add values in X in sequence to the previous value with a constant say 10 then we can use the command given below −10+cumsum(X)Example 1Following snippet creates a sample data frame −x1

Create cross tabulation for three categorical columns in an R data frame.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:00:04

2K+ Views

To create cross tabulation for three categorical columns, we can use xtabs function. The xtabs function will create contingency table for each category in two columns and each contingency table will be created for the category in the third column.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −df1

Advertisements