converting columns of data frame to factor

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
  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 ....
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



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

fac_conv_lapply <- function(data,col_names,labels) {
list_of_fac <- purrr::map_chr(seq_len(length(col_names)),
ldat <- map(data, ~ {
.x[col_names] <- map2(.x %>%
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.

Hi this works perfectly fine on the data frame but i want to give ldat in place of data.
I want to run it for ldat.

actually the requirement is only for ldat, because have created function all on the basis of ldat. that's why i am looking for a function which works for ldat.
also please explain for me to understand.

ldat is completely dertived from data (although there seem to be typos in the code for its construction that you shared)
Given that ldat is composed from data, process data then make ldat... why not ?

It is possibly to do it onto ldat, but its more complicated, I don't know why that effort would be justified.

ldat is not a dataframe, its a list of two dataframes.
mutate will not work on it directly.

# a representative ldat
(ldat <- list(slice(data,1:20),


Note that such an approach will fail if a dataframe within ldat doesnt have every level corresponding to every possible label of each factor.
Again, why its a better idea to process to factors on data first, and then build ldat from that