Gender
Sector
Col5 ?

disregard those columns, i mean actual data would be like this

data<-data.frame(
col1=c(1,1,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,1,1,1,NA,1,1,NA,NA,NA,NA,1,NA,NA,NA,NA,1,NA,1),
col2=c(1,1,1,1,1,NA,NA,NA,NA,1,1,1,1,1,NA,NA,NA,1,1,1,NA,1,1,1,1,1,NA,NA,NA,1,1,1,1,1,1,1,NA,NA,NA),
col3=c(1,1,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,1,1,1,NA,NA,NA,1,NA,NA,1,1,1,1,1,NA,NA,1),
col4=c(1,NA,NA,NA,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA),
)
data$col1<-factor(data$col1, levels=1, labels="Sales")
data$col2<-factor(data$col2, levels=1, labels="OPS")
data$col3<-factor(data$col3, levels=1, labels="Management")
data$col4<-factor(data$col4, levels=1, labels="HR")

Can I ask please if this is a homework task?

There was another question posted recently with an Identical dataset and had also had a (failed?) attempt very close that which I posted above.

No Idea about that, which question..??

I don't wish to cast aspersions, I only want to know if this might be a homework assignment.

I have no issue helping people with their studies, but I would like to be aware - it may influence the way in which I provide help.

yes this is a kind of assignment

Excellent. Please do continue to seek guidance here, but please identify your posts as homework. Most people will probably try to guide you understanding a bit more in their answers.

Also please consult this

#how to make this faclist 
faclist <- list(
  col1 = "Sales",
  col2 = "OPS",
  col3 = "Management",
  col4 = "HR"
)

#from
var <- c("col1","col2","col3","col4")
labels <- c("Sales","OPS","Management","HR")

#do 
faclist2 <- setNames(purrr::map(labels,identity),
                     var)

#check
all.equal(faclist,faclist2)

approach should be this only like input data will be ldat (list of dataframes)

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.