Found 2038 Articles for R Programming

How to find the frequency with subsetting in base R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:36:30

335 Views

The easiest way to find the frequency is to create a table for the provided vector or data frame column and it can be done with table function. But, if the frequency needs to be found with subsetting then subset function and transform function will also be required as shown in the below examples.Example 1Following snippet creates a sample data frame −head(ChickWeight, 20)OutputThe following dataframe is created − weight Time Chick Diet 1   42   0   1     1 2   51  2    1     1 3   59  4    1     1 4 ... Read More

How to create a block diagonal matrix using a matrix in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:24:46

2K+ Views

To create a block diagonal matrix using a matrix in R, we can use bdiag function of Matrix package.For Example, if we have a matrix called M and we want to create block diagonal using M 4 times by using the below command −bdiag(replicate(4,M,simplify=FALSE))Check out the Examples given below to understand how it can be done.Example 1Following snippet creates a sample matrix −M1

How to create a table with sub totals for every row and column in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:07:25

2K+ Views

The sub totals for every row and column are actually called marginal sums. Therefore, we can use addmargins function to our table to get the sub totals for every row and column.For example, if we have table called TABLE then we can add the sub totals by using the command given below −TABLE

How to find the sample size for two sample proportion tests with given power in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:01:21

693 Views

To find the sample size for two sample proportion tests with given power, we can use the function power.prop.test where we need to at least pass the two proportions and power.By default the significance level will be taken as 0.05 and if we want to change it then sig.level argument will be used.Given below are some examples with the display of significance levels.Example 1Use the code given below to find the sample size for two sample proportion tests −power.prop.test(p1=8/20, p2=6/20, power=0.90, sig.level=0.05)OutputIf you execute the above given snippet, it generates the following Output for the two-sample comparison of proportions power ... Read More

How to remove rows that have NA in R data frames stored in a list?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:58:36

866 Views

To remove rows that have NA in R data frames stored in a list, we can use lapply function along with na.omit function.For example, if we have a list called LIST that contains some data frames each containing few missing values then the removal of rows having missing values from these data frames can be done by using the command given below −lapply(LIST,na.omit)Check out the below given example to understand how it works.ExampleFollowing snippet creates a sample data frame −df1

How to create boxplots based on two factor data in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:52:32

2K+ Views

To create boxplots based on two factor data, we can create facets for one of the factors, where each facet will contain the boxplots for the second factor.For example, if we have a data frame called df that contains two factor columns say F1 and F2 and one numerical column say Num then the boxplot based on these two factors can be created by using the below given command −ggplot(df,aes(F1,Num))+geom_boxplot()+facet_wrap(~F2)ExampleFollowing snippet creates a sample data frame −Gender

How to create table of two factor columns in an R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:03:46

5K+ Views

To create table of two factor columns in an R data frame, we can use table function and with function.For Example, if we have a data frame called df that contains two factor columns say F1 and F2 then we can create table of these two columns by using the below command −with(df,table(F1,F2))Example 1Following snippet creates a sample data frame −Group

How to remove first character from column name in R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:53:53

3K+ Views

To remove first character from column name in R data frame, we can use str_sub function of stringr package.For Example, if we have a data frame called df that contains two columns say XID and XDV then we can remove X from both the column names by using the below mentioned command −names(df)=str_sub(names(df),2)Example 1Following snippet creates a sample data frame −XColor

How to increase the width of the X-axis line for a ggplot2 graph?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:38:19

4K+ Views

To increase the width of the X-axis line for a ggplot2 graph in R, we can use theme function where we can set the axis.line.x.bottom argument size to desired size with element_line.Check out the below Example to understand how it can be done.ExampleFollowing snippet creates a sample data frame −x

Change the color of X-axis line for a graph using ggplot2.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:32:41

6K+ Views

To change the color of X-axis line for a graph using ggplot2, we can use theme function where we can set the axis.line.x.bottom argument color to desired color with element_line.Check out the below Example to understand how it can be done. This might be required when we want to highlight the X-axis for viewers.ExampleFollowing snippet creates a sample data frame −x

Advertisements