0

My problem was initially solved from the answer in this post: Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "list"

I have the same format as the linked post, a large list (376) data frames. Here's my adapted code from the above solution.

data_above20trials %>% 
  map(~summarise(group_by(., odor_setting),
                 trials_performed = length(odor_setting),
                 trial_length = mean(trial_length),
                 percent_correct = mean(success) * 100)) %>% 
  {. ->> percent_correct} #save to its own variable in environment

This was working for a long time and am completely unsure of why it stopped. I need the group_by function to calculate the new variables (trials_performed, trial_length, percent_correct) based on the odor_setting (0/1) and save that to the new variable "percent_correct".

The code executes, but I only get one row for each new saved df, which is the combined total, not the individual condition totals.

Here is some code to replicate the issue with a list of dfs:

d1 <- data.frame(y1 = c(1, 2, 3), y2 = c(4, 5, 6), y3 = c(12, 3, 1), odor_setting = c(0, 1, 0))
d2 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4), y3 = c(11, 15, 44), odor_setting = c(1, 1, 0))
d3 <- data.frame(y1 = c(3, 7, 1), y2 = c(12, 5, 9), y3 = c(19, 1, 14), odor_setting = c(1, 1, 1))
my.list <- list(d1, d2, d3)

My solution applied to code above:

my.list %>% 
  map(~summarise(group_by(., odor_setting),
                 trials_performed = length(odor_setting),
                 trial_length = mean(y2),
                 percent_correct = mean(y1) * 100)) %>% 
  {. ->> test_frame} 

test_frame should have within each data frame, 2 rows and 4 columns, but it does not! I want to see an odor_setting column, with 0 and 1 in the rows, having calculated trial info for each odor setting.

Amanda
  • 49
  • 6

1 Answers1

0

I restarted R and it worked again as written. Delightfully frustrating lesson!

Amanda
  • 49
  • 6