R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Step:::1

Load Library

…………First of all we have to run libraries………………..

library(tidyverse)
## -- Attaching packages ---------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.5
## v tidyr   1.0.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(datasets)

Step:::2 ……..we run data from datasets library and check the structure of dataset. ………..Here the worldphone dataset represent a matrices.

Load Data

WorldPhones
##      N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
## 1951  45939  21574 2876   1815    1646     89      555
## 1956  60423  29990 4708   2568    2366   1411      733
## 1957  64721  32510 5230   2695    2526   1546      773
## 1958  68484  35218 6662   2845    2691   1663      836
## 1959  71799  37598 6856   3000    2868   1769      911
## 1960  76036  40341 8220   3145    3054   1905     1008
## 1961  79831  43173 9053   3338    3224   2005     1076
str(WorldPhones)
##  num [1:7, 1:7] 45939 60423 64721 68484 71799 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : chr [1:7] "1951" "1956" "1957" "1958" ...
##   ..$ : chr [1:7] "N.Amer" "Europe" "Asia" "S.Amer" ...
is.matrix(WorldPhones)
## [1] TRUE

Step:::3 ……..Since Worldphone dataset represent a matrics. So I want to convert it into dataframe. ……….So, For this we can do,

#create a variable
 year <- c('1951','1956','1957','1958','1959','1960','1961')
      w1 <- WorldPhones%>%as_tibble()%>%rownames_to_column(var = 'Year')
        w1 <- cbind(w1,year)%>%select(9:2)
w1          
##   year Mid.Amer Africa Oceania S.Amer Asia Europe N.Amer
## 1 1951      555     89    1646   1815 2876  21574  45939
## 2 1956      733   1411    2366   2568 4708  29990  60423
## 3 1957      773   1546    2526   2695 5230  32510  64721
## 4 1958      836   1663    2691   2845 6662  35218  68484
## 5 1959      911   1769    2868   3000 6856  37598  71799
## 6 1960     1008   1905    3054   3145 8220  40341  76036
## 7 1961     1076   2005    3224   3338 9053  43173  79831

ii.. ………….Here we will see worldphone datasets easily convert to dataframe. ……………..very interesting!!! isn’t it?

year <- c('1951','1956','1957','1958','1959','1960','1961')
    w2 <- data.frame(WorldPhones)%>%rownames_to_column('Year')
w2
##   Year N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
## 1 1951  45939  21574 2876   1815    1646     89      555
## 2 1956  60423  29990 4708   2568    2366   1411      733
## 3 1957  64721  32510 5230   2695    2526   1546      773
## 4 1958  68484  35218 6662   2845    2691   1663      836
## 5 1959  71799  37598 6856   3000    2868   1769      911
## 6 1960  76036  40341 8220   3145    3054   1905     1008
## 7 1961  79831  43173 9053   3338    3224   2005     1076

……..this method almost same with method(ii) but have little bit different….and we see that exactly same with worldphone data. just rows are convert to coloumn with a variable (Year). It’s magic of code,isn’t it?

……..in method(ii) we use data.frame but in method(iii) use as.data.frame()..But why? ……..Because data.frame() can be used to build a data frame while as.data.frame() can only be used to coerce other object to a data frame.

w3 <- as.data.frame(WorldPhones)%>%rownames_to_column('Year')
w3
##   Year N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
## 1 1951  45939  21574 2876   1815    1646     89      555
## 2 1956  60423  29990 4708   2568    2366   1411      733
## 3 1957  64721  32510 5230   2695    2526   1546      773
## 4 1958  68484  35218 6662   2845    2691   1663      836
## 5 1959  71799  37598 6856   3000    2868   1769      911
## 6 1960  76036  40341 8220   3145    3054   1905     1008
## 7 1961  79831  43173 9053   3338    3224   2005     1076