[R] Extracting arithmetic mean for specific values from multiple .txt-files

Rui Barradas ruipbarradas at sapo.pt
Wed Jul 11 11:33:04 CEST 2012


Hello,

Try


make.row <- function(x, skip = 14, column){
     dat <- read.table(x, skip = skip - 1, header = TRUE, 
stringsAsFactors = FALSE)
     vpNum <- dat$vpNum[1]
     trial <- length(dat[[ column ]])
     correct <- sum(dat$correct == 1)
     result <- c(vpNum, trial, correct, correct/trial)
     names(result) <- c("vpNum", column, "correct", "ratio")
     result
}


files <- list.files(pattern = "^XYZ_.*.txt")
ratios <- t(sapply(files, make.row, column = "trial"))
ratios <- data.frame(ratios, row.names = seq_len(nrow(ratios)))
ratios


I think it's what you want.

Rui Barradas

Em 11-07-2012 06:18, vimmster escreveu:
> Dear Rui,
>
> 1) With test subject I mean each file (I have posted three similar files
> above (2, 50 and 1112), but each test subject has one exact file (which
> differs of course! --> 2, 50 an 1112 are the same file but I renamed it for
> the problem described ans solved above). In this file the vpNum is always
> the same (for each test subject of course; example: for test subject 44 it
> is always vpNum = 44). The examples above (2, 50 and 1112) are in fact all
> the second test subject's file (vpNum always "2").
>
> 2) With "trials" or "trialCount" I mean the number of trials (149 in your
> example, and 151 in the examples 2, 50 and 1112). But the number of trials
> differs between subjects, because in the examples below (vpNum = 3, 43 and
> 63) it is 152 (for vpNum = 3), 150 (for vpNum = 43) and 157 (for vpNum =
> 63).
>
> http://r.789695.n4.nabble.com/file/n4636106/XYZ_3.txt XYZ_3.txt
> http://r.789695.n4.nabble.com/file/n4636106/XYZ_43.txt XYZ_43.txt
> http://r.789695.n4.nabble.com/file/n4636106/XYZ_63.txt XYZ_63.txt
>
> 3) I mean the number of correct answers given per test subject (for example
> for test subject 3 in the previous example (5 rows above) we have 152 trials
> and 4 trials that are not correct, which means 148 correct trials (with the
> categorical value "1")). So in R this would be the ratio of:
>> 148/152
> [1] 0.9736842
>
> The wanted output should (if possible) look like this (here only for vpNum =
> 3 !!!):
> vpNum trial OR trialCount correct (reactions) ratio (which means: number
> correct / trialCount)
> 3              152                              148
> 0.9736842
>
> The final output should look like this:
> vpNum trial OR trialCount correct (reactions) ratio (which means: number
> correct / trialCount)
> 1              n                                  n
> x
> 2              n                                  n
> x
> 3              152                              148
> 0.9736842
> and so on until vpNum = 65 (In my question before I forgot to ask for the
> column "vpNum", sorry about that!).
>
> I hope this makes it more clear!
>
> Thanks for your time and help!
>
> Kind regards
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Extracting-arithmetic-mean-for-specific-values-from-multiple-txt-files-tp4635809p4636106.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list