Obtaining mean from data frame and matrix


This is an explanation about the conversion of given values of data into data frame and matrix and obtaining mean from them.

Name of the candidates outlined in the data frame is Jeb, Donald, Ted, Marco, Carly, Hillary and Bernie. Sources of the polls here are ABC and CBS.

Data of the names and political results of both polls are added to the environment.

> Name <- c("Jeb", "Donald", "Ted", "Marco", "Carly", "Hillary", "Berine")
> ABC_political_poll_results <- c(4, 62, 51, 21, 2, 14, 15)
> CBS_political_poll_results <- c(12, 75, 43, 19, 1, 21, 19)

By applying ‘cbind’ to the values, merged data with all three columns of the values into data 'poll_results' is obtained.

> poll_results<-cbind(Name,ABC_political_poll_results,CBS_political_poll_results)
> poll_results
     Name      ABC_political_poll_results CBS_political_poll_results
[1,] "Jeb"     "4"                        "12"                     
[2,] "Donald"  "62"                       "75"                     
[3,] "Ted"     "51"                       "43"                     
[4,] "Marco"   "21"                       "19"                     
[5,] "Carly"   "2"                        "1"                      
[6,] "Hillary" "14"                       "21"                     
[7,] "Berine"  "15"                       "19"                     

Following, a data frame ‘poll_results.df’ is created by using the following code

> poll_results.df<- data.frame(Name,ABC_political_poll_results,CBS_political_poll_results)
> poll_results.df
     Name ABC_political_poll_results CBS_political_poll_results
1     Jeb                          4                         12
2  Donald                         62                         75
3     Ted                         51                         43
4   Marco                         21                         19
5   Carly                          2                          1
6 Hillary                         14                         21
7  Berine                         15                         19

With ‘colMeans’ implemented in both poll results, mean for ABC political poll results as 24.14 and CBS political poll results as 27.14 is obtained.

> colMeans(poll_results.df[, 2:3])
ABC_political_poll_results CBS_political_poll_results
                  24.14286                   27.14286

After this, the code to convert the data frame to the matrix is executed.

> mat<-as.matrix(poll_results.df)
> mat

In order to get the means of overall poll results from the matrix, ‘new_mat’ is created from the matrix ‘mat’, which contains now only poll results of ABC and CBS.

> new_mat=matrix(as.numeric(mat[,2:3]))
> mean(new_mat)
[1] 25.64286
 

The Mean of the ABC political poll results is 24.14286.

The Mean of the CBS political poll results is 27.14286.

The overall Mean of the poll results is 25.64286.

The Mean of the CBS political poll results is high compared to ABC political poll results. 

In this way, we can obtain mean from data by using codes for data frames and matrices.

URL to git repo:https://github.com/VedaVangala/vedas-r-repo/tree/main/R3


Comments

Popular posts from this blog

Input/Output, String manipulation and 'plyr' package in R

PACKAGE "ACCURACY"

Visualization of Graphics in R