Dummy and Contrast Codings in R

 “Dummy” or “treatment” coding is to create dichotomous variables where each level of the categorical variable is contrasted to a specified reference level. Basic Syntax of Dummy and Contrast Coding 1. Dummy Coding The following is the syntax to do dummy coding in R. contr.treatment( number_of_level_of_X ) 2 3 1 0 0 2 1 0 3 … Read more

How to Read SPSS Files in R

You can use the read_sav() function from the haven library to read SPSS files in R. The syntax of the function is as follows. read_sav(“file_name.sav”) Step 1: Install haven library The first step is to install the library of “haven” into your local computer. install.packages(‘haven’) Step 2: Library it in R Studio As for other libraries, you need … Read more

Two Sample t-test in R (2 Examples)

This tutorial shows how you can two sample t-test in R. Note that, Two sample t-test is also called independent sample t-test or unpaired sample t-test.  Method 1: Vector format t.test(vector_1, vector_2, var.equal = TRUE) Method 2: Data frame format t.test(Y ~ group, data = df_name , var.equal = TRUE) Data and Hypothesis Suppose you … Read more

Categories R

One Sample t-test in R

The following is the core R syntax to do one sample t-test in R. In particular, Method 1 uses built-in R function, whereas method 2 writes the function to test one sample t-test in R from scratch. Method 1: t.test(vector_name, mu = value_to_compare, alternative = “two.sided”) Method 2: (mean(vector_name)-value_to_compare)/(sd(vector_name)/sqrt(number_of_observation)) Data Example for One Sample t-test … Read more

rep() Function in R (4 Examples)

rep() is a built-in R function that replicates the values in the provided vector. This tutorial shows how to use rep() function in R with 4 examples. Example 1 The following code is to repeat the number 4 twice. Output: [1] 4 4 Example 2 Output: [1] 1 2 3 1 2 3 1 2 3 … Read more

Categories R

Paired t-test in R (2 Examples)

Method 1 for paired t-test is for a situation group 1 and group 2 are two separate vectors, whereas Method 2 is for two groups of data in the same column. Method 1: t.test(group_1, group_2, paired = TRUE) Method 2: t.test(y~group, data=my_data, paired = TRUE) Data and Hypothesis Suppose we would like to test whether … Read more

Categories R