R Programming Articles

Page 136 of 174

How to find the row means for each matrix stored in an R list?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 402 Views

To find the row mean of all matrices stored in an R list, we can use sapply function along with rowMeans function.For example, if we have a list called LIST that contains some matrices then the row means for each matrix can be found by using the following command −sapply(LIST,rowMeans)Check out the below example to understand how it works.ExampleFollowing snippet creates the matrices −M1

Read More

How to find the median of frequency data in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 2K+ Views

If we have frequency data then we first need to find the total data or complete data by repeating the values up to the frequency corresponding to each value after that we can apply median function on this complete data.For Example, if we have a data frame called df that contains two columns say X and Frequency then we can find the total data by using the following command −Total_data

Read More

How to find the sample size for t test in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 4K+ Views

To find the sample size for t test, we can use pwr.t.test function of pwr package, wherever we can pass the arguments for alternative hypothesis such as one-sided or two-sided, significance level, power of the test and difference for two samples.Check out the below examples to understand how it works.Example 1Consider the following code to find sample size for t test −library("pwr") pwr.t.test(power=0.80, d=1, sig.level=0.05, alternative="two.sided")OutputIf you execute the above given code, it generates the following Output for the two-sample t test power calculation −   n = 16.71472    d = 1 sig.level = 0.05    power = 0.8 alternative ...

Read More

How to make duplicate factor levels unique in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 2K+ Views

The factor with duplicate levels represents grouping data but if we want to convert the grouping data into nominal data then duplicate values must be removed or converted into unique values. To make duplicate factor levels unique in an R data frame, we can use make.unique function.Check out the below Examples to understand how it works.Example 1Following snippet creates a sample data frame −Factor

Read More

How to divide each value in an R data frame by 100?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 4K+ Views

We sometimes need to perform mathematical operations on all values in the data set. One such operation could be dividing each value by 100.For example, if we have a data frame called df then we can divide each value in df by 100 by using the below command −df[,1:ncol(df)]/100ExampleFollowing snippet creates a sample data frame −x1

Read More

How to find the groupwise common value for a data.table object?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 219 Views

To find the groupwise common value for a data.table object, we can use Reduce function with intersect function.For example, if we have a data.table object called DT that contains a numerical column say Num and a categorical column say C where C exists at the first position then the groupwise common value can be found by using the command given below −Reduce(intersect,DT[,.(list(unique(Num))),C]$V1)ExampleConsider the below data.table object −Group

Read More

Find the frequency of exclusive group combinations based on multiple categorical columns in R.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 631 Views

To find the frequency of exclusive group combinations in an R data frame, we can use count function of dplyr package along with ungroup function.For Example, if we have a data frame called df that contains four grouping columns say Grp1, Grp2, Grp3, and Grp4 then we can count the unique group combinations in df by using the below command −count(df,Grp1,Grp2,Grp3,Grp4)%%ungroup()Example 1Following snippet creates a sample data frame −Class1

Read More

How to display superscript for X-axis title in base R plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 1K+ Views

To display superscript for X-axis title in base R plot, we can use ^ sign inside mtext function before defining the plain text.For example, if we want to display X2 at position 5 on X-axis then it can be done by using the below command −mtext(expression(paste(plain("X")^plain("2"))), side=1, line=2, at=5, cex=1.2)ExampleConsider the following snippet −plot(1:10) OutputIf you execute the above given snippet, it generates the following Output −To display superscript for X-axis title in base R plot, add the following code to the above snippet −Exampleplot(1:10, xlab="")OutputIf you execute all the above given snippets as a single program, it generates the ...

Read More

How to change the order of independent variables for regression summary output in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 757 Views

To change the order of independent variables in regression Output, we can pass the variables in the sequence we want while creating the regression model.For example, if we want to have three independent variables and we want to display first at the last position then it can be done as follows −lm(DP1~ ind_var_3+ ind_var_2+ind_var_1,data=”data_frame_name”)ExampleFollowing snippet creates a sample dataframe −iv1

Read More

How to check matrix values equality with a vector values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 266 Views

If we have a vector called V that contains five values and a matrix say M that contains five columns and we want to check whether first value in the vector is present in the first column of each row in the matrix and so on for each value in the vector then we can use the below command −t(t(M)==V)Example 1Consider the below matrix and vector −M1

Read More
Showing 1351–1360 of 1,740 articles
« Prev 1 134 135 136 137 138 174 Next »
Advertisements