• No products in the cart.

# 103.3.1 Basic Statistics, Graphs and Reports

##### Dive into Statistics with basics

This is the 3rd session of the R programming. The 1st session consisted of R introduction, how to code in R, how to compile the code, how to get the output. etc. The 2nd session was all about data handling, importing the data from various sources into R,merging different data, creating a new variable, filtering the data, how to take different  R data set & combine them to create the resultant data, exporting the data, etc. Session 3 is all about sampling, statistics, quartiles, percentiles, box plot, graphs, etc.

Sampling

Many a times we need only some part of the data or sample of the data or a subset of data, instead of the entire data set. For example, lets us consider the sales data or purchase orders data of last 20 years. We might not be interested in the whole 20 years data, we might only need the last 2 years data for the analysis. How do we take the sample. We take the dataset and use the sample function.

Syntax: sampleset<-dataset[sample(1:nrow(mydata),n),]

Let us consider the Online Retail data set.

>Retail_data<-read.csv("C:\\Amrita\\Datavedi\\Online Retail Sales Data\\Online Retail.csv")
>dim(Retail_data)

So there are 541909 rows and 8 columns. We don’t want all the 541909 rows, we want to take only the sample of 10000 rows out of this  Online Retail dataset

>Sample_set<-Retail_data[sample(1:nrow(Retail_data),10000), ]

This is the syntax for sampling the whole dataset for 1000 observations. Sample_set, is the new object created, into which we assign the new dataset of 10000 elements. Retail_data, is the original data, from which we extract  the sample 10000 elements. We need to give 2 parameters, i.e., 1 for the rows and 1 for the column. The row part will include “sample(1:nrow(Retail_data),10000)”, which means for taking random 1000 observations, all the rows, i.e., from 1 to n rows is considered  and the column part is left blank, as we need all the columns.

Instead of “sample(1:nrow(Retail_data),1000)”, we can also give as “sample(1:5000,10000)”, which will consider only the first 5000 rows from the original dataset for sampling the 10000 rows. If the syntax seems to be confusing, then we can simply write it as “sample(1:541909,10000)”, as we know there are total 541909 rows. The 1000 rows which we get from sampling will be randomly taken from the dataset.

Sampling in R

Let us consider the census income data that is there in the dataset folder.

>Income_data<-read.csv("C:\\Amrita\\Datavedi\\Census Income Data\\Income_data.csv")

The exercise is to take a sample dataset of 5000 records, from the dataset, Income-data which is a very large data set.

>dim(Income_data)
#32561 15

The Income_data consists of 32561 rows and 15 columns.

>sample<-Income_data[sample(1:nrow(Income_data),5000),]

The above command will store the 5000 records in the object sample, and if we check the dimension we get it as 50000 rows and 15 columns. This is how the sampling is done.

In next section, we will be studying about Descriptive Statistics.

20th June 2017

### 0 responses on "103.3.1 Basic Statistics, Graphs and Reports"

Statinfer Software Solutions LLP

Software Technology Parks of India,
NH16, Krishna Nagar, Benz Circle,
{"ft_lang_label":"__label__en","ft_lang_prob":0.83559054,"math_prob":0.9797791,"size":3043,"snap":"2020-34-2020-40","text_gpt3_token_len":773,"char_repetition_ratio":0.14478447,"word_repetition_ratio":0.0,"special_character_ratio":0.2734144,"punctuation_ratio":0.15070644,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9791536,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-24T08:25:44Z\",\"WARC-Record-ID\":\"<urn:uuid:fc330426-6f42-4a39-b931-eff0b615f068>\",\"Content-Length\":\"115978\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:029bfb63-08ac-4e5c-89e4-e0bcd67a38c5>\",\"WARC-Concurrent-To\":\"<urn:uuid:d21b1894-067d-434b-ab47-4fba326407e7>\",\"WARC-IP-Address\":\"139.59.6.51\",\"WARC-Target-URI\":\"https://statinfer.com/103-3-1-basic-statistics-graphs-and-reports/\",\"WARC-Payload-Digest\":\"sha1:UQFALG2OUNT2TFTFFHH655IGMQMRFEM3\",\"WARC-Block-Digest\":\"sha1:7B53SNGU46AYIMMT7VQKHKWYHTHZR7YV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400214347.17_warc_CC-MAIN-20200924065412-20200924095412-00484.warc.gz\"}"}