Post by matia on May 20, 2021 7:17:52 GMT
Hello,
I was having troubles getting multiple RT datafiles into one datatable. Once I've managed to do it, I thought of sharing a code that can be easily used for anyone interested. It is a bit messy, and it can certainly be improved, but it certainly does the job:
library (tidyverse)
library (readxl)
list_of_files <- list.files(path = ".\\Folder", recursive = TRUE, #Once you've exported all the datafiles (in *.txt format into a Folder)
pattern = "\\.txt$", #you import them using this command (it makes a vector of the file names
full.names = TRUE) #you wish to import (be careful when defining a directory in which
#the files are exported to
data_file <- list_of_files %>% #Honestly, I am not 100% sure what the code does, but
set_names(.) %>% #col_names=FALSE because there are no col names in Psytoolkit datafiles
map_df(read_table2, col_names=FALSE,.id = "FileName") #and each datafile will be added a column containing the datafile names
data_file$FileName<-substring(data_file$FileName,93,128) #PT's datafiles names contain information on the date and time the exp
#was done, about the experiment name and the participant's code
#You are really interested in the participant's name since you will be
#joining this data_file and data.xls (containing some survey data) by this
#participant's name
###IMPORTANT! - look at the command substring (data_file$FileName, X,Y)
#it defines the starting and ending position of the participant's code in the
#datafile name. 93 and 128 refer to my own because it depends on the name
#you gave to your experiment, time, etc. (but it is always 35 characters long)
colnames(data_file)<-c("ParticipantID", #As there are no column names, time to create them, just make sure
"Variable1", #that the first column contains info on the participant's code
"Variable2",
"VariableN")
data_survey <- read_excel(".\\Folder\\data.xlsx") #Importing the data.xlsx table containing info on survey
#The survey data has a column named participant ID, what I do is to open
#the Excel file and change this column name by hand into ParticipantID
#I just find it easier to do it in Excel than writing a command in R
###IMPORTANT since I am already in the Excel, I select this ParticipantID
#column and Replace "s." with "" [nothing] and ".txt" also with "" nothin
#so it contains only participants' codes
data_file_final=full_join(data_file,data_survey,by="ParticipantID") #Finally, I join the two tables by this column "ParticipantID" and have all
#the information on each participant
Hope that at least someone will find this helpful
Best, Matia