getting the vector from list

i have a list of column definition now i want to get output as a list of column names which are defined as character and which start with "col"

I have very big list and names of columns are varied

library(readr)

# Define the cols variable
cols <- readr::cols(
region = col_character(),
name = col_character(),
  col1 = col_character(),
  col2 = col_double(),
  col3 = col_integer(),
col4 = col_double(),
col5 = col_double(),
col6 = col_integer(),
col7 = col_double(),
col8 = col_character(),
date = col_character()
)
column_names <- names(cols)
print(column_names)

its not working for me and gives me output as "cols" "default" "delim"
but i want output as vector of columns like c(col1,col8)

names(cols$cols)

also i want names which are defined as character, how i can fetch, and start with col only

library(tidyverse)
# Define the cols variable
cols <- readr::cols(
  region = col_character(),
  name = col_character(),
  col1 = col_character(),
  col2 = col_double(),
  col3 = col_integer(),
  col4 = col_double(),
  col5 = col_double(),
  col6 = col_integer(),
  col7 = col_double(),
  col8 = col_character(),
  date = col_character()
)

enframe(cols$cols) |> 
  rowwise() |> 
  filter(identical(value,
                   col_character())) |> 
  ungroup()
# # A tibble: 5 × 2
# name   value     
# <chr>  <list>    
#   1 region <cllctr_c>
#   2 name   <cllctr_c>
#   3 col1   <cllctr_c>
#   4 col8   <cllctr_c>
#   5 date   <cllctr_c>

Thanks, this working as expected and the last thing i also want is exclude column names which do not start with "col" . so in output i want only col1 and col8

enframe(cols$cols) |> 
  rowwise() |> 
  filter(identical(value,
                   col_character()),
         startsWith(name,"col")) |> 
  ungroup()

cols1 <- tibble::enframe(colsf$cols) %>% rowwise() %>%
filter(identical(value, col_character()),
startsWith(name,"Q") & !endswith(name,"_target")) %>% ungroup() %>% select(name) %>% tibble::deframe()

just last one thing , i am also trying to add one thing like to exclude column name which ends with text "Target" and its giving me a error, i mean not working

is not the same as endsWith

yes, updated it but still its showing column names with "Target"

Target or target ?

Yes target, it must be target, please explain what i am doing wrong and why its not working for me

I don't have your code or your data, I only have the earlier examples.
I cant see what mistake you are making.

cols <- readr::cols(
region = col_character(),
name = col_character(),
col1 = col_character(),
col2 = col_double(),
col3 = col_integer(),
col4 = col_double(),
col5 = col_double(),
col6 = col_integer(),
col7 = col_double(),
col8 = col_character(),
date_target = col_character()
)

cols1 <- tibble::enframe(cols$cols) %>% rowwise() %>%
filter(identical(value, col_character()),
startsWith(name,"col") & !endswith(name,"_target")) %>% ungroup() %>% select(name) %>% tibble::deframe()

Are you getting ...

Error in `filter()`:
ℹ In argument: `startsWith(name, "col") & !endswith(name, "_target")`.
ℹ In row 1.
Caused by error in `endswith()`:
! could not find function "endswith"

ok got it what i was doig wrong it should be endsWith
Thanks for all the help

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.