0% found this document useful (0 votes)
2 views8 pages

Practical 01 - Introduction to R

This document serves as an introduction to using R for data entry and statistical analysis, focusing on importing data, recoding variables, and performing basic computations. It provides step-by-step instructions for entering data from Excel, creating new variables, and selecting specific cases for analysis. Additionally, it covers generating random numbers and calculating descriptive statistics to summarize the data effectively.

Uploaded by

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

Practical 01 - Introduction to R

This document serves as an introduction to using R for data entry and statistical analysis, focusing on importing data, recoding variables, and performing basic computations. It provides step-by-step instructions for entering data from Excel, creating new variables, and selecting specific cases for analysis. Additionally, it covers generating random numbers and calculating descriptive statistics to summarize the data effectively.

Uploaded by

laseya.greenhill
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/15/24, 10:04 AM Lecture 1: Introduction and Data Entry in R

For this module we are going to familiarise ourselves with the programming language R. R is a widely
used, free software environment designed specifically for statistics and data analysis. It is pretty
intuitive and easy to learn.

Using R, we will implement introductory statistical methods on some sample datasets. Firstly, let’s look
at a few simple recipes for entering or importing data from a previously created file.

Data entry

It is possible to ‘copy and paste’ data into R. This can be done by opening an Excel file, for example,
selecting the data of interest and copying it onto the clipboard (ctrl-C). Then, typing the following at
the R command line will paste it into R:

data_file <- [Link]("clipboard",header=TRUE)

data_file here is the name of the R file (it could be any) which stores the data read from the clipboard.
To see what was copied to R, simply type the name of your file

data_file

or click on it in the Environment window. Functions in R are precoded files which manipulate data. To
get a description of a function and what options are available to use, type a question mark followed by
the function name, e.g. ‘?[Link]’.

Although not advisable for general use, reading from the clipboard is handy for one-off uses when you
are in a hurry.

For general use, however, especially when dealing with large quantities of data, it is much more
efficient to read directly from the file. We will look at how to do this in the following example.

Example. In a study of the effectiveness of drug X on weight loss in obese men and women of various
ages, all of the values for initial weight and final weight as well as gender (male/female) and age are
recorded:

Table 1. Data on the effectiveness of drug X on weight


loss
Subject Initial_weight Final_weight Gender Age
1 95.3 86.2 female 42
2 87.5 85.1 female 53
3 102.4 103.3 male 52
4 98.6 91.4 female 36
5 93.4 81.2 male 29
6 105.1 97.1 female 62
7 95.1 91.2 female 58
8 87.7 81.1 female 63
9 104.3 87.2 male 48
10 128.4 120.8 male 54
11 85.9 82.3 female 48
12 91.7 85.2 female 59
13 94.7 91.6 male 43

[Link] 1/8
2/15/24, 10:04 AM Lecture 1: Introduction and Data Entry in R

14 104.3 104.4 male 56


15 111.2 103.5 male 61
16 101.8 96.8 male 59

Now do the following:

copy and paste this table into a new Excel file


save the file as a ‘csv’ file with the name ‘Weight_loss_data’ in a new folder called ‘StatsData’
set the working directory of R to this new folder as follows:

setwd("H://StatsData") (on Windows)

setwd('~/Documents/StatsData') (on Mac OS/Linux)

read this file into R by typing the following into the command line:

data_file <- [Link]("weight_loss_data.csv", header = TRUE)

Recoding data
You might decide to simplify the age variable into a set of age ranges and save as a new
variable in column [Link] example, let’s say category 1 is for those younger than 30, category 2. for
those between 30 and 39, category 3 between 40 and 49, category 4 between 50 and 59, and
category 5 for those 60 and over. To do this, first we can create a list of the brackets between
categories as a sequence from 20 up to 70 in steps of 10 (which includes all ages):

deciles <- seq(20, 70, by = 10)

Then, we can use the ‘cut’ function to categorise the ages of participants based on these brackets as
follows:

cuts <- cut(data_file$Age, breaks = deciles)

The output of ‘cuts’ should look like this:

cuts
## [1] (40,50] (50,60] (50,60] (30,40] (20,30] (60,70] (50,60] (60,70]
## [9] (40,50] (50,60] (40,50] (50,60] (40,50] (50,60] (60,70] (50,60]
## Levels: (20,30] (30,40] (40,50] (50,60] (60,70]

The first two lines show the age bracket for each subject and the final line shows the possible age
brackets.

Now, we can create a new column for data_file called ‘Age_category’, and write who belongs to what
category with the following:

data_file$Age_category <- rep(0,16) # number of cases = 16


data_file$Age_category[which(cuts==levels(cuts)[1])] <- 1

data_file$Age_category[which(cuts==levels(cuts)[5])] <- 5

The first line creates the new Age_category column in data_file, initialising all entries as 0s. The next
line then finds all participants in the first category and assigns a 1 to them in the Age_category
column. The same can then be done for categories 2 to 5. Note, the square brackets are used for data

[Link] 2/8
2/15/24, 10:04 AM Lecture 1: Introduction and Data Entry in R

to work on entries of interest, while the round brackets are used for functions to define arguments and
options of the function.

To see the results, type

data_file

## Subject Initial_weight Final_weight Gender Age X Age_category


## 1 1 95.3 86.2 female 42 NA 3
## 2 2 87.5 85.1 female 53 NA 4
## 3 3 102.4 103.3 male 52 NA 4
## 4 4 98.6 91.4 female 36 NA 2
## 5 5 93.4 81.2 male 29 NA 1
## 6 6 105.1 97.1 female 62 NA 5
## 7 7 95.1 91.2 female 58 NA 4
## 8 8 87.7 81.1 female 63 NA 5
## 9 9 104.3 87.2 male 48 NA 3
## 10 10 128.4 120.8 male 54 NA 4
## 11 11 85.9 82.3 female 48 NA 3
## 12 12 91.7 85.2 female 59 NA 4
## 13 13 94.7 91.6 male 43 NA 3
## 14 14 104.3 104.4 male 56 NA 4
## 15 15 111.2 103.5 male 61 NA 5
## 16 16 101.8 96.8 male 59 NA 4

Computing new variables

In some experiments you may want to carry out some mathematical manipulation of a variable to
create a new variable. In the previous example, we had the initial weights and final weights, but what
we really want are the weight losses. To get these we calculate the weight losses as the difference
between the initial and final weights and write these under the new column weight_loss:

data_file$Weight_loss <- data_file$Initial_weight - data_file$Final_weight

Selecting Cases
If you want to select only certain subjects for your analysis, e.g. only males, you can do this using the
following command:

data_file_male <- data_file[data_file$Gender == "male",]

data_file_male

Note the trailing comma is necessary for this to work. It tells R to subset the data by rows rather than
columns.

## Subject Initial_weight Final_weight Gender Age X Age_category


## 3 3 102.4 103.3 male 52 NA 4
## 5 5 93.4 81.2 male 29 NA 1
## 9 9 104.3 87.2 male 48 NA 3
## 10 10 128.4 120.8 male 54 NA 4
## 13 13 94.7 91.6 male 43 NA 3
## 14 14 104.3 104.4 male 56 NA 4
## 15 15 111.2 103.5 male 61 NA 5

[Link] 3/8
2/15/24, 10:04 AM Lecture 1: Introduction and Data Entry in R

## 16 16 101.8 96.8 male 59 NA 4


## Weight_loss
## 3 -0.9
## 5 12.2
## 9 17.1
## 10 7.6
## 13 3.1
## 14 -0.1
## 15 7.7
## 16 5.0

Generating random numbers


Randomness is a very important concept in statistics. In programming, randomness is usually
modelled by pseudorandomness. This relies on an algorithm to generate numbers which would
appear to be generated at random, but which are actually deterministic and could be reproduced given
knowledge of the algorithm. Nevertheless, for most practical purposes using pseudorandom numbers
is indistinguishable to using truly random numbers.

Let’s imitate the National Lottery and select numbers randomly from 1 to 49. To do this we use the
sample function:

RS<- sample(1:49, size = 6)


RS

## [1] 44 40 11 10 43 48

Another useful function is the ‘sort’ function which sorts our data as we want. The default for numbers
is to sort numerically in ascending order (it can also sort letters). To sort these 6 numbers simply type:

sort(RS)

[1] 10 11 40 43 44 48

Descriptive Statistics

Let’s continue with the previous example. In a study of the effectiveness of drug X on weight loss in
obese men and women of various ages, the values for initial weight, final weight, gender and age are
recorded. We then learned how to calculate the new variable for weight_loss in R. The data now
looks like this:

Table 2. Data on the effectiveness of drug X with weight loss


column
Subject Initial Final Gender Age Weight
weight weight (y) loss
(kg) (kg) (kg)
1 95.3 86.2 female 42 9.1
2 87.5 85.1 female 53 2.4
3 102.4 103.3 male 52 -0.9
4 98.6 91.4 female 36 7.2
5 93.4 81.2 male 29 12.2
6 105.1 97.1 female 62 8.0

[Link] 4/8
2/15/24, 10:04 AM Lecture 1: Introduction and Data Entry in R

7 95.1 91.2 female 58 3.9


8 87.7 81.1 female 63 6.6
9 104.3 87.2 male 48 17.1
10 128.4 120.8 male 54 7.6
11 85.9 82.3 female 48 3.6
12 91.7 85.2 female 59 6.5
13 94.7 91.6 male 43 3.1
14 104.3 104.4 male 56 -0.1
15 111.2 103.5 male 61 7.7
16 101.8 96.8 male 59 5.0

To start to understand this data it is helpful to have some ways of ordering and summarising it. We call
this ‘Descriptive Statistics’.

First, note the number of subjects, which we will denote generally as n. In this case for the weight loss
trial, n = 16. It is often useful to arrange the data so the main variable of interest, weight_loss in this
case, is in order from smallest to largest:

Table 3. Data on the effectiveness of drug X ordered by weight loss


Gender Age Weight
(y) loss
Subject Initial Final (kg)

weight weight

(kg) (kg)

3 102.4 103.3 male 52 -0.9


14 104.3 104.4 male 56 -0.1
2 87.5 85.1 female 53 2.4
13 94.7 91.6 male 43 3.1
11 85.9 82.3 female 48 3.6
7 95.1 91.2 female 58 3.9
16 101.8 96.8 male 59 5.0
12 91.7 85.2 female 59 6.5
8 87.7 81.1 female 63 6.6
4 98.6 91.4 female 36 7.2
10 128.4 120.8 male 54 7.6
15 111.2 103.5 male 61 7.7
6 105.1 97.1 female 62 8.0
1 95.3 86.2 female 42 9.1
5 93.4 81.2 male 29 12.2
9 104.3 87.2 male 48 17.1

This immediately enables us to see the range of values for weight loss, 17.1 – (-0.9) = 18.0.

[Link] 5/8
2/15/24, 10:04 AM Lecture 1: Introduction and Data Entry in R

With R, we do not need to sort the data in any way to calculate this. We can do it through the
predefined functions ‘max’ and ‘min’, which will return the maximum and minimum values, respectively,
from your data.

attach(data_file) ## from now on you do not need to use data_file$


range_Weight_loss <- max(Weight_loss)-min(Weight_loss)
range_Weight_loss

(If you continue working with one file, you don’t have to use $ each time when you call a variable, the function
‘attach’ allows you to call column names of your data as if they were separate variables).

However, we still don’t know how these values are distributed across the range- are they all at one
end, or bunched in the middle? What is a typical value? To look at the distribution across the range we
need to construct a frequency distribution.

To do this we can divide the range of values for weight loss into equally spaced bins ranging from the
lowest to the highest value. Then we can count the frequency of values falling within each bin. There
is no rule to decide on the number of bins, although having at least one class with at least 4 values in
it is advisable, otherwise there is probably not much point in doing it.

Here eight bins are chosen manually, not using any software:

Table 4. Weight loss frequency table

Bin Bin limits of weight loss Frequency

1 -1.25-1.25 2
2 1.25-3.75 3
3 3.75-6.25 2
4 6.25-8.75 6
5 8.75-11.25 1
6 11.25-13.75 1
7 13.75-16.25 0
8 16.25-18.75 1

We can see that the spread is uneven and that the bin with the highest frequency is bin 4.

To better understand the R language, let’s go through this process in R. First, we need to define the 8
bins. For this, we can use the function ‘seq’ which allows us to define sequences of numbers spaced
equally. We need three parameters for this sequence, the lowest value, the highest value and either
the number of bins (which will automatically equally space bins between these values) or the length of
one bin (if you want to specify precisely the bin edges). We’ll do the latter:

wl_edges <- seq(-1.25,18.75,by = 2.5)

Now we can use the function ‘cut’ to assign each value of weight loss to a bin:

wl_cats <- cut(Weight_loss, breaks = wl_edges)


wl_cats

to obtain

[Link] 6/8
2/15/24, 10:04 AM Lecture 1: Introduction and Data Entry in R

## [1] (8.75,11.2] (1.25,3.75] (-1.25,1.25] (6.25,8.75] (11.2,13.8]


## [6] (6.25,8.75] (3.75,6.25] (6.25,8.75] (16.2,18.8] (6.25,8.75]
## [11] (1.25,3.75] (6.25,8.75] (1.25,3.75] (-1.25,1.25] (6.25,8.75]
## [16] (3.75,6.25]
## 8 Levels: (-1.25,1.25] (1.25,3.75] (3.75,6.25] ... (16.2,18.8]

To count the frequencies of each bin, we run the following command

table(wl_cats)

to obtain

## wl_cats
## (-1.25,1.25] (1.25,3.75] (3.75,6.25] (6.25,8.75] (8.75,11.2]
## 2 3 2 6 1
## (11.2,13.8] (13.8,16.2] (16.2,18.8]
## 1 0 1

Alternatively, and more efficiently, we can get a much better view of the distribution by representing the
frequency distribution as a histogram as follows:

hist(Weight_loss, breaks = wl_edges)

[Link] 7/8
2/15/24, 10:04 AM Lecture 1: Introduction and Data Entry in R

You should be aware that for small datasets histograms for the same data can look quite different
depending on the selected bin limits.

[Link] 8/8

You might also like