This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
#read in the data. Save in the r environment to 'caregiversData
caregiversData <- read.csv("caregivers.csv")
#view the help file
?read.csv
## starting httpd help server ... done
#summarize the data
summary(caregiversData)
## ID Caregiver.Age Caregiver.Income Duration.of.caregiving
## Min. : 1.00 Min. :23.00 Min. : 30.0 Min. : 2.00
## 1st Qu.: 25.75 1st Qu.:35.75 1st Qu.: 200.0 1st Qu.: 12.00
## Median : 50.50 Median :41.00 Median : 255.0 Median : 24.00
## Mean : 50.50 Mean :41.85 Mean : 319.1 Mean : 29.82
## 3rd Qu.: 75.25 3rd Qu.:48.25 3rd Qu.: 308.6 3rd Qu.: 36.00
## Max. :100.00 Max. :70.00 Max. :2000.0 Max. :120.00
## Health ADL Memory.behavior.problems Cognitive.impairment
## Min. :1.0 Min. :22.00 Min. : 3.0 Min. : 0.00
## 1st Qu.:2.0 1st Qu.:42.00 1st Qu.:14.0 1st Qu.: 7.00
## Median :2.0 Median :56.00 Median :24.0 Median :15.00
## Mean :2.3 Mean :57.85 Mean :26.3 Mean :13.69
## 3rd Qu.:3.0 3rd Qu.:77.50 3rd Qu.:34.0 3rd Qu.:19.00
## Max. :4.0 Max. :90.00 Max. :66.0 Max. :27.00
## Perceived.social.support Burden Satisfaction FCGHEALTH
## Min. : 95.0 Min. : 28.00 Min. :26.0 Length:100
## 1st Qu.:119.0 1st Qu.: 52.75 1st Qu.:38.0 Class :character
## Median :133.0 Median : 69.50 Median :45.0 Mode :character
## Mean :131.6 Mean : 69.24 Mean :44.6
## 3rd Qu.:143.2 3rd Qu.: 85.50 3rd Qu.:50.0
## Max. :172.0 Max. :115.00 Max. :64.0
## Sex Relationship Education
## Length:100 Length:100 Length:100
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
##
##
##
#get variables names of the dataset
names(caregiversData)
## [1] "ID" "Caregiver.Age"
## [3] "Caregiver.Income" "Duration.of.caregiving"
## [5] "Health" "ADL"
## [7] "Memory.behavior.problems" "Cognitive.impairment"
## [9] "Perceived.social.support" "Burden"
## [11] "Satisfaction" "FCGHEALTH"
## [13] "Sex" "Relationship"
## [15] "Education"
#use a single variable using '$'
caregiversData$Caregiver.Age
## [1] 41 30 41 35 37 42 49 39 49 40 40 70 49 55 27 39 39 44 33 42 52 48 53 40 35
## [26] 47 33 41 43 25 35 35 45 36 52 41 40 45 48 50 31 33 30 36 45 32 55 50 37 40
## [51] 40 49 37 47 41 33 28 33 34 40 54 32 44 44 42 44 25 41 28 24 65 50 40 47 44
## [76] 37 36 55 45 45 23 42 38 41 25 47 35 59 49 51 54 53 49 44 36 64 51 43 54 29
#summarize caregive age
summary(caregiversData$Caregiver.Age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 23.00 35.75 41.00 41.85 48.25 70.00
#set ID as a categorical variable or factor
caregiversData$ID <- as.factor(caregiversData$ID)
summary(caregiversData$Caregiver.Age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 23.00 35.75 41.00 41.85 48.25 70.00
#get mean of caregivers age
mean(caregiversData$Caregiver.Age)
## [1] 41.85
?mean
#see Help File for Mean - includes info on defaults and potential arguments
#na.rm = logical value indicating whether NA values should be stripped before computation proceeds - R indicates missing as NA#
# Separate argument with ,
#if you have missing data set na.rm=T
mean(caregiversData$Caregiver.Age, na.rm = TRUE)
## [1] 41.85
#variance of age
var(caregiversData$Caregiver.Age)
## [1] 84.2702
#standard deviation of age
sd(caregiversData$Caregiver.Age)
## [1] 9.17988
#can also calculate by taking square root of variance
#sqrt(var(caregiversData$Caregiver.Age))
#covariance of burden and age
#cov(variable1 , variable2)
cov(caregiversData$Caregiver.Age , caregiversData$Burden)
## [1] 34.00606
#correlation
cor(caregiversData$Caregiver.Age , caregiversData$Burden)
## [1] 0.1846891
#select a subset
#bind variables to a column - variables listed will be saved into a column
threeVar <- cbind(caregiversData$Burden, caregiversData$ADL,
caregiversData$Caregiver.Age)
#covariance of the subset
cov(threeVar)
## [,1] [,2] [,3]
## [1,] 402.30545 153.612121 34.006061
## [2,] 153.61212 406.411616 9.007576
## [3,] 34.00606 9.007576 84.270202
#correlation
cor(threeVar)
## [,1] [,2] [,3]
## [1,] 1.0000000 0.37989578 0.18468913
## [2,] 0.3798958 1.00000000 0.04867297
## [3,] 0.1846891 0.04867297 1.00000000
#plot ADL vs burden
plot(caregiversData$ADL, caregiversData$Burden)
#add labels
#add labels to plot
plot(caregiversData$ADL, caregiversData$Burden, xlab = "Activities of Daily Living", ylab = "Caregiver Burden")
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.