0% found this document useful (0 votes)
9 views6 pages

R Console

The document provides an overview of basic inbuilt functions in R for statistical analysis, including calculations for mean, median, standard deviation, variance, and various statistical tests such as t-tests, ANOVA, and chi-square tests. It also includes examples of generating random samples and calculating probabilities using binomial and normal distributions. Additionally, it demonstrates how to summarize data and perform visualizations in R.

Uploaded by

kedarbhoir286
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

R Console

The document provides an overview of basic inbuilt functions in R for statistical analysis, including calculations for mean, median, standard deviation, variance, and various statistical tests such as t-tests, ANOVA, and chi-square tests. It also includes examples of generating random samples and calculating probabilities using binomial and normal distributions. Additionally, it demonstrates how to summarize data and perform visualizations in R.

Uploaded by

kedarbhoir286
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

> # Basic inbuilt functions in R

> x <- c(1, 2, 3, 4, 5)


>
> mean(x) # Mean of x
[1] 3
> median(x) # Median of x
[1] 3
> sd(x) # Standard deviation of x
[1] 1.581139
> var(x) # Variance of x
[1] 2.5
> summary(x) # Summary statistics
Min. 1st Qu. Median Mean 3rd Qu. Max.
1 2 3 3 4 5
> length(x) # Number of elements
[1] 5
>
> # Define the data
> marks <- c(25, 23, 21, 14, 18, 19, 15, 16, 12, 10)
>
> # Calculate required statistics
> max_value <- max(marks) # Maximum
> min_value <- min(marks) # Minimum
> length_value <- length(marks) # Length
> product_value <- prod(marks) # Product
> sum_value <- sum(marks) # Sum
> square_values <- marks^2 # Square of each value
> root_values <- sqrt(marks) # Square root of each value
> mean_value <- mean(marks) # Mean
> median_value <- median(marks) # Median
> sd_value <- sd(marks) # Standard Deviation
> variance_value <- var(marks) # Variance
> sorted_values <- sort(marks) # Sorted data
> summary_values <- summary(marks)# Summary statistics
>
> # Print results
> print(paste("Max:", max_value))
[1] "Max: 25"
> print(paste("Min:", min_value))
[1] "Min: 10"
> print(paste("Length:", length_value))
[1] "Length: 10"
> print(paste("Product:", product_value))
[1] "Product: 1665074880000"
> print(paste("Sum:", sum_value))
[1] "Sum: 173"
> print("Squares of values:")
[1] "Squares of values:"
> print(square_values)
[1] 625 529 441 196 324 361 225 256 144 100
> print("Square roots of values:")
[1] "Square roots of values:"
> print(root_values)
[1] 5.000000 4.795832 4.582576 3.741657 4.242641 4.358899 3.872983 4.000000
3.464102 3.162278
> print(paste("Mean:", mean_value))
[1] "Mean: 17.3"
> print(paste("Median:", median_value))
[1] "Median: 17"
> print(paste("Standard Deviation:", sd_value))
[1] "Standard Deviation: 4.80855718716355"
> print(paste("Variance:", variance_value))
[1] "Variance: 23.1222222222222"
> print("Sorted values:")
[1] "Sorted values:"
> print(sorted_values)
[1] 10 12 14 15 16 18 19 21 23 25
> print("Summary:")
[1] "Summary:"
> print(summary_values)
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.00 14.25 17.00 17.30 20.50 25.00
>
>
> # Define the data
> marks <- c(25, 23, 21, 14, 18, 19, 15, 16, 12, 10)
>
> # Calculate required statistics
> max_value <- max(marks) # Maximum
> min_value <- min(marks) # Minimum
> length_value <- length(marks) # Length
> product_value <- prod(marks) # Product
> sum_value <- sum(marks) # Sum
> square_values <- marks^2 # Square of each value
> root_values <- sqrt(marks) # Square root of each value
> mean_value <- mean(marks) # Mean
> median_value <- median(marks) # Median
> sd_value <- sd(marks) # Standard Deviation
> variance_value <- var(marks) # Variance
> sorted_values <- sort(marks) # Sorted data
> summary_values <- summary(marks)# Summary statistics
>
> # Print results
> print(paste("Max:", max_value))
[1] "Max: 25"
> print(paste("Min:", min_value))
[1] "Min: 10"
> print(paste("Length:", length_value))
[1] "Length: 10"
> print(paste("Product:", product_value))
[1] "Product: 1665074880000"
> print(paste("Sum:", sum_value))
[1] "Sum: 173"
> print("Squares of values:")
[1] "Squares of values:"
> print(square_values)
[1] 625 529 441 196 324 361 225 256 144 100
> print("Square roots of values:")
[1] "Square roots of values:"
> print(root_values)
[1] 5.000000 4.795832 4.582576 3.741657 4.242641 4.358899 3.872983 4.000000
3.464102 3.162278
> print(paste("Mean:", mean_value))
[1] "Mean: 17.3"
> print(paste("Median:", median_value))
[1] "Median: 17"
> print(paste("Standard Deviation:", sd_value))
[1] "Standard Deviation: 4.80855718716355"
> print(paste("Variance:", variance_value))
[1] "Variance: 23.1222222222222"
> print("Sorted values:")
[1] "Sorted values:"
> print(sorted_values)
[1] 10 12 14 15 16 18 19 21 23 25
> print("Summary:")
[1] "Summary:"
> print(summary_values)
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.00 14.25 17.00 17.30 20.50 25.00
> # 4704 Kedar Bhoir
>
> # Binomial distribution example
> n <- 10 # Number of trials
> p <- 0.5 # Probability of success
> x <- 0:10 # Possible outcomes
>
> # Probability Mass Function (PMF)
> dbinom(x, size = n, prob = p)
[1] 0.0009765625 0.0097656250 0.0439453125 0.1171875000 0.2050781250 0.2460937500
0.2050781250 0.1171875000 0.0439453125 0.0097656250 0.0009765625
>
> # Cumulative Distribution Function (CDF)
> pbinom(x, size = n, prob = p)
[1] 0.0009765625 0.0107421875 0.0546875000 0.1718750000 0.3769531250 0.6230468750
0.8281250000 0.9453125000 0.9892578125 0.9990234375 1.0000000000
>
> # Random sample from binomial distribution
> rbinom(5, size = n, prob = p)
[1] 9 8 5 3 4
>
> # 4704 Kedar Bhoir
>
> # Parameters for normal distribution
> mean_value <- 50
> sd_value <- 10
>
> # Probability Density Function (PDF)
> dnorm(50, mean = mean_value, sd = sd_value)
[1] 0.03989423
>
> # Cumulative Distribution Function (CDF)
> pnorm(50, mean = mean_value, sd = sd_value)
[1] 0.5
>
> # Generate random samples
> rnorm(10, mean = mean_value, sd = sd_value)
[1] 55.71194 40.43658 46.84347 42.85710 45.67247 57.95089 49.92067 44.53402
51.80006 63.57791
>
> # 4704 Kedar Bhoir
>
> # One Sample T-Test
> data <- c(15.7, 15.9, 16.3, 16.2, 15.7, 15.9)
> t.test(data, mu = 16)

One Sample t-test


data: data
t = -0.48795, df = 5, p-value = 0.6462
alternative hypothesis: true mean is not equal to 16
95 percent confidence interval:
15.68659 16.21341
sample estimates:
mean of x
15.95

>
> # Two Sample T-Test
> group1 <- c(12, 15, 11, 16, 14, 14, 16)
> group2 <- c(8, 10, 14, 10, 13)
> t.test(group1, group2)

Welch Two Sample t-test

data: group1 and group2


t = 2.2849, df = 7.3239, p-value = 0.05461
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.07699065 6.07699065
sample estimates:
mean of x mean of y
14 11

>
> # Paired T-Test
> before <- c(110, 120, 123, 132, 125)
> after <- c(120, 118, 125, 136, 121)
> t.test(before, after, paired = TRUE)

Paired t-test

data: before and after


t = -0.8165, df = 4, p-value = 0.4601
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
-8.800874 4.800874
sample estimates:
mean difference
-2

>
> # 4704 Kedar Bhoir
>
> # Z-Test
> library(BSDA)
Error in library(BSDA) : there is no package called ‘BSDA’
> z.test(x = 10.2, sigma.x = 2.25, n = 400, mu = 10)
Error in z.test(x = 10.2, sigma.x = 2.25, n = 400, mu = 10) :
could not find function "z.test"
>
> # Two Sample Z-Test
> z.test(x = 67.5, y = 68, sigma.x = 2.5, sigma.y = 2.5, n.x = 1000, n.y = 2000)
Error in z.test(x = 67.5, y = 68, sigma.x = 2.5, sigma.y = 2.5, n.x = 1000, :
could not find function "z.test"
>
> # 4704 Kedar Bhoir
>
> # Chi-Square Goodness of Fit Test
> observed <- c(16, 20, 25, 14, 29, 28)
> expected <- rep(132/6, 6)
> chisq.test(observed, p = expected/sum(expected))

Chi-squared test for given probabilities

data: observed
X-squared = 9, df = 5, p-value = 0.1091

>
> # Chi-Square Test for Independence
> data_matrix <- matrix(c(70, 50, 80, 20, 35, 45), nrow = 3, byrow = TRUE)
> chisq.test(data_matrix)

Pearson's Chi-squared test

data: data_matrix
X-squared = 25.646, df = 2, p-value = 2.698e-06

>
> # 4704 Kedar Bhoir
>
> # Mann-Whitney U Test
> x <- c(5.6, 4.6, 6.8, 4.9, 6.1, 5.3, 4.5, 5.8, 5.4)
> y <- c(7.2, 8.1, 5.1, 7.3, 6.9, 7.8, 5.9, 6.7, 6.5, 7.1)
> wilcox.test(x, y)

Wilcoxon rank sum exact test

data: x and y
W = 10, p-value = 0.002988
alternative hypothesis: true location shift is not equal to 0

>
> # Sign Test
> library(BSDA)
Error in library(BSDA) : there is no package called ‘BSDA’
> sign_test_data <- c(46, 49, 47, 55, 59, 63, 53, 56, 67, 54, 48, 69)
> SIGN.test(sign_test_data, md = 50)
Error in SIGN.test(sign_test_data, md = 50) :
could not find function "SIGN.test"
>
> # 4704 Kedar Bhoir
>
> # Kruskal-Wallis Test
> group_A <- c(1, 5, 8, 17, 16)
> group_B <- c(2, 1, 6, 5, 7, 4)
> group_C <- c(1, 1, 3, 7, 9)
> group_D <- c(2, 1, 5, 2, 9, 7)
>
> data_list <- list(A = group_A, B = group_B, C = group_C, D = group_D)
> kruskal.test(data_list)

Kruskal-Wallis rank sum test

data: data_list
Kruskal-Wallis chi-squared = 2.1699, df = 3, p-value = 0.5379
>
> # 4704 Kedar Bhoir
>
> # One-Way ANOVA
> drug_A <- c(4, 5, 4, 3, 2, 4, 3, 4, 4)
> drug_B <- c(6, 8, 4, 5, 4, 6, 5, 8, 6)
> drug_C <- c(6, 7, 6, 6, 7, 5, 6, 5, 5)
>
> data <- data.frame(
+ Response = c(drug_A, drug_B, drug_C),
+ Group = rep(c("A", "B", "C"), each = 9)
+ )
>
> anova_result <- aov(Response ~ Group, data = data)
> summary(anova_result)
Df Sum Sq Mean Sq F value Pr(>F)
Group 2 28.22 14.111 11.91 0.000256 ***
Residuals 24 28.44 1.185
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # 4704 Kedar Bhoir
>
> # Two-Way ANOVA
> sales_data <- data.frame(
+ Salesman = rep(c("A", "B", "C", "D"), times = 3),
+ Season = rep(c("Summer", "Winter", "Monsoon"), each = 4),
+ Sales = c(36, 32, 21, 30, 24, 25, 20, 22, 20, 18, 19, 15)
+ )
>
> two_way_anova <- aov(Sales ~ Salesman + Season, data = sales_data)
> summary(two_way_anova)
Df Sum Sq Mean Sq F value Pr(>F)
Salesman 3 77.67 25.89 2.162 0.19358
Season 2 279.50 139.75 11.673 0.00855 **
Residuals 6 71.83 11.97
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
>

You might also like