<- read.table("TB_stats.txt", header=TRUE) myTBdata
P01. Introduction to R, part 2
A. read in a data file
In order to run this, your computer need to know where “TB_stats.txt” is. You could download it here.
What does the “header=TRUE” option mean?
Answer:
B. Have a look at the first few lines
# Now let's investigate the data file
head(myTBdata)
How many rows can you see? What is the first row?
Answer:
C. What are the names of the columns?
names(myTBdata)
Is this what you expected?
Answer:
D. How many rows and columns are there in your data ?
dim(myTBdata)
What is the first number telling you? And the second?
Answer:
E. How are your data stored?
attributes(myTBdata)
What new piece of information have you learned from the ‘attributes()’ function?
Answer:
F. Now take a look at some summary statistics for your data
summary(myTBdata)
Let’s extract some information from our data
G. First, Calculate the total number of deaths across all countries.
The following two methods should give you the same answer
<- sum(myTBdata[,2:3]) # method 1
total_TB_mortality1 <- sum(myTBdata$HIV_pos_TB_mortality + myTBdata$HIV_neg_TB_mortality) # method 2 total_TB_mortality2
Do you think one method is better than the other?
Answer:
H. Now let’s check that both methods give the same answer.
We’ll use two ways to check this. First, let’s output both answers
total_TB_mortality1 total_TB_mortality2
Now, let’s ask R to check whether they are both equal
==total_TB_mortality2
total_TB_mortality1# logical expression which gives TRUE if equal and FALSE if not
Why might you prefer to use the second check (using the logical expression) than the first?
Answer:
I. How different is the TB mortality rate in HIV positive persons in Lesotho compared to Zimbabwe?
First, let’s add “mortality rate” as another column in our data frame
$Mortality_Per1000 <- 1000 * (myTBdata$HIV_pos_TB_mortality + myTBdata$HIV_neg_TB_mortality)/myTBdata$Population myTBdata
Now subset the dataset to extract the TB mortality rate for both Lesotho and Zimbabwe
<- myTBdata[myTBdata$Country=="Lesotho", "Mortality_Per1000"]
Lesotho_mortalityrate <- myTBdata[myTBdata$Country=="Zimbabwe", "Mortality_Per1000"]
Zimbabwe_mortalityrate <- Lesotho_mortalityrate / Zimbabwe_mortalityrate Relative_Mortality_Rate
How many times higher is the mortality rate for TB in Lesotho as it is in Zimbabwe?
paste("The relative mortality rate is", round(Relative_Mortality_Rate, 2), sep=" ")
J. Finally in this section, let’s look at what can go wrong when reading in data files.
In order to complete this section, you would need to download readfileexample_1.txt, readfileexample_2.txt, and readfileexample_3.txt.
(a) There is not an equal number of columns in each of the rows.
<- read.table("readfileexample_1.txt", header=TRUE) readFile_a
How do you fix this error? Hint: set missing values in the data file to be ‘Not Assigned’ by adding them as NA in the original file. Try running this line again with the updated file.
(b) The wrong delimiter is used
<- read.table("readfileexample_2.txt", header=TRUE) readFile_b
Is an error given? Check out ‘readFile_b’ - is it correct?
Answer:
How do you fix this? Ask R for help (?read.table) Which option do you need to specify?
Answer:
Is there another way of fixing this problem?
Answer:
(c) The names are read in as data rows rather than names
<- read.csv("readfileexample_2.txt", header=FALSE) readFile_c
Is an error given? Check out ‘readFile_c’ - is it correct?
Answer:
Type a new line of code to correct this problem (hint: copy-paste from above and change one of the options)
##### YOUR CODE GOES HERE #####
(d) One of more of the columns contain different classes
<- read.table("readfileexample_3.txt", header=TRUE) readFile_d
Is an error given? Check out ‘readFile_d’ - is it correct?
Answer:
How do you fix this issue? Hint: check the ‘class’ of the problem column. Try running this line again with an updated file.
##### YOUR CODE GOES HERE #####
The solutions can be accessed here.