Analyzing Patient Data

2019年03月09日 星期六 09時(shí)22分18秒

Overview

Questions

  • How do I read data into R?
  • How do I assign varirables?
  • What is a data frame?
  • How do I access subsets of a data frame?
  • How do I calculate simple statistic like mean and median?
  • where can I get help?
  • How can I plot my data?

Objectives

  • Read tabular data from a file into a program.
  • Assign values to variables.
  • Select individual values and subsections from data.
  • Perform operations on a data frame of data.
  • Display simple graphs.

Have been Given the data, wo want to:

  • Load data into memory,
  • Calculate the average value of inflammation per day across all patients, and
  • Plot the results.
    To do all that, we'll have to learn a little bit about programming.

Loading Data

change the working directory.

setwd("~/workspace/R/Lesson1")

read file from directory.

read.csv(file = "data/inflammation-01.csv", header = FALSE)

read.csvhas two arguments: the name of the file we want to read, and whether the first line of the file contains names for the columns of data. ... Assigning the second argument, header, to beFALSEindicates that the date files does not have column headers.

create variables...

dat <- read.csv(file = "data/inflammation-01.csv", header = FALSE)

Manipulating Data

Now that our data load into R, we can start doing thins with them.
first ask what type of thing in variable is:

class(dat)
[1] "data.frame"

The output tells us that it's a data frame.
see the shape, or dimensions, of the data frame:

dim(dat)
[1] 60 40

first value in dat, row 1, column 1

dat[1, 1]
[1] 0

if we want to select more than one row or column, we can use the function c(), which stands for combine.

dat[c(1, 3, 5), c(10, 20)]
V10 V20
1 3 18
3 9 10
5 4 17

Operator :, This special function generates sequences of numbers

1:5
[1] 1 2 3 4 5

All columns from row 5

dat[5, ]
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
5 0 1 1 3 3 1 3 5 2 4 4 7 6 5 3 10 8 10 6 17

V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38
5 9 14 9 7 13 9 12 6 7 7 9 6 3 2 2 4 2 0
V39 V40
5 1 1

Columns can also be addressed by name, with either the operator (ie. datV16) or square brackets (ie. dat[, 'V16']).

first row, all of the columns

patient_1 <- dat[1, ]

[1]max inflammation for patient 1

max(patient_1)

[2]max inflammation for patient 2

max(dat[2, ])

[3]minimum inflammation on day 7

min(dat[, 7])

[4]mean inflammation on day 7

mean(dat[, 7])

[5]median inflammation on day 7

median(dat[, 7])

[6]standard deviation of inflammation on day 7

sd(dat[, 7])

For every column in the data frame, the function “summary” calculates: the minimun value, the first quartile, the median, the mean, the third quartile and the max value, giving helpful details about the sample distribution.
[7]Summarize function

summary(dat[, 1:4])

apply allows us to repeat a function on all of the rows (MARGIN = 1) or columns (MARGIN = 2) of a data frame.

Thus, to obtain the average inflammation of each patient we will need to calculate the mean of all of the rows (MARGIN = 1) of the data frame.

avg_patient_inflammation <- apply(dat, 1, mean)

And to obtain the average inflammation of each day we will need to calculate the mean of all of the columns (MARGIN = 2) of the data frame.

avg_day_inflammation <- apply(dat, 2, mean)

Subsetting and Re-Assignment[^picture02]

Let’s pretend there was something wrong with the instrument on the first five days for every second patient (#2, 4, 6, etc.), which resulted in the measurements being twice as large as they should be.

whichPatients <- seq(2, 60, 2) # i.e., which rows
whichDays <- seq(1, 5) # i.e., which columns
dat2 <- dat

check the size of your subset: returns 30 5, that is 30 [rows=patients] by 5 [columns=days]

dim(dat2[whichPatients, whichDays])
dat2[whichPatients, whichDays] <- >dat2[whichPatients, whichDays] / 2
dat2

Using the Apply Function on Patient Data

Please use a combination of the apply function and indexing to:

  1. calculate the mean inflammation for patients 1 to 5 over the whole 40 days
  2. calculate the mean inflammation for days 1 to 10 (across all patients).
  3. calculate the mean inflammation for every second day (across all patients).

1.

apply(dat[1:5, ], 1, mean)

2.

apply(dat[, 1:10], 2, mean)

3.

apply(dat[, seq(1, 40, by = 2)], 2, mean)

Key Points

  • Use variable <- value to assign a value to a variable in order to record it in memory.
  • Objects are created on demand whenever a value is assigned to them.
  • The function dim gives the dimensions of a data frame.
  • Use object[x, y] to select a single element from a data frame.
  • Use from:to to specify a sequence that includes the indices from from to to.
  • All the indexing and subsetting that works on data frames also works on vectors.
  • Use # to add comments to programs.
  • Use mean, max, min and sd to calculate simple statistics.
  • Use apply to calculate statistics across the rows or columns of a data frame.
  • Use plot to create simple visualizations.

picture01
picture02
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末猖毫,一起剝皮案震驚了整個(gè)濱河市茴迁,隨后出現(xiàn)的幾起案子眼坏,更是在濱河造成了極大的恐慌,老刑警劉巖表鳍,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異另绩,居然都是意外死亡爆班,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門蜜猾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秀菱,“玉大人,你說我怎么就攤上這事蹭睡⊙芰猓” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵肩豁,是天一觀的道長(zhǎng)脊串。 經(jīng)常有香客問我,道長(zhǎng)清钥,這世上最難降的妖魔是什么琼锋? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮祟昭,結(jié)果婚禮上斩例,老公的妹妹穿的比我還像新娘。我一直安慰自己从橘,他們只是感情好念赶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著恰力,像睡著了一般叉谜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上踩萎,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天停局,我揣著相機(jī)與錄音,去河邊找鬼。 笑死董栽,一個(gè)胖子當(dāng)著我的面吹牛码倦,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锭碳,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼袁稽,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了擒抛?” 一聲冷哼從身側(cè)響起推汽,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎歧沪,沒想到半個(gè)月后歹撒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡诊胞,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年暖夭,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片撵孤。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡鳞尔,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出早直,到底是詐尸還是另有隱情,我是刑警寧澤市框,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布霞扬,位于F島的核電站,受9級(jí)特大地震影響枫振,放射性物質(zhì)發(fā)生泄漏喻圃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一粪滤、第九天 我趴在偏房一處隱蔽的房頂上張望斧拍。 院中可真熱鬧,春花似錦杖小、人聲如沸肆汹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昂勉。三九已至,卻和暖如春扫腺,著一層夾襖步出監(jiān)牢的瞬間岗照,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留攒至,地道東北人厚者。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像迫吐,于是被迫代替她去往敵國(guó)和親库菲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容