-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathread_multiple_txt_files.R
More file actions
68 lines (44 loc) · 2.19 KB
/
Copy pathread_multiple_txt_files.R
File metadata and controls
68 lines (44 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# install, update and load packages -----------------------------------------------
pkg <- c("stringr", "reshape2", "dplyr", "ggplot2", "magrittr")
new.pkg <- pkg[!(pkg %in% installed.packages())]
if (length(new.pkg)) {
install.packages(new.pkg)
}
library(stringr)
library(reshape2)
library(dplyr)
library(ggplot2)
# Read in data ------------------------------------------------------------
# update this file path to point toward appropriate folder on your computer
folder <- "/Users/majerus/Desktop/thesis_projects/linguistics/Yevgeniy/exp1/" # path to folder that holds multiple .csv files
file_list <- list.files(path=folder, pattern="*.txt") # create list of all .csv files in folder
# read in each .csv file in file_list and rbind them into a data frame called data
data <-
do.call("rbind",
lapply(file_list,
function(x)
cbind(file = x, read.table(paste(folder, x, sep=''),
header = TRUE,
stringsAsFactors = FALSE))))
# Clean data --------------------------------------------------------------
clean.data <- function(df){
df <- cbind(df, colsplit(df$stimulus, ',', names = c('s1','s2', 's3')))
df$answer <- ifelse(str_count(df$stimulus, 'A') == 2, 'A', 'B')
df$correct <- ifelse(df$response == df$answer, 1, 0)
df$reactionTime <- as.numeric(df$reactionTime)
return(df)
}
data <- clean.data(data)
# Write out data ----------------------------------------------------------
write.csv(data, paste(folder,'cleaned_data.csv', sep = ''), row.names = FALSE)
# Create data frame of summary statistics ---------------------------------
summary_stats <-
data %>%
group_by(subject, correct, answer) %>%
summarise(count = n(),
mean_reactionTime = mean(reactionTime, na.rm = TRUE),
sd_reactionTime = sd(reactionTime, na.rm = TRUE),
min_reactionTime= min(reactionTime, na.rm = TRUE),
max_reactionTime = max(reactionTime, na.rm = TRUE))
# Write out data frame of summary statistics ------------------------------
write.csv(summary_stats, paste(folder,'summary_stats.csv', sep = ''), row.names = FALSE)