R Error: Can't subset columns that don't exist

Dear Community, I'm trying to run the code down below , however when I run it R keeps saying "Error: Can't subset columns that don't exist.
Column tree_id doesn't exist." When I delete "tree_id" same Error refers to "plot_id", "block" and so on. What can I do to solve this?

DF10 <- mgs_raw_clean %>%
group_by_at(c("tree_id","plot_id","block","def_risk","treatment","tree_nr","oak_spec")) %>%
summarise(gall_pa = sum(gall_abund != 0),
agamous = sum(agamous), sexual = sum(sexual), single = sum(single), unknown = sum(unknown),
gall_abund = sum(gall_abund), .groups = "drop") %>%
mutate(total_galls = agamous + sexual + single + unknown + gall_abund) %>%
dplyr::select(c(tree_id, plot_id, block, def_risk, treatment, tree_nr, oak_spec,
n_leaves, mildew_cov, gall_pa, gall_abund, everything()))

Hello @Tess ,

the best way to solve this is to provide a reproducible example .
In that way we don't have to create our own test-file to see what is wrong.

But probably you can solve the problem yourself. Start with looking at

DF10 <-  mgs_raw_clean %>%
         header() %>%
         print()

If that looks all right then insert the next line from the pipeline:

DF10 <-  mgs_raw_clean %>%
        group_by_at(c("tree_id","plot_id","block","def_risk","treatment","tree_nr","oak_spec")) %>%
         header() %>%
         print()

Continue doing that until the error occurs.
In that way you find out at which point the error occurs and probably you can handle it yourself.
If not then copy the output of the last step that was working as expected in your next message to the community.

Hi!
I agree with @HanOostdijk regarding the reproducible example and the suggested debugging steps.
However, looking at your code I guess the error is in the call to select(), where you want to select columns that dont exist (anymore) at that point in the pipe. Note that summarise() will return only the columns you define within the function call and the grouping columns. Specifying groups = "drop" as you did also removes the grouping columns.
So I guess follow the guide by @HanOostdijk to debug your pipe and find the columns that are not present there anymore and remove them from your call to select().
Best,
Valentin

Hi HanOostdijk,

I didn't know about the reproducible example. Thanks for the hint. I hope you can work with the one down below. So as I can now see, the problem probably is that R can not open the file BRC19-galls-tree.csv . How can I solve this though? I was sure that the first code "mgs_raw_clean <- read.csv .... " is the one for loading the data.

mgs_raw_clean <- read.csv("./Data/BRC19-galls-tree.csv", header = TRUE, sep = ";")
#> Warning in file(file, "rt"): can not open file './Data/BRC19-galls-tree.csv' 
#> No such file or directory
#> Error in file(file, "rt"): can not open connection

DF10 <- mgs_raw_clean %>%
  group_by_at(c("tree_id","plot_id","block","def_risk","treatment","tree_nr","oak_spec")) %>%
  summarise(gall_pa = sum(gall_abund != 0),
            agamous = sum(agamous), sexual = sum(sexual), single = sum(single), unknown = sum(unknown),
            gall_abund = sum(gall_abund), .groups = "drop") %>%
  mutate(total_galls = agamous + sexual + single + unknown + gall_abund) %>%
  dplyr::select(c(tree_id, plot_id, block, def_risk, treatment, tree_nr, oak_spec,
                  n_leaves, mildew_cov, gall_pa, gall_abund, everything()))
#> Error in mgs_raw_clean %>% group_by_at(c("tree_id", "plot_id", "block", : can not find function "%>%" 

Hello @Tess,

your input file "./Data/BRC19-galls-tree.csv" is not found.
Check if there is a file BRC19-galls-tree.csv in the subfolder Data of your working directory .
You can find out what directory (folder) is considerd "the working directory" by using
getwd()
in your command window.

In your message I see a second error (can not find function "%>%") but this is probably caused by the first error. If the message keeps appearing after you have solved the first error, be sure to include

library(dplyr)
library(magrittr)

in your code.