1

I am almost a beginner in R so please forgive me if i sound stupid. here is my situation:

I simulated 960 different reply patterns for a 10-item test. they are stored in my directory in .txt format as pairs, so there are 480 pairs of text files. they are named like: x_a_b_c_d or y_a_b_c_d where a, b, c and d are numbers. a is between 1 and 3, b and c are between 1 and 4 and d is between 1 and 10. I need to call each pair from the directory, convert them into frequency tables and equate them. I can do this one by one:

First I call a pair from the directory and turn them into frequency tables with freqtab() function (because equate() only works with them).

path1<-"directory//x_1_1_1_1.txt"
x1<-(read.table(path1, header=TRUE))
ftx1<-freqtab(x1, items = list(1:10, 9:10), scales = list(0:10, 0:2))

path2<-"directory//y_1_1_1_1.txt"
y1<-(read.table(path2, header=TRUE))
fty1<-freqtab(y1, items = list(1:10, 9:10), scales = list(0:10, 0:2))

then i equate them as in:

eq1<- equate(ftx1, fty1, type="linear", method="levine", ws=1)$conc$yx

however, I need to do that for all of the pairs one by one. so is there any way that I can call .txt files as pairs and equate them in one function?

jimmyquidd
  • 23
  • 2
  • Make a vector of paths with `ls`, read them into [a list of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames?s=2|238.9284) (which will fit nicely in a data frame column), then reshape or aggregate to work with the pairs – alistaire Jul 29 '19 at 00:35

1 Answers1

0

I don't know equate, so I'll answer from a generic data-processing standpoint.

xfiles <- list.files(path = "some_directory", pattern = "^x.*", full.names = TRUE)
yfiles <- gsub("^x", "y", xfiles)
bothexist <- file.exists(xfiles) & file.exists(yfiles)
xdata <- lapply(xfiles[bothexist], function(fn) {
  freqtab(read.table(fn, header = TRUE), items = list(1:10, 9:10), scales = list(0:10, 0:2))
})
ydata <- lapply(yfiles[bothexist], function(fn) {
  freqtab(read.table(fn, header = TRUE), items = list(1:10, 9:10), scales = list(0:10, 0:2))
})
eq <- Map(function(x,y) equate(x, y, type="linear", method="levine", ws=1)$conc$yx,
          xdata, ydata)
r2evans
  • 77,184
  • 4
  • 55
  • 96
  • tx for that reply. helped a lot. instead of `"^x*" ` in the first line, `"^x_[0-9]*_[0-9]*_[0-9]*_[0-9]*.txt" ` worked for me. `"^x*" ` didnt distinguish between x and y files. Besides, map function created an error as: Error in solve.default(smat) : Lapack routine dgesv: system is exactly singular: U[2,2] = 0 – jimmyquidd Jul 29 '19 at 02:00
  • Oops, the pattern should have been `"^x.*"`, with the additional dot. My bad. Does this resolve your issue? If so, please [accept it](https://stackoverflow.com/help/someone-answers). (I'm not in a rush, but new members may not be familiar with the netiquette of accepting. If you already knew and are mulling it over, take your time. Thanks!) – r2evans Jul 29 '19 at 02:18