Calculation creating list column

I have a data frame with this structure:

tibble [2 × 4] (S3: tbl_df/tbl/data.frame)
 $ CTY_GEOID: chr [1:2] "08000" "08001"
 $ NAME     : chr [1:2] "Colorado" "Adams County"
 $ 2020     : num [1:2] 5786877 520489
 $ 2024     : num [1:2] 5956729 543760

I have to take the difference between column 4 and column 3. In production these column names will change, so I am using column incides in the calculation:

    f.pop_sum <- f.pop_sum %>%
      mutate(diff = .[4] - .[3])

This calculation creates a list:

tibble [2 × 5] (S3: tbl_df/tbl/data.frame)
 $ CTY_GEOID: chr [1:2] "08000" "08001"
 $ NAME     : chr [1:2] "Colorado" "Adams County"
 $ 2020     : num [1:2] 5786877 520489
 $ 2024     : num [1:2] 5956729 543760
 $ diff     :'data.frame':	2 obs. of  1 variable:
  ..$ 2024: num [1:2] 169852 23271

This is new, bizarre behavior.
This is happening with R version 4.6.0 and R studio 2026.05.0 Build 218
I have three questions:

  1. Why is this happening?
  2. How do I unlist this column to assign the value to the indivual rows in the data frame?
  3. Is there anyway to prevent this behavior?

TIA

1 Like

This doesn't exactly answer all of your questions but I think the current best way to do this is as follows:

f.pop_sum |> 
  mutate(diff = pick(4)[[1]] - pick(3)[[1]])

I'd be interested to hear if others have better suggestions though.

1 Like

Thanks @kevbear
I was able to fix the immediate issue with the following code:

    f.pop_sum <- bind_rows(f.pop_state, f.pop_data) %>%
      mutate(diff = .[4] - .[3])
    f.pop_sum$pop_diff <- unlist(f.pop_sum$diff)

This solves the immediate problem, but now I have to check my codebase.
I think something is up with the new version of R and R-Studio. Code that has been working for years is now doing strange things.
AB

1 Like
f.pop_sum <- f.pop_sum %>%
      mutate(diff = .[4] - .[3])

contains a syntax error. Use double brackets:

f.pop_sum <- f.pop_sum %>%
      mutate(diff = .[[4]] - .[[3]])
1 Like

Hi @prubin,
Thanks for pointing this out. It solved my problem.
Cheers
AB

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.