Programming Articles - Page 892 of 3363

How to remove dot at last position from every value in R data frame column?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:28:15

1K+ Views

To remove dot at last position from every value in R data frame column, we can follow the below steps −First of all, create a data frame with a column having dot at last position in every value.Then, use gsub function to remove the dot at last position from every value in the column.ExampleCreate the data frameLet’s create a data frame as shown below −x

Apply a mathematical operation on rows of an R data frame stored in a list.

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:28:31

1K+ Views

To apply a mathematical operation such as multiplication, division, subtraction and addition on rows of an R data frame stored in a list, we can use apply function.For Example, if we have a list called LIST that contains a data frame df with two columns say X and Y and we want to add rows of df then we can use the command mentioned below −apply(df,1,function(x) x["X"]+x["Y"])Check out the Example given below to understand how it can be done.ExampleFollowing snippet creates a sample data frames: −x1

How to find the position of minimum of each numerical column if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:25:43

135 Views

To find the position of minimum of each numerical column if some columns are categorical in R data frame, we can follow the below steps −First of all, create a data frame.Then, use numcolwise function from plyr package to find the minimum of each numerical column if some columns are categorical.Example 1Create the data frameLet’s create a data frame as shown below −factor1

How change the color of facet title using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:22:07

772 Views

To change the color of facet title using ggplot2 in R, we can use theme function with strip.text.x. argument.For Example, if we have a data frame called df that contains three columns say X, Y and F where F is a factor column then we can create facetted scatterplots between X and Y for values in F having different colored facet title by using the below mentioned command with facet title in different color −ggplot(df, aes(X, Y))+geom_point()+facet_wrap(~F)+theme(strip.text.x=element_text(colour="red"))ExampleFollowing snippet creates a sample data frame −PriceRead More

How to multiply corresponding values from two matrices in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:19:48

291 Views

To multiply corresponding values from two matrices in R, we can follow the below steps −First of all, create two matrices.Then, use mapply function to multiply corresponding values from those two matrices.ExampleCreate the first matrixLet’s create a matrix as shown below −M1

Increase the space between facets in a facetted plot created using ggplot2 in R.

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:17:43

4K+ Views

To increase the space between facets in a facetted plot created by using ggplot2 in R, we can use theme function with panel.spacing argument.For Example, if we have a data frame called df that contains three columns say X, Y and F where F is a factor column then we can create facetted scatterplots having facets spaced at a larger distance between X and Y for values in F by using the command given below −ggplot(df, aes(X, Y))+geom_point()+facet_wrap(~F)+theme(panel.spacing=unit(1, "lines"))ExampleFollowing snippet creates a sample data frame −IVRead More

How to multiply corresponding row values in a data.table object with single row data.table object in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:13:14

478 Views

To multiply row values in a data.table object having multiple rows with single row data.table object in R, we can follow the below steps −First of all, create a data.table object with multiple rows and a data.table object with single row.Then, use mapply function to multiply row values in those objects.ExampleCreate the first data.table objectLet’s create a data.table object as shown below −library(data.table) x1

How to change the sign of even number rows in column of a data.table object in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:10:11

151 Views

To change the sign of even number rows in a data.table object in R, we can follow the below steps −First of all, create a data.table object.Then, use vector multiplication with 1 and minus 1 to change the sign of even number rows.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) x

Change the color of legend element border using ggplot2 in R.

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:10:47

4K+ Views

To change the color legend element border using ggplot2, we can use theme function where can put color in legend.key argument to desired color with element_rect.For Example, if we have a data frame called df that contains three columns say X and Y and F where X and Y are numerical and F is categorical then we can create scatterplot between X and Y with blue color of legend element border by using the command given below −ggplot(df, aes(IV, DV))+geom_point(aes(colour=factor(Class)))+theme(legend.key=element_rect(colour="red"))ExampleFollowing snippet creates a sample data frame −IVRead More

How to find the row variance of columns having same name in R matrix?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:05:15

166 Views

To find the row variance of columns having same name in R matrix, we can follow the below steps −First of all, create a matrix with some columns having same name.Then, use tapply along with colnames and var function to find the row variance of columns having same name.ExampleCreate the matrixLet’s create a matrix as shown below −M

Advertisements