converting columns of data frame to factor

its not clear what you are trying to say.
maybe you should restate your issue in full

rather than input parameter for columns and labels like
factor_list <- data.frame (colnames=c("col1","col2"....),col_labels =c("sales","OPS"....)

in this i was giving a list of var and their labels but i want to give parameters separately
like
var <- c("col1","col2","col3","col4")
labels <- c("Sales","OPS","Management","HR")

i mean the question is same as I asked but the parameter will be like separate var and label....

but i will apply the same way like below you helped me
(ldat <- list(slice(data,1:20),
slice(data,20:39)))

purrr::map(ldat,
~mutate(.,
!!!parse_exprs(list_of_mutators)))

i am also updating in question for more understanding

I provided that in my answer.
You can produce the table of parameters from the two vectors provided.

actually that is based on faclist (a list of col and labels but if I want to give separate vector of col and labels)

I showed how to make faclist from two vectors.
Read it again

sorry if i am unable to make you understand....
Thanks, but my requirement is like below, I have N list of data frames like below

df1<- data
  df1$cc1<-1
  df2<- subset(df, col5 == 'Local')
  df$cc2<-ifelse(df$col5 == 'Local',1,NA)
  lst<-list(df$cc1, df$cc2)
  ldat<-list("ALL" = df, "Local" =df2)

after defining list of dataframes , it should work like ....
 
colnames=c("col1","col2"...."col4")
col_labels =c("sales","OPS"...."HR")
# so here I will be just needed to update the list of columns and their labels

conv_frac <- function(dataset,colnames,collabels){
for(i in 1:ldat)
lapply(factor,ldat(i))  # may be lapply or any thing else

}

# then  will apply function like this

conv_frac(dataset = ldat, col =colnames  , labels = col_labels)

so the input parameters for function will be ldat, colnames, col_labels

I answered this already. you chose my answer as a solution.
you wanted to change a detail: - rather than desrcribe the relationship with a table , to do so with two vectors,.. i showed how to construct the table from the two vectors, thereby solving that requirement.

I'll let whoever else wants to continue this thread to continue without me

should i create a new topic for that....??

You could very easily adapt this piece of code to do what you want.

conv_frac <- function(dataset,colnames,collabels){
for(i in 1:ldat)
lapply(factor,ldat(i))  # may be lapply or any thing else
}

Might I suggest another look at my answer. It is very similar to this code you have written. You are very nearly there with this. If you compare your attempt with mine, I think you will gain some understanding.

I have tried to create a full function like below but doesn't work , please help if you can

###############

"sub_function"

fac_conv <- function(x) {
paste0(
"factor(", col_names[],
",labels=c('",
paste0(labels[],
collapse = "','"
), "'))"
)
}

fac_conv_lapply <- function(data,col_names,labels) {
list_of_fac <- purrr::map_chr(seq_len(length(col_names)),
fac_conv)
ldat <- map(data, ~ {
.x[col_names] <- map2(.x %>%
dplyr::select(col_names),
labels, ~ factor(.x, labels= .y))
.x} )

}

col_name <- c("col1","col2","col3","col4")
label <- c("Sales","Ops","admin","HR")

fac_conv_lapply(data = ldat,col_names = col_name, labels = label )

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.