I used ChatGPT for work for real for the first time today

I had tried it before… once asking it to write R code for a non-standard
task involving date objects (I wasn’t able to frame the problem well enough for
it to give me something useful); once, facing some legacy teaching materials in
need of major revision, asking it to write a fresh introduction to the activity
(it did great, but I didn’t use its output after all). Today it clearly boosted
my progress.

I know that do() is superseded in dplyr, but I don’t yet have a handle on
some of the newer functions like across, and I had never encountered
group_modify() before today. Hoping ChatGPT would teach me, I asked for help.
As you can see, it gave me a nice example… the logical filters and grouping
factors are a bit simpler here than in my actual application, but basically this
is doing what I needed.

I did have to play with the syntax a little to get it work. It was still probably quicker than my normal process…. with a similar outcome…

I have working code and now I kind of know how to use group_modify().

Me: Write an R script that will group a dataframe flowers by factors site, date, and round, and if the string “ectar” is in the column plant_code for any of the rows, it will replace plant_species with “NONE” if there is exactly one row and will remove the row containing “ectar” if there is more than one row.

ChatGPT: Certainly! Here’s an R script that performs the described operations on the flowers dataframe:

# Grouping the dataframe by factors site, date, and round
grouped_data <- group_by(flowers, site, date, round)

# Function to process each group
process_group <- function(group) {
  # Check if "ectar" is present in the plant_code column
  if ("ectar" %in% group$plant_code) {
    # Get the number of rows in the group
    num_rows <- nrow(group)

    # If there is exactly one row, replace plant_species with "NONE"
    if (num_rows == 1) {
      group$plant_species <- "NONE"
    }
    # If there is more than one row, remove the row containing "ectar"
    else {
      group <- group[!grepl("ectar", group$plant_code), ]
    }
  }

  # Return the processed group
  return(group)
}

# Applying the process_group function to each group
processed_data <- do(grouped_data, process_group)

# Viewing the resulting dataframe
print(processed_data)

Make sure to replace flowers with the actual name of your dataframe. The script first groups the dataframe by the specified factors (site, date, and round). Then, it defines a function process_group to handle each group. The function checks if “ectar” is present in the plant_code column. If it is, the function performs the required operations based on the number of rows in the group. Finally, the do() function applies the process_group function to each group, and the resulting processed dataframe is printed.

Please note that you need to have the dplyr package installed in your R
environment for the script to work, as it uses the group_by() and do() functions from that package. You can install it using install.packages(“dplyr”).

Me: This is great. Can you do this in pure dplyr, perhaps using across(), rather than do()?

ChatGPT: Certainly! Here’s an updated version of the script that uses dplyr and across() to achieve the same result:

library(dplyr)

# Grouping the dataframe by factors site, date, and round
grouped_data <- flowers %>%
  group_by(site, date, round)

# Function to process each group
process_group <- function(group) {
  # Check if "ectar" is present in the plant_code column
  if ("ectar" %in% group$plant_code) {
    # Get the number of rows in the group
    num_rows <- nrow(group)

    # If there is exactly one row, replace plant_species with "NONE"
    if (num_rows == 1) {
      group <- group %>%
        mutate(plant_species = "NONE")
    }
    # If there is more than one row, remove the row containing "ectar"
    else {
      group <- group %>%
        filter(!grepl("ectar", plant_code))
    }
  }

  # Return the processed group
  return(group)
}

# Applying the process_group function to each group
processed_data <- grouped_data %>%
  group_modify(process_group)

# Viewing the resulting dataframe
print(processed_data)

In this version, the group_by() function is used to group the dataframe. Then, the process_group() function is defined to handle each group. The function checks if “ectar” is present in the plant_code column and performs the necessary operations using mutate() and filter(). Finally, the group_modify() function is used to apply the process_group() function to each group, and the resulting processed dataframe is printed.

This version uses across() indirectly through the mutate() function. If you want to perform operations across multiple columns simultaneously using across(), you can modify the script accordingly.

https://chat.openai.com/share/a22985b8-7000-40e2-abc6-4ae9171d4117

In the end, I changed my code so that the group_modify() call doesn’t just have the function name, instead it reads something like group_modify(~process_group(.)). But ChatGPT got me almost 100% of the way there, very, very fast.

Leave a Reply

Your email address will not be published. Required fields are marked *