Data wrangling: In-Class Exercise 6

2020-Spring [Data Management] Instructor: SHEU, Ching-Fan

CHIU, Ming-Tzu

2020-04-11

Convert the data set probe words from long to wide format as described.

data set:

Dr. Paul Ammon at the University of California at Berkeley studied whether a person’s immediate memory of a sentence is organized according to the sentence’s phrase structure. In the study, subject listened to tape-recorded sentences. Each sentence was followed by a “probe word” taken from one of five positions in the sentence. The subject was to respond with the word that came immediately after the probe word in the sentence.

Source: Timm, N. (1975). Multivariate Analysis, with Applications in Education and Psychology. p. 232.

Column 1: Subject ID

Column 2: Response time

Column 3: Position

to

Column 1: Subject ID

Column 2: Speed of response for the proble word in the first position (transformed reaction time)

Column 3: Speed of response for the proble word in the second position

Column 4: Speed of response for the proble word in the third position

Column 5: Speed of response for the proble word in the fourth position

Column 6: Speed of response for the proble word in the fifth position

讀取資料

dta <- read.table("C:/Users/TheorEco Lab/Desktop/108-2/DataManagement/0330/probel.txt", header = TRUE)
head(dta)
#>    ID Response_Time Position
#> 1 S01            51        1
#> 2 S01            36        2
#> 3 S01            50        3
#> 4 S01            35        4
#> 5 S01            42        5
#> 6 S02            27        1

使用 reshape() 讓資料變型

result <- reshape(dta, idvar = "ID", timevar = "Position", direction = "wide")
result
#>     ID Response_Time.1 Response_Time.2 Response_Time.3 Response_Time.4
#> 1  S01              51              36              50              35
#> 6  S02              27              20              26              17
#> 11 S03              37              22              41              37
#> 16 S04              42              36              32              34
#> 21 S05              27              18              33              14
#> 26 S06              43              32              43              35
#> 31 S07              41              22              36              25
#> 36 S08              38              21              31              20
#> 41 S09              36              23              27              25
#> 46 S10              26              31              31              32
#> 51 S11              29              20              25              26
#>    Response_Time.5
#> 1               42
#> 6               27
#> 11              30
#> 16              27
#> 21              29
#> 26              40
#> 31              38
#> 36              16
#> 41              28
#> 46              36
#> 51              25

更改欄位標籤符合要求

colnames(result) <- c("ID", "Pos_1","Pos_2", "Pos_3", "Pos_4","Pos_5")
result
#>     ID Pos_1 Pos_2 Pos_3 Pos_4 Pos_5
#> 1  S01    51    36    50    35    42
#> 6  S02    27    20    26    17    27
#> 11 S03    37    22    41    37    30
#> 16 S04    42    36    32    34    27
#> 21 S05    27    18    33    14    29
#> 26 S06    43    32    43    35    40
#> 31 S07    41    22    36    25    38
#> 36 S08    38    21    31    20    16
#> 41 S09    36    23    27    25    28
#> 46 S10    26    31    31    32    36
#> 51 S11    29    20    25    26    25