tidyr::pivot_longer() with duplicate problems with no apparent duplicate column names or dataset in R
I suspect you already have a column named name or value before you run pivot_longer, which by default tries to create columns with those names. As noted here, the error message isn't necessarily clear that's the problem.
Try grep("name", colnames(as)) and grep("value", colnames(as)) to find those columns.
Either rename in your data frame or use pivot_longer( ... names_to = "a_new_name_col", values_to = "a_new_value_col")
data.frame(a = 1:2, name = 3:4, value = 7:8) %>%
  tidyr::pivot_longer(a)
#Error in `vec_cbind()`:
#! Names must be unique.
#✖ These names are duplicated:
#  * "name" at locations 1 and 3.
#  * "value" at locations 2 and 4.
#ℹ Use argument `names_repair` to specify repair strategy.
#Run `rlang::last_trace()` to see where the error occurred.

data.frame(a = 1:2, name2 = 3:4, value2 = 7:8) %>%
  tidyr::pivot_longer(a)
## A tibble: 2 × 4
#  name2 value2 name  value
#  <int>  <int> <chr> <int>
#1     3      7 a         1
#2     4      8 a         2


My goal is to change value 99999 with the value adjacent to it unless it's 99999 again.
I took the advice from here before, now I am having a new problem.
MRE:
'as' is a dataframe with 9 different cohort datasets; 10030 obs of 7060 variables. I am mainly (as of now) dealing with as$AS1_WEIGHT ... as$AS9_WEIGHT
> as %>%
+     select(starts_with("AS") & ends_with("_WEIGHT")) %>% head() %>% dput()

structure(list(AS1_WEIGHT = c(72, 59, 50, 55.2, 82.1, 50.4), 
    AS2_WEIGHT = c(74.8, NA, NA, 54.8, 84.5, 52.5), AS3_WEIGHT = c(75.2, 
    NA, NA, 55.9, 81.7, 54.6), AS4_WEIGHT = c(75, NA, NA, 55.1, 
    80.6, NA), AS5_WEIGHT = c(75.4, NA, NA, 58.8, 89.5, NA), 
    AS6_WEIGHT = c(77.3, NA, NA, NA, NA, NA), AS7_WEIGHT = c(70.7, 
    NA, NA, 56, NA, NA), AS8_WEIGHT = c(73.8, NA, NA, 55.5, NA, 
    NA), AS9_WEIGHT = c(74.5, NA, NA, 54.8, NA, 52)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))



as %<>%
  mutate(row = row_number()) %>%
  tidyr::pivot_longer(starts_with("AS") & ends_with("_WEIGHT")) %>%
  mutate(value = if_else(value == '99999', lead(value), value), .by = row) %>%
  pivot_wider(names_from = name, values_from = value)

returns:
Error in tidyr::pivot_longer():
! Names must be unique.
✖ These names are duplicated:

"name" at locations 7049 and 7053.
"value" at locations 7050 and 7054.
ℹ Use argument names_repair to specify repair strategy.
Run rlang::last_trace() to see where the error occurred.

So I ran this code to see which columns are duplicated:
> dup_col <- duplicated(base::as.list(as))
colnames(as[dup_col])

character(0)

I ran another code to see if I am referring to the right columns
> as %>%
  select(starts_with("AS") & ends_with("_WEIGHT")) %>%
  colnames()

[1] "AS1_WEIGHT" "AS2_WEIGHT" "AS3_WEIGHT" "AS4_WEIGHT" "AS5_WEIGHT" "AS6_WEIGHT" "AS7_WEIGHT" "AS8_WEIGHT"
[9] "AS9_WEIGHT"


Thank you in advance!

