How to return a logical value for a t test based on 0.05 level of significance in R?

R ProgrammingServer Side ProgrammingProgramming

To return a logical value for a t test based on 0.05 level of significance in R, we can follow the below steps −

  • First of all, create a data frame with one column.
  • Apply t.test function with ifelse to return logical value based on 0.05 level of significance.

Example1

Create the data frame

Let's create a data frame as shown below −

 Live Demo

x<-rnorm(20)
df1<-data.frame(x)
df1

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

      x
1 0.44038858
2 1.08938356
3 -0.23627885
4 0.73953751
5 -0.55476732
6 0.97726848
7 0.25612436
8 -1.89827676
9 0.50333232
10 0.55482166
11 1.83279952
12 0.93609228
13 0.35048901
14 0.05136088
15 -0.89102106
16 1.06392349
17 -0.15777431
18 0.45506977
19 1.43752763
20 1.27393923

Apply t.test to get the logical return

Using t.test with ifelse to return logical output based on significance level 0.05 for less than alternative −

 Live Demo

x<-rnorm(20)
df1<-data.frame(x)
ifelse(t.test(df1$x,mu=10,alternative="less")[["p.value"]]<0.05,"Yes","No")

Output

[1] "Yes"

Example 2

Create the data frame

Let's create a data frame as shown below −

 Live Demo

y<-rpois(20,5)
df2<-data.frame(y)
df2

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

  y
1 5
2 5
3 5
4 3
5 8
6 3
7 5
8 8
9 7
10 6
11 2
12 7
13 3
14 4
15 5
16 6
17 11
18 9
19 4
20 6

Apply t.test to get the logical return

Using t.test with ifelse to return logical output based on significance level 0.05 for greater than alternative −

 Live Demo

y<-rpois(20,5)
df2<-data.frame(y)
ifelse(t.test(df2$y,mu=10,alternative="greater")[["p.value"]]<0.05,"Yes","No")

Output

[1] "No"
raja
Published on 07-Aug-2021 09:59:01
Advertisements