1

I have multiple .csv files with names "sub block#":

01 block1 01 block2 01 block3 02 block1 02 block2 02 block3

I would like to

  1. get the mean for each participant grouped by "cue" (my current code tries get the mean for each file, but not for each subject)
  2. save the output to one .csv file
files <- list.files(path="C:/Users/", pattern="*.csv", full.names=TRUE, 
    recursive=FALSE)
            for (file in files ){
              data<-read.csv(file)
              summarize.stat<-
                data%>% 
              group_by(Cue) %>%  # grouping stats
              drop_na() %>%
              summarize(count=n(),
                        mean.RT= mean(RT))
              write.csv(summarize.stat, paste("C:/Users/",'RTcue,csv',sep = ""), 
    row.names = FALSE, quote = FALSE)
         }

This is what shows in the Console summarise() ungrouping output (override with .groups argument) summarise() ungrouping output (override with .groups argument) summarise() ungrouping output (override with .groups argument) summarise() ungrouping output (override with .groups argument) summarise() ungrouping output (override with .groups argument) summarise() ungrouping output (override with .groups argument)

** Error: Problem with summarise() input mean.RT. x error in evaluating the argument 'x' in selecting a method for function 'mean': object 'RT' not found** i Input mean.RT is mean(RT). i The error occurred in group 1: Cue = "Center".

So my questions are: 1.How to fix the error? 2.and how to get the mean by each subject?

Thank you for any advice! I've struggled with it for quite a while.

Sophy
  • 151
  • 7

1 Answers1

1

There are multiple ways to tackle this issue. One option is to use a conditional logic with if to check whether the column exists in the read data and summarise

library(dplyr)
library(purrr)
library(readr)
library(stringr)
nm1 <- tools::file_path_sans_ext(basename(files))
imap_dfr(setNames(files, nm1), ~ {
            dat <- read_csv(.x) 
            if(exists('RT', where = dat)) {
                   dat %>%
                     group_by(Cue) %>%
                     summarise(Count = n(), 
                        mean.RT= mean(RT, na.rm = TRUE), .groups = 'drop')  %>%
             mutate(filename = .y)
            
      
               } else tibble(Cue = first(dat$Cue),
         mean.RT = NA_real_, filename = .y)
               }) %>%

    write_csv(path  =  file.path("C:/Users/", "RTcue.csv"))

Or another option is tryCatch

akrun
  • 874,273
  • 37
  • 540
  • 662
  • You are so right! One file doesn't. Now the code runs but didn't generate the output "RTcue.csv" – Sophy Nov 03 '20 at 20:29
  • @Sophy Sorry, I will update it. One thing i noticed is that the file name is the same for all the output. WOuldn't that overwrites the output? – akrun Nov 03 '20 at 20:29
  • In the Console it says "`summarise()` ungrouping output (override with `.groups` argument)". The output for each file is in the Console but losing the original file name. I would like the output to one .csv with the original file name. Thank you! – Sophy Nov 03 '20 at 20:33
  • @Sophy Don't worry about that warning. It is just a friendly warning. You can use `.groups = 'drop'`. Please check [here](https://stackoverflow.com/questions/62140483/how-to-interpret-dplyr-message-summarise-regrouping-output-by-x-override/62140681#62140681) – akrun Nov 03 '20 at 20:35
  • @Sophy I updated the post with the original file name with 'RTcue' as suffix. Can you please check if that works – akrun Nov 03 '20 at 20:36
  • Thank you so much! The only thing is it outputs to each individual file instead of one file with filename and mean.RT – Sophy Nov 03 '20 at 20:41
  • @Sophy Ok, then I misunderstood from your code. I thought you wanted multiple files. WIll update – akrun Nov 03 '20 at 20:42
  • It won't generate the output file. this is the error "Error in as_mapper(.f, ...) : argument ".f" is missing, with no default" – Sophy Nov 03 '20 at 20:52
  • @Sophy can you try with the update. I couldn't test it. So, it is just based on my intuition – akrun Nov 03 '20 at 20:54
  • 1
    @Sophy Sorry, an `,` was missing. Can you test now – akrun Nov 03 '20 at 21:00